HDU 2460 双联通图+LCA
Network
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 548 Accepted Submission(s): 105
Problem 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.
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.
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
Case 1:
1
0
Case 2:
2
0
题意:已知一个连通图,有多次询问,每次加边,然后查询加边后图中桥的个数。
解题思路:tarjin缩点,找出起始桥的个数,标记出dfn时间戳,以及每个节点的父亲,对于每次查询,找到两个节点的lca,把途中的桥都去掉,剩下的即为答案。
代码:
1 #include<iostream> 2 #include<string.h> 3 #include<algorithm> 4 #include<stdio.h> 5 #include<map> 6 #include<set> 7 #include<vector> 8 #include<queue> 9 #include<stack> 10 using namespace std; 11 #pragma comment(linker,"/STACK:20140000,10240000") 12 int head[200000],tol,low[200000],dfn[200000],fa[200000],bridge,indexx,top,Stack[200000],isbridge[200000],instack[200000]; 13 struct node 14 { 15 int next,to; 16 }edge[500000]; 17 void add(int u,int v) 18 { 19 edge[tol].to=v; 20 edge[tol].next=head[u]; 21 head[u]=tol++; 22 } 23 void tarjin(int u,int pre) 24 { 25 low[u]=dfn[u]=++indexx; 26 Stack[top++]=u; 27 int i,v; 28 for(i=head[u];i!=-1;i=edge[i].next) 29 { 30 v=edge[i].to; 31 if(v==pre)continue; 32 if(!dfn[v]) 33 { 34 fa[v]=u; 35 tarjin(v,u); 36 if(low[u]>low[v])low[u]=low[v]; 37 if(low[v]>dfn[u]) 38 { 39 isbridge[v]=1; 40 bridge++; 41 } 42 } 43 else if(low[u]>dfn[v])low[u]=dfn[v]; 44 } 45 top--; 46 } 47 void lca(int u,int v) 48 { 49 while(dfn[u]>dfn[v]) 50 { 51 if(isbridge[u]) 52 { 53 bridge--; 54 isbridge[u]=0; 55 } 56 u=fa[u]; 57 } 58 while(dfn[v]>dfn[u]) 59 { 60 if(isbridge[v]) 61 { 62 bridge--; 63 isbridge[v]=0; 64 } 65 v=fa[v]; 66 } 67 while(u!=v) 68 { 69 if(isbridge[u]) 70 { 71 isbridge[u]=0; 72 bridge--; 73 } 74 if(isbridge[v]) 75 { 76 isbridge[v]=0; 77 bridge--; 78 } 79 u=fa[u];v=fa[v]; 80 } 81 } 82 int main() 83 { 84 int i,j,k,m,n,T=0; 85 while(~scanf("%d%d",&n,&m)) 86 { 87 if(m==0&&n==0)break; 88 memset(head,-1,sizeof(head));tol=0; 89 while(m--) 90 { 91 scanf("%d%d",&i,&j); 92 add(i,j); 93 add(j,i); 94 } 95 for(i=0;i<=n+1;i++)fa[i]=i; 96 memset(isbridge,0,sizeof(isbridge)); 97 memset(low,0,sizeof(low)); 98 memset(dfn,0,sizeof(dfn)); 99 memset(instack,0,sizeof(instack)); 100 indexx=top=bridge=0; 101 tarjin(1,1); 102 scanf("%d",&m); 103 printf("Case %d:\n",++T); 104 while(m--) 105 { 106 scanf("%d%d",&i,&j); 107 lca(i,j); 108 printf("%d\n",bridge); 109 } 110 puts(""); 111 } 112 return 0; 113 }

浙公网安备 33010602011771号