现在的位置: 首页 > 综合 > 正文

POJ3694—-Network

2018年02月21日 ⁄ 综合 ⁄ 共 4101字 ⁄ 字号 评论关闭
Network
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 6989   Accepted: 2497

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

Source

先tarjan,缩点得到树,然后暴力LCA+并查集统计桥的数目

/*************************************************************************
    > File Name: tj5.cpp
    > Author: ALex
    > Mail: 405045132@qq.com 
    > Created Time: 2015年01月17日 星期六 13时17分51秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;

const int N = 110010;
const int M = 210010;

int head[N];
int head2[N];
int DFN[N];
int low[N];
int deep[N];
int father[N];
int block[N];
int pre[N];
int st[N];
bool instack[N];
int tot, tot2, top, ord, sccnum, icase;

struct node
{
	int next;
	int to;
	int id;
}edge[M << 2], edge2[M << 2];

void init ()
{
	memset (DFN, -1, sizeof(DFN));
	memset (head, -1, sizeof(head));
	memset (head2, -1, sizeof(head2));
	memset (low, 0, sizeof(low));
	memset (instack, 0, sizeof(instack));
	tot = tot2 = ord = sccnum = top = 0;
}

void addedge (int from, int to, int id)
{
	edge[tot].to = to;
	edge[tot].id = id;
	edge[tot].next = head[from];
	head[from] = tot++;
}

void addedge2 (int from, int to)
{
	edge2[tot2].to = to;
	edge2[tot2].next = head2[from];
	head2[from ] = tot2++;
}

void dfs (int u, int fa, int d)
{
	pre[u] = fa;
	deep[u] = d;
	for (int i = head2[u]; ~i; i = edge2[i].next)
	{
		int v = edge2[i].to;
		if (v == fa)
		{
			continue;
		}
		dfs (v, u, d + 1);
	}
}

int find (int x)
{
	if (x == father[x])
	{
		return x;
	}
	return father[x] = find(father[x]);
}

void tarjan (int u, int x)
{
	DFN[u] = low[u] = ++ord;
	instack[u] = 1;
	st[top++] = u;
	for (int i = head[u]; ~i; i = edge[i].next)
	{
		int v = edge[i].to;
		if (edge[i].id == x)
		{
			continue;
		}
		if (DFN[v] == -1)
		{
			tarjan (v, edge[i].id);
			low[u] = min(low[u], low[v]);
		}
		else if (instack[v])
		{
			low[u] = min(low[u], DFN[v]);
		}
	}
	int v;
	if (DFN[u] == low[u])
	{
		++sccnum;
		do
		{
			v = st[--top];
			instack[v] = 0;
			block[v] = sccnum;
		}while (v != u);
	}
}

int LCA (int a, int b)
{
	while (a != b)
	{
		if (deep[a] > deep[b])
		{
			a = pre[a];
		}
		else if (deep[a] < deep[b])
		{
			b = pre[b];
		}
		else
		{
			a = pre[a];
			b = pre[b];
		}
		a = find(a);
		b = find(b);
	}
	return a;
}

void solve (int n)
{
	tarjan (1, -1);
	for (int u = 1; u <= n; ++u)
	{
		for (int i = head[u]; ~i; i = edge[i].next)
		{
			int v = edge[i].to;
			if (block[u] == block[v])
			{
				continue;
			}
			addedge2 (block[u], block[v]);
		}
	}
	for (int i = 1; i <= sccnum; ++i)
	{
		father[i] = i;
	}
	int cnt = sccnum - 1;
	dfs(1, -1, 0);
	int q, a, b, lca;
	scanf("%d", &q);
	printf("Case %d:\n", icase++);
	while (q--)
	{
		scanf("%d%d", &a, &b);
		a = block[a];
		b = block[b];
		if (a == b)
		{
			printf("%d\n", cnt);
			continue;
		}
		a = find(a);
		b = find(b);
		lca = LCA (a, b);
		int x = 0;
		while (a != lca)
		{
			++x;
			father[a] = lca;
			a = pre[a];
			a = find(a);
		}
		while (b != lca)
		{
			++x;
			father[b] = lca;
			b = pre[b];
			b = find(b);
		}
		cnt -= x;
		printf("%d\n", cnt);
	}	
}

int main()
{
	int n, m;
	int u, v;
	icase = 1;
	while (~scanf("%d%d", &n, &m))
	{
		if (!n && !m)
		{
			break;
		}
		init();
		for (int i = 1; i <= m; ++i)
		{
			scanf("%d%d", &u, &v);
			addedge (u, v, i);
			addedge (v, u, i);
		}
		solve(n);
		printf("\n");
	}
	return 0;
}
	

抱歉!评论已关闭.