欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POJ 3694 Network DCC + LCA

程序员文章站 2024-03-22 18:25:10
...

一、内容

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

二、思路

POJ 3694 Network DCC + LCA

三、代码

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int N = 1e5 + 5, M = 6e5 + 5;
struct E{int v, next;} e[M];
struct Node {int fa, j;} f[N];//保存父亲 和通往父亲的那条边 
int n, m, q, u, v, dcc_cnt, id[N], len, dep[N], h[N], dh[N], num, dfn[N], low[N];
bool brid[M];  
void add(int h[], int u, int v) {e[++len].v = v; e[len].next = h[u]; h[u] = len;}
void tarjan(int u, int in_edge) {
	dfn[u] = low[u] = ++num;
	for (int j = h[u]; j; j = e[j].next) {
		int v = e[j].v;
		if (!dfn[v]) {
			tarjan(v, j);
			low[u] = min(low[u], low[v]);
			if (dfn[u] < low[v]) brid[j] = brid[j ^ 1] = true;
		} else if ((j ^ 1) != in_edge) low[u] = min(low[u], dfn[v]);
 	}
}
void dfs(int u) {
	id[u] = dcc_cnt;
	for (int j = h[u]; j; j = e[j].next) {
		int v = e[j].v;
		if (id[v] || brid[j]) continue; //若是桥不通过
		dfs(v);
	}
}
void bfs() {
	memset(dep, 0, sizeof(dep));
	dep[1] = 1; f[1].fa = 0;
	queue<int> q; q.push(1);
	while (!q.empty()) {
		int u = q.front(); q.pop();
		for (int j = dh[u]; j; j = e[j].next) {
			int v = e[j].v;
			if (dep[v]) continue;
			dep[v] = dep[u] + 1;
			q.push(v); f[v].fa = u; f[v].j = j; 
			//printf("%d==%d-%d\n", u, f[v].fa, f[v].j);
		}
	}
}
int LCA(int x, int y) {
	int cnt = 0;
	if (dep[y] >= dep[x]) swap(x, y);
	while (dep[x] > dep[y]) {
		if (brid[f[x].j]) cnt++, brid[f[x].j] = brid[f[x].j ^ 1] = false;
		x = f[x].fa;
	}
	if (x == y) return cnt;
	while (x != y) {
		int xj = f[x].j, yj = f[y].j;
		if (brid[xj]) cnt++, brid[xj] = brid[xj ^ 1] = false;
		if (brid[yj]) cnt++, brid[yj] = brid[yj ^ 1] = false;
		x = f[x].fa, y = f[y].fa;
	}
	return cnt;
}
int main() {
	int T = 1;
	while (scanf("%d%d", &n, &m), n) {
		memset(h, 0, sizeof(h)); len = num = 1;
		memset(dh, 0, sizeof(dh));
		memset(brid, false, sizeof(brid));
		memset(dfn, 0, sizeof(dfn));
		memset(id, 0, sizeof(id)); 
		for (int i = 1; i <= m; i++) {
			scanf("%d%d", &u, &v);
			add(h, u, v); add(h, v, u);
		}
		tarjan(1, 0);
		//进行 e-dcc 缩点
		dcc_cnt = 0;
		for (int i = 1; i <= n; i++) {
			if (!id[i]) {
				++dcc_cnt;
				dfs(i);
			} 
		} 
		//建立缩点后的树 
		int ans = 0;
		int t = len;
		for (int j = 2; j <= t; j += 2) {
			u = e[j].v, v = e[j ^ 1].v;
			if (id[u] == id[v]) continue;
			brid[len + 1] = brid[len + 2] = true; //这2条边都是桥 
			add(dh, id[u], id[v]); add(dh, id[v], id[u]); ans++; //有一条边代表有一个桥 
		}
		//以1为根节点遍历所有的节点求出f[] 
		bfs();
		scanf("%d", &q);
		printf("Case %d:\n", T++);
		while (q--) {
			scanf("%d%d", &u, &v);
			if (id[u] != id[v]) ans -= LCA(id[u], id[v]);
			printf("%d\n", ans);
		}
	}
	return 0;
}
相关标签: POJ LCA 连通性