Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 9462 | Accepted: 3517 |
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 2 1 2 2 3 2 1 2 1 3 4 4 1 2 2 1 2 3 1 4 2 1 2 3 4 0 0
Sample Output
Case 1: 1 0 Case 2: 20
题意:
给出一个N(1 ≤ N ≤ 100,000)个点 和 M(N - 1 ≤ M ≤ 200,000)的连通图.
有Q ( 1 ≤ Q ≤ 1,000)个询问 每次询问增加一条边(累加下去)
输出每增加一条边后剩下的桥的数量
题解:
10W点加1000次询问 每次询问都用Tarjin算法求一次肯定会超时的
考虑 每次加一条边a-b的实质:
从a到b的路径中的所有割边都将消失
那如何记录这条路径呢
在Tarjan算法递归的过程中通过树边建立一棵树
然后每次询问分别从a b开始 一直到它们的最近公共祖先 出现的割边全部消失即可
#include <stdio.h> #include <string.h> #define min(a,b) a>b?b:a #define Max 100050 struct Edge { int to; int next; }edge[Max*4]; int head[Max],tol; void add( int u,int v)//邻接表 { edge[tol].to = v; edge[tol].next = head[u]; head[u] = tol++; } int dfn[Max],low[Max],vis[Max],bcnt,time; int flag[Max];//存割边 int deep[Max];//存深度 int pre[Max];//存祖先节点 int ans;//计数割边 void tarjan(int u,int fa)//求割边 { dfn[u] = low[u] = ++time; deep[u] = deep[fa]+1; for( int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if(!dfn[v]) { pre[v] = u; tarjan(v,u); low[u] = min(low[u], low[v]); if(dfn[u] < low[v]) { flag[v] = 1;//统计割边,并计数 ans++; } } else if( v != fa) { low[u] = min(low[u], dfn[v]); } } } void lca(int a,int b) { while(deep[a] > deep[b])//如果深度a,比b的深度深,则a向上返回,直到与b同级 { if(flag[a]) { flag[a] = 0; ans--; } a = pre[a]; } while(deep[a] < deep[b])//b同a { if(flag[b]) { flag[b] = 0; ans--; } b = pre[b]; } while(a != b)//如果a,b同级则找他们的公共祖先,找到公共祖先的过程中,去掉所有割边 { if(flag[a]) { flag[a]=0; ans--; } a= pre[a]; if(flag[b]) { flag[b] = 0; ans--; } b=pre[b]; } } int main() { int m,n,u,v; int cnt = 1; while(~scanf("%d%d",&n,&m)) { if(!n && !m) break; memset(head, -1, sizeof(head)); tol = 0; for( int i = 0; i < m; i++) { scanf("%d%d",&u,&v); add(u, v); add(v, u); } memset(dfn, 0, sizeof(dfn)); memset(low, 0, sizeof(low)); memset(deep,0, sizeof(deep)); memset(flag,0,sizeof(flag)); for( int i = 1; i <= n; i++) pre[i] = i; ans = 0; time = 0; tarjan(1,0); //for( int i = 1; i <= n; i++) // printf("%d : %d\n",i,pre[i]); int q; scanf("%d",&q); printf("Case %d:\n",cnt++); while(q--) { scanf("%d%d",&u,&v); if(ans != 0) lca(u,v); printf("%d\n",ans); } printf("\n"); } return 0; }