POJ - 3694 Network (无向图 并查集缩点+割边+朴素求lca)

Network
Time Limit: 5000MS         Memory Limit: 65536K
Total Submissions: 9714         Accepted: 3603
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
 

参考:

题意:https://blog.csdn.net/bbbbswbq/article/details/78028224

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

分析:

思路:边连通分量+LCA。

首先运行一次tarjan,求出桥和缩点,缩点用并查集缩一下,那么无向图将缩点为一棵树,树边正好是原来的桥。每次连接两点,看看这两点是不是在同一个缩点内,如果是,那么缩点后的树没任何变化,如果两点属于不同的缩点,那么连接起来,然后找这两个缩点的LCA,因为从点u到LCA再到点v再到点u,将形成环,里面的树边都会变成不是桥。计数的时候注意

///tarjan算法求无向图(可有重边)的桥、边双连通分量并缩点
#include
#include
#include
#include
#include
using namespace std;
const int SIZE = 1000010;
int head[SIZE], ver[SIZE * 2], Next[SIZE * 2];
///head[i]=x表示以i起点的数组下标,ver[x]表示以i起点的终点编号
///next[x]=y下一个表示以i起点的数组下标 ,ver[y]表示另一个以i起点的终点编号
int dfn[SIZE], low[SIZE], pre[SIZE];
///dfn表示时间戳
///low表示追溯值,c[x]表示结点x属于边连通分量的编号
int n, m, tot, num,nbridge,f[SIZE];  
bool bridge[SIZE * 2]; ///是否是桥
void add(int x, int y) {
	ver[++tot] = y, Next[tot] = head[x], head[x] = tot;
}
 
void init()
{
	tot=1;
	num=0;
	nbridge=0;
	for(int i=0;i<=n;i++) f[i]=i;
	memset(dfn,0,sizeof(dfn));
	memset(pre,0,sizeof(pre));
	memset(low,0,sizeof(low));
	memset(ver,0,sizeof(ver));
	memset(head,0,sizeof(head));
}
int Find(int x)
{
    return x==f[x]?x:(f[x]=Find(f[x]));
}
 
void Union(int a,int b)
{
    int x=Find(a),y=Find(b);
    if(x!=y) f[x]=y;
}
void tarjan(int x, int in_edge) {
	dfn[x] = low[x] = ++num;
	for (int i = head[x]; i; i = Next[i]) {///遍历每一个结点
		int y = ver[i];
		if (!dfn[y]) {
			pre[y]=x;
			tarjan(y, i);
			low[x] = min(low[x], low[y]);
			if (low[y]<=dfn[x]) Union(x,y);
			if (low[y] > dfn[x]) ///找到桥
			{
				bridge[i] = bridge[i ^ 1] = true;
				nbridge++;
			}

		}
		else if (i != (in_edge ^ 1)) ///利用异或性质解决重边
                                     ///防止x结点到父亲结点
			low[x] = min(low[x], dfn[y]);
	}
}

int lca(int x,int y)  ///朴素求lca
{
    if(dfn[x]dfn[y])
    {
        if(Find(x)!=Find(pre[x]))
          nbridge--,f[Find(x)]=Find(pre[x]);
        x=pre[x];
    }
    while(y!=x)
    {
        if(Find(y)!=Find(pre[y]))
          nbridge--,f[Find(y)]=Find(pre[y]);
        y=pre[y];
    }
    return nbridge;
}
int main() {
	int cas=1;
    while(~scanf("%d%d",&n,&m)&&(n+m))
    {
    	
        printf("Case %d:\n",cas++);
        init();
        for(int i=0; i

 

你可能感兴趣的:(POJ - 3694 Network (无向图 并查集缩点+割边+朴素求lca))