Network POJ - 3694(边双连通分量, LCA)

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

比如以下一副图,一开始各点的dfn值为圈中的值,low值为旁边绿色的值。
Network POJ - 3694(边双连通分量, LCA)_第1张图片
从7连到3时,开始回溯,更新low值
Network POJ - 3694(边双连通分量, LCA)_第2张图片
下图中紫色的点都被标记为1,记录了桥,如果查询图中橙色的两个点x, y,左边的点x的dfn值深,一步步找它的父节点,更新x=f[x], 当x, y的深度一样后,一起往上找父节点,由于下图中,a[x], a[y]都等于1,所以桥的数量减去了2,x=f[x],y=f[y],此时x=y,找到LCA

Network POJ - 3694(边双连通分量, LCA)_第3张图片

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long 
#define mem(a, b) memset(a, b, sizeof(a))
#define N 500010 
#define MOD
using namespace std;

int dfn[N], low[N], num;
int n, m, Q, ans; 
int f[N];
int a[N];
int ver[N*2], Next[2*N], head[N], tot;

void add(int x, int y) {//邻接表 
	ver[++tot] = y, Next[tot] = head[x], head[x] = tot;
} 

void init() {
	tot = 0, num = 0, ans = 0;
	mem(head, -1);
	mem(low, 0);
	mem(dfn, 0);
	for(int i = 1; i<=n; i++) {
		f[i] = i;
	} 
	mem(a, 0);
}

void tarjan(int x, int root) { 
	low[x] = ++num;
	dfn[x] = dfn[root] + 1;
	for (int i = head[x]; i!=-1; i=Next[i]) {
		int y=ver[i];
		if(!dfn[y]) {
			f[y] = x;
			tarjan(y, x);
			low[x] = min(low[x], low[y]);
			if(low[y] > dfn[x]) {
				ans++;
				a[y] = 1;
			}
		}
		else if(root != y)
			low[x] = min(low[x], dfn[y]);
	}
}

void lca(int x, int y) {
	while(dfn[x] < dfn[y]) {
		if(a[y]) {
			ans--;
			a[y] = 0;
		}
		y = f[y];
	}
	while(dfn[x] > dfn[y]) {
		if(a[x]) {
			ans--;
			a[x] = 0;
		}
		x = f[x];
	}
	while(x != y) {
		if(a[x]) {
			ans--;
			a[x] = 0;
		}
		if(a[y]) {
			ans--;
			a[y] = 0;
		}
		x=f[x], y=f[y];
	}
}



int main() {
	int iCase = 0;
	while(scanf("%d%d", &n, &m)!=EOF) {
		if(n == 0 && m == 0) break;
		init();
		int x, y;
		for(int i = 1; i <= m; i++) {
			
			scanf("%d%d", &x, &y);
			add(x, y), add(y, x);
		}
		tarjan(1,0);		
		scanf("%d", &Q);
		printf("Case %d:\n",++iCase);
		for(int i = 1; i <= Q; i++) {
			scanf("%d%d",&x, &y);
			lca(x, y);
			printf("%d\n",ans);
		}
	}
}

你可能感兴趣的:(Network POJ - 3694(边双连通分量, LCA))