poj 3417 Network(统计边被环覆盖的次数)

Network
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 3460 Accepted: 984

Description

Yixght is a manager of the company called SzqNetwork(SN). Now she's very worried because she has just received a bad news which denotes that DxtNetwork(DN), the SN's business rival, intents to attack the network of SN. More unfortunately, the original network of SN is so weak that we can just treat it as a tree. Formally, there are N nodes in SN's network, N-1 bidirectional channels to connect the nodes, and there always exists a route from any node to another. In order to protect the network from the attack, Yixght builds M new bidirectional channels between some of the nodes.

As the DN's best hacker, you can exactly destory two channels, one in the original network and the other among the M new channels. Now your higher-up wants to know how many ways you can divide the network of SN into at least two parts.

Input

The first line of the input file contains two integers: N (1 ≤ N ≤ 100 000), M (1 ≤ M ≤ 100 000) — the number of the nodes and the number of the new channels.

Following N-1 lines represent the channels in the original network of SN, each pair (a,b) denote that there is a channel between node a and node b.

Following M lines represent the new channels in the network, each pair (a,b) denote that a new channel between node a and node b is added to the network of SN.

Output

Output a single integer — the number of ways to divide the network into at least two parts.

Sample Input

4 1
1 2
2 3
1 4
3 4

Sample Output

3
 
一下引自http://www.cppblog.com/Yuan/archive/2010/07/11/120101.html
题意:给出一棵树,再给出M条新边,问删除一条树边以及一条新边,使之至少变为两部分的方案数

    对于新边(a,b),会在a->LCA(a,b)->b这里形成一个环,所以删除新边(a,b)以及这个环上的没有被其他环覆盖的边
    即可分成两部分。所以问题转化为求每条边被环覆盖的次数
    设dp[x]表示x所在的父边被覆盖的次数
    引进一条新边(a,b)后,dp[a]++,dp[b]++
    而这个环上的其他边的统计可以用treeDP解决,即for(v)
                                                      dp[u]+=dp[v]
    注意到LCA(a,b)的父边是不在环上的,所以每次引进新边(a,b),dp[LCA[a,b]]-=2
    最后,if(dp[i]==1)ans++        删除该边及覆盖它的那个环
           if(dp[i]==0)ans+=M  表明这条树边是桥,删除它及任意一条新边都可以
 
 
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <map>
#include <cstdlib>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define ll long long
using namespace std;

const int maxn=100005;
struct node
{
    int v,next;
}edge[maxn*2];
struct node1
{
    int v,next,id;
}que[maxn*2];
int G[maxn],Q[maxn],fa[maxn];
int dp[maxn],lca[maxn];
bool vis[maxn];
int n,m,num,numq;
void init()
{
    memset(G,-1,sizeof(G));
    memset(Q,-1,sizeof(Q));
    memset(dp,0,sizeof(dp));
    num=numq=0;
}
void add(int u,int v)
{
    edge[num].v=v;
    edge[num].next=G[u];
    G[u]=num++;
}
void addq(int u,int v,int id)
{
    que[numq].v=v;
    que[numq].id=id;
    que[numq].next=Q[u];
    Q[u]=numq++;
}
int Find_set(int x)
{
    return x==fa[x]?x:fa[x]=Find_set(fa[x]);
}
void Union(int a,int b)
{
    int ra=Find_set(a),rb=Find_set(b);
    if(ra!=rb) fa[rb]=ra;
}
void input()
{
    int a,b;
    for(int i=1;i<n;i++)
    {
        scanf("%d%d",&a,&b);
        add(a,b);
        add(b,a);
    }
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&a,&b);
        addq(a,b,i);
        addq(b,a,i);
        dp[a]++;
        dp[b]++;
    }
}
void dfs(int u)
{
    fa[u]=u;
    vis[u]=true;
    for(int i=G[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(!vis[v])
        {
            dfs(v);
            Union(u,v);
        }
    }
    for(int i=Q[u];i!=-1;i=que[i].next)
    {
        int v=que[i].v;
        if(vis[v]) lca[que[i].id]=Find_set(v);
    }
}
void treedp(int u,int pre)
{
    for(int i=G[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==pre) continue;
        treedp(v,u);
        dp[u]+=dp[v];
    }
}
void solve()
{
    memset(vis,false,sizeof(vis));
    dfs(1);
    for(int i=1;i<=m;i++)
    dp[lca[i]]-=2;
    treedp(1,-1);
    int ans=0;
    for(int i=2;i<=n;i++)
    {
        if(dp[i]==1) ans++;
        else if(!dp[i]) ans+=m;
    }
    printf("%d\n",ans);
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        init();
        input();
        solve();
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值