POJ 3694 Network

来源:互联网 发布:淘宝大牌原单正品店 编辑:程序博客网 时间:2024/05/21 12:06

Description

A network administrator manages a large network. The network consists of N computers and M links between pairs of computers. Any pair of computers are connected directly or indirectly by successive links, so data can be transformed between any two computers. The administrator finds that some links are vital to the network, because failure of any one of them can cause that data can't be transformed between some computers. He call such a link a bridge. He is planning to add some new links one by one to eliminate all bridges.

You are to help the administrator by reporting the number of bridges in the network after each new link is added.

Input

The input consists of multiple test cases. Each test case starts with a line containing two integersN(1 ≤ N ≤ 100,000) and M(N - 1 ≤ M ≤ 200,000).
Each of the following M lines contains two integers A and B ( 1≤AB ≤ N), which indicates a link between computer A andB. Computers are numbered from 1 to N. It is guaranteed that any two computers are connected in the initial network.
The next line contains a single integer Q ( 1 ≤ Q ≤ 1,000), which is the number of new links the administrator plans to add to the network one by one.
The i-th line of the following Q lines contains two integer A and B (1 ≤ ABN), which is the i-th added new link connecting computer A and B.

The last test case is followed by a line containing two zeros.

Output

For each test case, print a line containing the test case number( beginning with 1) andQ lines, the i-th of which contains a integer indicating the number of bridges in the network after the firsti new links are added. Print a blank line after the output for each test case.

Sample Input

3 21 22 321 21 34 41 22 12 31 421 23 40 0

Sample Output

Case 1:10Case 2:20

Source

2008 Asia Hefei Regional Contest Online by USTC

最近,tarjan学得比较迷,上网找了了题。
题目大意:给定一个无向连通图,每次加一些边,询问每次加边之后桥的数量。
所以先跑一遍tarjan,求出桥的数量,每次加边就相当于寻找这两个点的lca,把路径上的桥都减去,中间缩点用到了并查集。

#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>using namespace std;const int N=100005;const int M=200005;int n,m,q,tim,cnt,ans,cas,hd[N],dfn[N],low[N],f[N],pre[N];struct edge{int to,nxt;}v[2*M];void addedge(int x,int y){++cnt;v[cnt].to=y;v[cnt].nxt=hd[x];hd[x]=cnt;}int fnd(int x){if(f[x]!=x)f[x]=fnd(f[x]);return f[x];}void tarjan(int u,int fa){dfn[u]=low[u]=++tim;for(int i=hd[u];i;i=v[i].nxt)if(!dfn[v[i].to]){pre[v[i].to]=u;tarjan(v[i].to,u);low[u]=min(low[u],low[v[i].to]);if(low[v[i].to]>dfn[u])ans++;elsef[fnd(v[i].to)]=fnd(u);}else if(v[i].to!=fa)low[u]=min(low[u],dfn[v[i].to]);}void lca(int x,int y){while(dfn[y]<dfn[x])swap(x,y);while(dfn[y]>dfn[x]){if(fnd(y)!=fnd(pre[y])){ans--;f[fnd(y)]=fnd(pre[y]);}y=pre[y];}while(x!=y){if(fnd(x)!=fnd(pre[x])){ans--;f[fnd(x)]=fnd(pre[x]);}x=pre[x];}}int main(){while(scanf("%d%d",&n,&m)&&n+m>0){printf("Case %d:\n",++cas);ans=0,tim=0,cnt=0;memset(v,0,sizeof(v));memset(pre,0,sizeof(pre));memset(hd,0,sizeof(hd));for(int i=1;i<=n;i++)f[i]=i;while(m--){int x,y;scanf("%d%d",&x,&y);addedge(x,y);addedge(y,x);}for(int i=1;i<=n;i++)if(!dfn[i])tarjan(i,0);scanf("%d",&q);while(q--){int x,y;scanf("%d%d",&x,&y);lca(x,y);printf("%d\n",ans);}printf("\n");}return 0;}

1 0
原创粉丝点击