http://poj.org/problem?id=3694
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
题目大意:给出一张
n
n
n个点
m
m
m条边的无向连通图,然后执行
Q
Q
Q次操作,每次向图中添加一条边,并且询问当前无向图中"桥"的数量。
思路:好像这题数据挺水的,不用缩点直接在原图的搜索树中暴力找LCA就可以。首先利用
t
a
r
j
a
n
tarjan
tarjan算法找到图中的桥并打上标记,然后跑一遍
d
f
s
dfs
dfs处理出边双连通分量。设得到了
d
c
c
dcc
dcc个边双连通分量,那么此时桥的个数就是
d
c
c
−
1
dcc-1
dcc−1个,同时每个点都有一个编号
i
d
[
i
]
id[i]
id[i]记录该点是在哪个边双联通分量中。对于每条添加的边
(
u
,
v
)
(u,v)
(u,v),如果
i
d
[
u
]
=
i
d
[
v
]
id[u]=id[v]
id[u]=id[v],直接输出
d
c
c
−
1
dcc-1
dcc−1即可;否则搜索树中从
u
u
u到
v
v
v的路径中的桥就要减掉,此时我们可以暴力找
L
C
A
LCA
LCA同时给经过的桥打上标记(因为不好给边打标记 所以代码中的方法是给桥的深度大的那个节点打上标记)。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1e5+5;
const int maxm=2e5+5;
struct edge
{
int to,nxt;
}Edge[maxm<<1];
bool bridge[maxm<<1];
bool vis[maxn];//标记哪些桥已经减过了
int head[maxn],dfn[maxn],low[maxn],deep[maxn],fa[maxn];
int n,m,tot,num;
inline void addedge(int x,int y)//存图
{
Edge[++tot].to=y,Edge[tot].nxt=head[x],head[x]=tot;
}
void tarjan(int x,int in_edge)
{
int y;
dfn[x]=low[x]=++num;
for(int i=head[x];i;i=Edge[i].nxt)
{
y=Edge[i].to;
if(!dfn[y])//未访问过的节点
{
deep[y]=deep[x]+1;
fa[y]=x;
tarjan(y,i);
low[x]=min(low[x],low[y]);
if(low[y]>dfn[x]) //桥
bridge[i]=bridge[i^1]=1;
}
else if(i!=(in_edge^1))//别忘了括号
low[x]=min(low[x],dfn[y]);
}
}
int id[maxn],dcc;//dcc个边双连通分量
void dfs(int x)
{
int y;
id[x]=dcc;
for(int i=head[x];i;i=Edge[i].nxt)
{
y=Edge[i].to;
if(id[y]||bridge[i])
continue;
dfs(y);
}
}
void lcawork(int u,int v)
{
if(deep[u]<deep[v])
swap(u,v);
while(deep[u]>deep[v])
{
if(id[u]!=id[fa[u]]&&!vis[id[u]])//这个桥还没有减掉
--dcc,vis[id[u]]=1;
u=fa[u];
}
while(u!=v)
{
if(id[u]!=id[fa[u]]&&!vis[id[u]])
--dcc,vis[id[u]]=1;
if(id[v]!=id[fa[v]]&&!vis[id[v]])
--dcc,vis[id[v]]=1;
u=fa[u],v=fa[v];
}
}
int main()
{
int times=0;
while(~scanf("%d%d",&n,&m)&&n&&m)
{
int x,y;
tot=1;
memset(head,0,sizeof(head));
memset(bridge,0,sizeof(bridge));
memset(vis,0,sizeof(vis));
memset(dfn,0,sizeof(dfn));
memset(id,0,sizeof(id));
num=dcc=0;
for(int i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
addedge(x,y),addedge(y,x);
}
deep[1]=fa[1]=0;
tarjan(1,0);
for(int i=1;i<=n;i++)
if(!id[i])
++dcc,dfs(i);
int q;
printf("Case %d:\n",++times);
scanf("%d",&q);
while(q--)
{
scanf("%d%d",&x,&y);
if(id[x]==id[y])
printf("%d\n",dcc-1);
else
{
lcawork(x,y);
printf("%d\n",dcc-1);
}
}
}
return 0;
}