POJ 3694 Network
Network
64-bit integer IO format: %lld Java class name: Main
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: 2 0
Source
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 const int maxn = 100010; 6 struct arc { 7 int to,next; 8 arc(int x = 0,int y = -1) { 9 to = x; 10 next = y; 11 } 12 } e[maxn<<2]; 13 int head[maxn],low[maxn],dfn[maxn],p[maxn]; 14 int tot,ans,idx,n,m,q,uf[maxn]; 15 void add(int u,int v) { 16 e[tot] = arc(v,head[u]); 17 head[u] = tot++; 18 e[tot] = arc(u,head[v]); 19 head[v] = tot++; 20 } 21 int Find(int x) { 22 return uf[x] = x == uf[x]?x:Find(uf[x]); 23 } 24 bool Union(int x,int y) { 25 int fx = Find(x); 26 int fy = Find(y); 27 if(fx != fy) uf[fx] = fy; 28 return fx != fy; 29 } 30 void tarjan(int u,int fa) { 31 dfn[u] = low[u] = ++idx; 32 bool flag = true; 33 for(int i = head[u]; ~i; i = e[i].next) { 34 if(e[i].to == fa && flag) { 35 flag = false; 36 continue; 37 } 38 if(!dfn[e[i].to]) { 39 tarjan(e[i].to,u); 40 p[e[i].to] = u; 41 low[u] = min(low[u],low[e[i].to]); 42 if(low[e[i].to] > dfn[u]) ans++; 43 else Union(u,e[i].to); 44 } else if(e[i].to != fa) low[u] = min(low[u],dfn[e[i].to]); 45 } 46 } 47 void LCA(int x,int y){ 48 while(x != y){ 49 while(dfn[x] >= dfn[y] && x != y){ 50 ans -= Union(x,p[x]); 51 x = p[x]; 52 } 53 while(dfn[y] >= dfn[x] && x != y){ 54 ans -= Union(y,p[y]); 55 y = p[y]; 56 } 57 } 58 } 59 void init(){ 60 for(int i = 0; i < maxn; ++i){ 61 head[i] = -1; 62 low[i] = dfn[i] = 0; 63 p[i] = 0; 64 uf[i] = i; 65 } 66 ans = idx = tot = 0; 67 } 68 int main() { 69 int u,v,cs = 1; 70 while(scanf("%d %d",&n,&m),n||m){ 71 init(); 72 for(int i = 0; i < m; ++i){ 73 scanf("%d %d",&u,&v); 74 add(u,v); 75 } 76 tarjan(1,-1); 77 scanf("%d",&q); 78 printf("Case %d:\n",cs++); 79 while(q--){ 80 scanf("%d %d",&u,&v); 81 if(Find(u) != Find(v)) LCA(u,v); 82 printf("%d\n",ans); 83 } 84 puts(""); 85 } 86 return 0; 87 }