您的位置:首页 > 其它

HDU 2460 Network 求桥(tarjan)+LCA

2013-11-16 10:18 246 查看
点击打开链接


Network

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 603    Accepted Submission(s): 119


Problem 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

2008 Asia Hefei Regional
Contest Online by USTC

 

Recommend

teddy   |   We have carefully selected several similar problems for you:  2459 2456 2462 2458 2463 

题意是说有n台电脑,m条线将它们连接起来,问当加入q次边之后,求每次加入之后还 有多少桥。
不得不说太坑了,在HDU交了7,8次,都RE了,各种解决RE的方法都不管用,到最后将G++换成C++交竟然奇迹般的过了。
此题一开始当然先将未添边时的桥的数目求出来。那么如果添加一条边,如果属于某个双联通分量,那么桥的数目没有减少,如果这两个顶点分别属于两个子图,那么桥的数量就会减少。所以先缩点,缩点之后就会形成一棵树,向该树添加一条边就会形成一个环,那么这个环上的边就是减少的桥的数目,那么我们现在的任务就是找出加边之后形成的环中的边,那么我们可以用暴力lca。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define M 101007
using namespace std;
int low[M],dfn[M],head[M],pre[M],isbridge[M];
int n,m,num,cnt,ans;
struct E
{
int v,next;
}edg[M*8];

void addedge(int u,int v)
{
edg[num].v=v;
edg[num].next=head[u];
head[u]=num++;
}

void tarjan(int u,int fa)
{
int flag=0;
dfn[u]=low[u]=++cnt;
for(int i=head[u];i!=-1;i=edg[i].next)
{
int v=edg[i].v;
if(v==fa&&!flag){flag=1;continue;}
if(!dfn[v])
{
pre[v]=u;
tarjan(v,u);
low[u]=min(low[u],low[v]);
if(low[v]>dfn[u])
{
isbridge[v]=1;
ans++;
}
}
else low[u]=min(low[u],dfn[v]);
}
}

void LCA(int u,int v)
{
while(dfn[u]>dfn[v])
{
if(isbridge[u])
{
ans--;
isbridge[u]=0;
}
u=pre[u];
}
while(dfn[v]>dfn[u])
{
if(isbridge[v])
{
ans--;
isbridge[v]=0;
}
v=pre[v];
}
while(u!=v)
{
if(isbridge[u]){ans--;isbridge[u]=0;}
if(isbridge[v]){ans--;isbridge[v]=0;}
u=pre[u];v=pre[v];
}
}

int main()
{
int textcase=1;
while(scanf("%d%d",&n,&m),n|m)
{
memset(dfn,0,sizeof(int)*(n+1));
memset(head,-1,sizeof(int)*(n+1));
memset(isbridge,0,sizeof(int)*(n+1));
for(int i=0;i<=n;i++)pre[i]=i;
num=cnt=ans=0;
int a,b,q;
while(m--)
{
scanf("%d%d",&a,&b);
addedge(a,b);
addedge(b,a);
}
printf("Case %d:\n",textcase++);
tarjan(1,-1);
scanf("%d",&q);
while(q--)
{
scanf("%d%d",&a,&b);
LCA(a,b);
printf("%d\n",ans);
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: