您的位置:首页 > 其它

POJ 3694 tarjan 桥+lca

2015-07-14 21:39 363 查看
Network

Time Limit: 5000MSMemory Limit: 65536K
Total Submissions: 7298Accepted: 2651
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

题目意思:
n个节点,m条无向边的图。然后q个操作,每个操作连接u、v点,然后输出连接后图中剩余桥的个数。

思路:
先tarjan标记一下桥,然后lca把路径上所有桥去掉标记。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#include <stack>
using namespace std;

#define N 100005

int n, m;
int dfn
, low
, Time;
int dep
;
bool brg
;
int father
;
int brg_num;

struct Edge{
int u, v, next;
}e[400005];

int cnt;
int head
;

void setEdge(int u,int v){
e[cnt].u=u;e[cnt].v=v;
e[cnt].next=head[u];head[u]=cnt++;
}

void tarjan(int u,int fa){
int i, v;
dfn[u]=low[u]=Time++;
for(i=head[u];i!=-1;i=e[i].next){
v=e[i].v;
if(v==fa) continue;
if(!dfn[v]){
dep[v]=dep[u]+1;
father[v]=u;
tarjan(v,u);
if(low[v]>dfn[u]){
brg_num++;
brg[v]=true;
}
low[u]=min(low[u],low[v]);
}
else low[u]=min(dfn[v],low[u]);
}
}

void lca(int u,int v){
while(dep[u]>dep[v]){
if(brg[u]) {
brg[u]=false,brg_num--;
}
u=father[u];
}
while(dep[v]>dep[u]){
if(brg[v]) {
brg[v]=false,brg_num--;
}
v=father[v];
}
while(u!=v){
if(brg[u]) {
brg[u]=false,brg_num--;
}
u=father[u];
if(brg[v]) {
brg[v]=false,brg_num--;
}
v=father[v];
}
}

main()
{
int i, j, k;
int u, v;
int kase=1;
while(scanf("%d %d",&n,&m)==2){
if(!n&&!m) break;
memset(head,-1,sizeof(head));
cnt=0;
for(i=0;i<m;i++){
scanf("%d %d",&u,&v);
setEdge(u,v);
setEdge(v,u);
}
Time=1;
memset(dfn,0,sizeof(dfn));
memset(brg,false,sizeof(brg));
brg_num=0;
for(i=1;i<=n;i++){
if(!dfn[i]){
dep[i]=0;
tarjan(i,i);
}
}
printf("Case %d:\n",kase++);
int q;
scanf("%d",&q);
for(i=0;i<q;i++){
scanf("%d %d",&u,&v);
lca(u,v);
printf("%d\n",brg_num);
}
cout<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: