POJ3694--Network

来源:互联网 发布:医用弹力绷带淘宝 编辑:程序博客网 时间:2024/04/25 20:46

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 integers N(1 ≤ N ≤ 100,000) and M(N - 1 ≤ M ≤ 200,000).
Each of the following M lines contains two integers A and B ( 1≤ A  B ≤ N), which indicates a link between computer A and B. 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 ≤ A  B  N), 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) and Q lines, the i-th of which contains a integer indicating the number of bridges in the network after the first i 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
/*    不错一道题    题意:给出一幅图,里面有桥,给出Q个操作,    每个操作会添加一条边,问每次操作后图中桥的个数    每次求桥会太慢    考虑一下dfs树,树边肯定是桥,然后每连上x,y,就会形成一个环,这个环内的边就全部都不是割边    所以只要找到x,y的lca,把这个路径上的桥标记为否即可 图连通 */#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define maxn 400080int first[maxn],nxt[maxn],vv[maxn],f[maxn],vis[maxn],dfn[maxn],low[maxn];bool isbridge[maxn];int e,cnt,bridgenum;void addEdge(int u,int v){ vv[e] = v; nxt[e] = first[u]; first[u] = e++; vv[e] = u; nxt[e] = first[v];  first[v] = e++;}int find(int x){ if(x == f[x]) return x; return f[x] = find(f[x]);}void Union(int x,int y){ int fx = find(x); f[y] = fx;}void Tarjan(int u,int fa){ f[u] = u; vis[u] = 1; dfn[u] = low[u] = ++cnt; for(int i = first[u];i != -1;i = nxt[i]) {  int v = vv[i];  if(vis[v] == 1 && v != fa)  {   low[u] = min(low[u],dfn[v]);  }  if(vis[v] == 0)  {   Tarjan(v,u);   Union(u,v);   low[u] = min(low[u],low[v]);   if(low[v] > dfn[u])     {    isbridge[v] = 1;    bridgenum++;   }  } } vis[u] = 2;}void lca(int x,int y){ if(dfn[x] < dfn[y]) swap(x,y); while(dfn[x] > dfn[y]) {  if(isbridge[x])  {   bridgenum--;   isbridge[x] = 0;  }  x = f[x]; } while(x != y) {  if(isbridge[y]) {bridgenum--; isbridge[y] = 0;}  y = f[y]; }}int main(){ //freopen("in.txt","r",stdin); int n,m; int cas = 0; while(scanf("%d%d",&n,&m)==2 &&(n||m)) {  cas++;  memset(vis,0,sizeof(vis));  memset(dfn,0,sizeof(dfn));  memset(first,-1,sizeof(first));  memset(isbridge,0,sizeof(isbridge));  e = cnt = bridgenum = 0;  for(int i = 1;i <= m;i++)  {   int u,v;   scanf("%d%d",&u,&v);   addEdge(u,v);  }  Tarjan(1,-1);  printf("Case %d:\n",cas);  int q; scanf("%d",&q);  for(int i = 1;i <= q;i++)  {   int u,v;   scanf("%d%d",&u,&v);   lca(u,v);   printf("%d\n",bridgenum);  }  printf("\n"); } return 0;}