UESTC Training for Graph Theory——M、Network

来源:互联网 发布:阿里云客服抢班语录 编辑:程序博客网 时间:2024/05/18 04:52

Network

Time Limit: 5000 ms Memory Limit: 65536 kB Solved: 31 Tried: 177

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

In the first line of input there is an integer T, meaning there is T test cases.
For each test case,
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

For each test case, first output "Case #C: ", where C is the number of test case, from 1 to T. Then output a single integer — the number of ways to divide the network into at least two parts.

Sample Input

1
4 1
1 2
2 3
1 4
3 4

Sample Output

Case #1: 3

Source

POJ Monthly--2007.10.06, Yang Mu

/*算法思想:  给了一个数,还有一些边,要求我们删掉一条树边和一条添加的边,使得图不连通,问共有多少种方法  我们可以给原树中每条边增加一个标记,统计的是树中每条边被新添加的边覆盖的次数。例如   4---1---2       |       |       3  假设我们以 4 作为根节点,现在添加了一条边:2---3,那么 1--2 被覆盖一次,1--3 被覆盖一次,但  是 4---1 没有被覆盖。如果在添加一条边:4---3,那么1---4 和 1---3 的覆盖次数又要+1,我们对每  条边统计一下被覆盖的次数  1、覆盖次数为1:这条边 + 覆盖它的新边一起删去是一种删边方案。总的方案数 +1  2、覆盖次数为0:这条边 + 任何一条新边一起删去是一种覆盖方案。总的方案数 +m  计算覆盖的次数的时候,我们可以这样做:  从上面举的例子可以看见,边被覆盖的截止点是他们的最近公共祖先。那么用一个数组dp[i],表示i节点  被覆盖的次数,那么每次覆盖两条边就可以将他们的dp值增 +1,而他们的最近公共祖先的dp值 -2*/#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<cmath>#define N 400005using namespace std;int n,m;bool vs[N];int dp[N],fa[N];long long ans;int head1[N],head2[N],tot;struct data{    int en,next;} edge[N],newe[N];int find_set(int x){    if(x!=fa[x]) fa[x]=find_set(fa[x]);    return fa[x];}void add_edge(int st,int en)  //向原来树中添加边{    edge[tot].en=en;    edge[tot].next=head1[st];    head1[st]=tot++;}void add_newe(int st,int en)  //向原来的树中添加新的边,记录在另一个边集中{    newe[tot].en=en;    newe[tot].next=head2[st];    head2[st]=tot++;}void tarjan(int v)  //tarjan找最近公共祖先,并且沿路更新节点的dp值{    vs[v]=true;    for(int i=head2[v];i!=-1;i=newe[i].next)        if(vs[newe[i].en])            dp[find_set(newe[i].en)]-=2;  //最近公共祖先的dp值 -2    for(int i=head1[v];i!=-1;i=edge[i].next)        if(!vs[edge[i].en])        {            tarjan(edge[i].en);            fa[edge[i].en]=v;            dp[v]+=dp[edge[i].en];            if(dp[edge[i].en]==0) ans+=m;  //统计            if(dp[edge[i].en]==1) ans++;  //统计        }}int main(){    int t;    scanf("%d",&t);    for(int ca=1;ca<=t;ca++)    {        scanf("%d%d",&n,&m);        tot=1;        memset(head1,-1,sizeof(head1));        int a,b;        for(int i=1;i<n;i++)        {            scanf("%d%d",&a,&b);            add_edge(a,b);            add_edge(b,a);        }        memset(head2,-1,sizeof(head2));        memset(dp,0,sizeof(dp));        for(int i=1;i<=m;i++)        {            scanf("%d%d",&a,&b);            if(a!=b)            {                add_newe(a,b);                add_newe(b,a);                dp[a]++;                dp[b]++;            }        }        for(int i=0;i<=n;i++)            fa[i]=i;        ans=0;        memset(vs,0,sizeof(vs));        tarjan(1);        printf("Case #%d: %lld\n",ca,ans);    }    return 0;}


 

 

原创粉丝点击