Network-POJ3694并查集+LCA

时间:2021-11-04 07:23:31
Network
Time Limit: 5000MS   Memory Limit: 65536K
     

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 integersN(1 ≤N ≤ 100,000) and
M(N - 1 ≤ M ≤ 200,000).

Each of the following M lines contains two integers A and B ( 1≤AB ≤ N), which indicates a link between computer
A andB. Computers are numbered from 1 toN. 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 ≤ ABN), 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) andQ lines, thei-th of which contains a integer indicating the number of bridges in the network after the firsti 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



题意:一个网络管理员管理一个网络,网络中的电脑直接或间接的相连接,管理员有Q次操作,每次向网络中建立一条新边,向管理员报告桥的个数。



思路:先将网络中的桥求出来,在求的过程中进行并查集缩点,在询问的时候,进行最朴素的LCA查找最近公共祖先,在求的过程中判断节点与父节点是不是在同一个集合中,如果不在同一个集合,说明是桥,则这个桥将不存在,将两个集合合并。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm> using namespace std; const int INF = 0x3f3f3f3f; const int Max = 110000; typedef struct Node
{
int v; int next;
}Line; Line Li[Max*4]; int Head[Max],top; int dfn[Max],low[Max],vis[Max]; int pre[Max],fa[Max]; int num; int n,m,Q; void AddEdge(int u,int v)
{
Li[top].v = v; Li[top].next = Head[u]; Head[u] = top++;
} int Find(int x)
{
return pre[x]==-1?x:pre[x]=Find(pre[x]);
} void Union(int x,int y)
{
int Fx = Find(x); int Fy = Find(y); if(Fx!=Fy)
{
pre[Fx]=Fy;
}
} void dfs(int fat,int u,int dep)
{
dfn[u]=low[u]=dep; fa[u]=fat; vis[u] = 1; for(int i=Head[u];i!=-1;i=Li[i].next)
{
if(Li[i].v!=fat&&vis[Li[i].v]==1)
{
low[u] = min(low[u],dfn[Li[i].v]);
}
if(vis[Li[i].v]==0)
{
dfs(u,Li[i].v,dep+1); low[u] = min(low[u],low[Li[i].v]); if(low[Li[i].v]<=dfn[u])//并查集缩点
{
Union(Li[i].v,u);
}
else
{
num++;
}
}
}
vis[u]=2;
} void Judge(int u)
{
int x=Find(u); int y=Find(fa[u]); if(x!=y)//同一集合,则集合合并
{
num--;
pre[x]=y;
}
} void LCA(int u,int v)//找公共祖先
{
while(dfn[u]>dfn[v])
{
Judge(u); u=fa[u];
}
while(dfn[v]>dfn[u])
{
Judge(v);
v=fa[v];
} while(u!=v)
{
Judge(u);
Judge(v);
u = fa[u];
v = fa[v];
}
}
int main()
{ int z=1;
while(~scanf("%d %d",&n,&m)&&(n+m))
{
top = 0; memset(Head,-1,sizeof(Head)); int u,v; for(int i=0;i<m;i++)
{
scanf("%d %d",&u,&v); AddEdge(u,v); AddEdge(v,u);
} num = 0 ; memset(vis,0,sizeof(vis)); memset(pre,-1,sizeof(pre)); dfs(0,1,1); scanf("%d",&Q); printf("Case %d:\n",z++); while(Q--)
{
scanf("%d %d",&u,&v); if(Find(u)!=Find(v))
{
LCA(u,v);
} printf("%d\n",num);
} printf("\n");
} return 0;
}