Network
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 7298 | Accepted: 2651 |
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: 2 0
Source
2008 Asia Hefei Regional Contest Online by USTC
题目意思:
n个节点,m条无向边的图。然后q个操作,每个操作连接u、v点,然后输出连接后图中剩余桥的个数。
思路:
先tarjan标记一下桥,然后lca把路径上所有桥去掉标记。
代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <vector> 6 #include <queue> 7 #include <cmath> 8 #include <set> 9 #include <map> 10 #include <stack> 11 using namespace std; 12 13 #define N 100005 14 15 int n, m; 16 int dfn[N], low[N], Time; 17 int dep[N]; 18 bool brg[N]; 19 int father[N]; 20 int brg_num; 21 22 struct Edge{ 23 int u, v, next; 24 }e[400005]; 25 26 int cnt; 27 int head[N]; 28 29 void setEdge(int u,int v){ 30 e[cnt].u=u;e[cnt].v=v; 31 e[cnt].next=head[u];head[u]=cnt++; 32 } 33 34 void tarjan(int u,int fa){ 35 int i, v; 36 dfn[u]=low[u]=Time++; 37 for(i=head[u];i!=-1;i=e[i].next){ 38 v=e[i].v; 39 if(v==fa) continue; 40 if(!dfn[v]){ 41 dep[v]=dep[u]+1; 42 father[v]=u; 43 tarjan(v,u); 44 if(low[v]>dfn[u]){ 45 brg_num++; 46 brg[v]=true; 47 } 48 low[u]=min(low[u],low[v]); 49 } 50 else low[u]=min(dfn[v],low[u]); 51 } 52 } 53 54 void lca(int u,int v){ 55 while(dep[u]>dep[v]){ 56 if(brg[u]) { 57 brg[u]=false,brg_num--; 58 } 59 u=father[u]; 60 } 61 while(dep[v]>dep[u]){ 62 if(brg[v]) { 63 brg[v]=false,brg_num--; 64 } 65 v=father[v]; 66 } 67 while(u!=v){ 68 if(brg[u]) { 69 brg[u]=false,brg_num--; 70 } 71 u=father[u]; 72 if(brg[v]) { 73 brg[v]=false,brg_num--; 74 } 75 v=father[v]; 76 } 77 } 78 79 main() 80 { 81 int i, j, k; 82 int u, v; 83 int kase=1; 84 while(scanf("%d %d",&n,&m)==2){ 85 if(!n&&!m) break; 86 memset(head,-1,sizeof(head)); 87 cnt=0; 88 for(i=0;i<m;i++){ 89 scanf("%d %d",&u,&v); 90 setEdge(u,v); 91 setEdge(v,u); 92 } 93 Time=1; 94 memset(dfn,0,sizeof(dfn)); 95 memset(brg,false,sizeof(brg)); 96 brg_num=0; 97 for(i=1;i<=n;i++){ 98 if(!dfn[i]){ 99 dep[i]=0; 100 tarjan(i,i); 101 } 102 } 103 printf("Case %d:\n",kase++); 104 int q; 105 scanf("%d",&q); 106 for(i=0;i<q;i++){ 107 scanf("%d %d",&u,&v); 108 lca(u,v); 109 printf("%d\n",brg_num); 110 } 111 cout<<endl; 112 } 113 }