#POJ 3694 Network (无向图缩点 + LCA)

19 篇文章 0 订阅

Network

Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 14538 Accepted: 5260

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≤ AB ≤ 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 ≤ 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) 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

题目大意 : 输入一个无向图, Q次询问, 每次添加一条边,对于每次询问, 输出图中的割边数目

思路 : q为1000, 每次都找割边铁超, 但是如果把图转换成一棵树, 就好解决了, 树的每条边一定是割边, 所以先缩点, 然后存一下新图, 新图一定是一颗树, 再以 1 为默认祖先节点, 深度搜索, 把每个点的父亲节点和深度都表示出来, 询问时, 找两个点的LCA, 找的过程中, 更新过程中的每条边已经走过(树的话点可以用来表示边), 最后输出割边的数量就可以了。其实这样算是比较暴力的,要是优化的话可以用并查集维护一下

Accepted code

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
#include<stack>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & (-x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 5e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }

struct Edge
{
	int v, f, next;
}e[MAXN << 1], edge[MAXN << 1]; // 左为原图, 右为新图
int head[MAXN], pre[MAXN], n, m, cnt, tot, X; 
int p[MAXN], dfn[MAXN], low[MAXN], q, ans;  // p为父亲
int suo[MAXN], sum[MAXN], dep[MAXN], scnt, stot;  // suo为缩点后的点, sum为边走过次数
stack <int> st;   // dep为深度
void init() {
	MEM(head, -1); MEM(dfn, 0); MEM(low, 0);
	MEM(pre, -1); MEM(dep, 0); MEM(suo, 0);
	cnt = scnt = tot = stot = 0;
}
void add(int from, int to) {
	e[cnt].v = to;
	e[cnt].next = head[from];
	head[from] = cnt++;
}
void Add(int from, int to) {
	edge[++stot].v = to;
	edge[stot].next = pre[from];
	pre[from] = stot;
}
void tarjan(int x) {
	dfn[x] = low[x] = ++tot;
	st.push(x);
	for (int i = head[x]; i != -1; i = e[i].next) {
		int vi = e[i].v;
		if (e[i].f) continue;
		e[i].f = e[i ^ 1].f = 1;
		if (!dfn[vi]) {
			tarjan(vi);
			low[x] = min(low[x], low[vi]);
		}
		else low[x] = min(low[x], dfn[vi]);
	}
	if (dfn[x] == low[x]) {
		scnt++;
		int k;
		do {
			k = st.top();
			st.pop();
			suo[k] = scnt;
		} while (k != x);
	}
}
void dfs(int x, int fa) {
	dep[x] = dep[fa] + 1; p[x] = fa;
	for (int i = pre[x]; i != -1; i = edge[i].next) {
		int vi = edge[i].v;
			if (vi != fa) dfs(vi, x);  // 更新深度和父亲
	}
}
void LCA(int x, int y) {
	if (dep[x] > dep[y]) swap(x, y);
	while (dep[y] - 1 >= dep[x]) {
		if (sum[y] == 1) ans--, sum[y]++;  // 先放到同一深度
		y = p[y];
	}
	if (x == y) return;
	while (x != y) {
		if (sum[x] == 1) ans--, sum[x]++;  // 有必要的话一起往上跳
		if (sum[y] == 1) ans--, sum[y]++;
		x = p[x], y = p[y];
	}
}

int main()
{
	while (~sc("%d %d", &n, &m) && n + m) {
		init(); 
		for (int i = 0; i < m; i++) {
			int ui, vi;
			sc("%d %d", &ui, &vi);
			add(ui, vi); add(vi, ui);
		}
		for (int i = 1; i <= n; i++) {
			if (!dfn[i]) tarjan(i);
		}
		for (int i = 1; i <= n; i++) {
			sum[i] = 1;
			for (int j = head[i]; j != -1; j = e[j].next) {
				int ui = suo[i], vi = suo[e[j].v];
				if (ui != vi) Add(ui, vi);
			}
		}
		ans = scnt - 1; dfs(1, 1);
		cin >> q;
		cout << "Case " << ++X << ":" << endl;
		while (q--) {
			int ui, vi;
			sc("%d %d", &ui, &vi);
			LCA(suo[ui], suo[vi]);
			cout << max(ans, 0) << endl;
		}
		cout << endl;
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值