poj 3694 Network (tarjan+树链剖分)

Network
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 9299 Accepted: 3459

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

Source

[Submit]   [Go Back]   [Status]   [Discuss]


题解:tarjan+树链剖分

利用tarjan缩点,将无向图变成一棵树,然后在树上加一条边,其实就相当于将该边的两个端点之间路径上的所有桥都变成了非桥边。所以将树进行树链剖分,起初所以树上的边的初值都是1,然后将边权下放为点权,每次加入一条边只需要将路径上的1都变成0即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 400003
using namespace std;
int n,m,q,x[N],y[N],belong[N],ins[N],st[N],top,tot,cnt,sz,belong1[N];
int dfsn[N],low[N],point[N],next[N],v[N],tr[N],f[N],size[N],deep[N],son[N],pos[N];
void add(int x,int y)
{
	tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y;
	tot++; next[tot]=point[y]; point[y]=tot; v[tot]=x;
}
void tarjan(int x,int fa)
{
	st[++top]=x; ins[x]=1;
	dfsn[x]=low[x]=++sz;
	bool pd=false;
	for (int i=point[x];i;i=next[i])
	{
		if (v[i]==fa&&!pd){
			pd=true;
			continue;
		}
		if (!dfsn[v[i]]) tarjan(v[i],x),low[x]=min(low[x],low[v[i]]);
		else if (ins[v[i]]) low[x]=min(low[x],low[v[i]]);
	}
	if (dfsn[x]==low[x]) {
		++cnt; int j;
		do{
			j=st[top--];
			belong1[j]=cnt;
		}while (j!=x);
	}
}
void dfs(int x,int fa)
{
	size[x]=1; f[x]=fa; deep[x]=deep[fa]+1;
	for (int i=point[x];i;i=next[i]) {
		if (v[i]==fa) continue;
		dfs(v[i],x);
		size[x]+=size[v[i]];
		if (size[son[x]]<size[v[i]]) son[x]=v[i];
	}
}
void dfs1(int x,int chain)
{
	belong[x]=chain; pos[x]=++sz;
	if (!son[x]) return ;
	dfs1(son[x],chain);
	for (int i=point[x];i;i=next[i])
	 if (v[i]!=f[x]&&v[i]!=son[x]) dfs1(v[i],v[i]);
}
void update(int now)
{
	tr[now]=tr[now<<1]+tr[now<<1|1];
}
void build(int now,int l,int r)
{
	if (l==r) {
		tr[now]=1;
		return;
	}
	int mid=(l+r)/2;
	build(now<<1,l,mid); build(now<<1|1,mid+1,r);
	update(now);
}
void cover(int now,int l,int r,int ll,int rr)
{
	if (ll<=l&&r<=rr) {
		tr[now]=0;
		return;
	}
	int mid=(l+r)/2;
	if (ll<=mid) cover(now<<1,l,mid,ll,rr);
	if (rr>mid) cover(now<<1|1,mid+1,r,ll,rr);
	update(now);
}
void solve(int x,int y)
{
	while (belong[x]!=belong[y]) {
		if (deep[belong[x]]<deep[belong[y]]) swap(x,y);
		cover(1,1,cnt,pos[belong[x]],pos[x]);
		x=f[belong[x]];
	}
	if (deep[x]>deep[y]) swap(x,y);
	if (x==y) return;
	cover(1,1,cnt,pos[x]+1,pos[y]);
}
int main()
{
	freopen("a.in","r",stdin);
	int t=0;
	while (true) {
		t++; 
		scanf("%d%d",&n,&m);
		if (n==0&&m==0) break;
		printf("Case %d:\n",t);
		tot=0; sz=0; cnt=0; top=0;
		memset(point,0,sizeof(point));
		memset(next,0,sizeof(next));
		for (int i=1;i<=m;i++) {
			scanf("%d%d",&x[i],&y[i]);
			add(x[i],y[i]);
		}
		memset(ins,0,sizeof(ins));
		memset(dfsn,0,sizeof(dfsn));
		memset(son,0,sizeof(son));
		memset(size,0,sizeof(size));
		for (int i=1;i<=n;i++)
		 if (!dfsn[i]) tarjan(i,i);
		tot=0; 
		memset(point,0,sizeof(point));
		memset(next,0,sizeof(next));
		for (int i=1;i<=m;i++) {
			int t=belong1[x[i]]; int t1=belong1[y[i]];
			if (t!=t1) add(t,t1);
		}
		sz=0;
		dfs(1,0); dfs1(1,1);
		memset(tr,0,sizeof(tr));
		build(1,1,cnt);
		scanf("%d",&q);
		for (int i=1;i<=q;i++) {
			int x,y; scanf("%d%d",&x,&y);
			x=belong1[x]; y=belong1[y];
			solve(x,y);
			printf("%d\n",tr[1]-1);
		}
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值