1. 程式人生 > >POJ3694加邊後橋的數目 LCA+並查集+強聯通分量

POJ3694加邊後橋的數目 LCA+並查集+強聯通分量

http://poj.org/problem?id=3694

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

 

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <climits>
 
using namespace std;
 
#define LL long long
const int INF=0x3f3f3f3f;
 
const int N=101000;
 
struct Node
{
    int v,id,nt,flag;
}edge[400020];
int s[N],cnt,dep;
int n,m;
int f[N],p[N];
int dfn[N],low[N],ans[N];//ans[N]用來記錄橋的編號,或者可以用brideg[N][2]來記錄橋的兩個端點
bool vis[N];
int nbridge;
 
int Find(int x)
{
    return x==f[x]?x:(f[x]=Find(f[x]));
}
 
void Union(int a,int b)
{
    int x=Find(a),y=Find(b);
    if(x!=y) f[x]=y;
}
 
void AddEdge(int u,int v,int id)
{
    for(int i=s[u];~i;i=edge[i].nt)
    {
        if(edge[i].v==v)
        {
            edge[i].flag++;
            return ;
        }
    }
    edge[cnt].v=v;
    edge[cnt].nt=s[u];
    edge[cnt].flag=0;
    edge[cnt].id=id;
    s[u]=cnt++;
}
 
void tarjan(int u,int pre)
{
    vis[u]=true;
    dfn[u]=low[u]=dep++;
    for(int i=s[u];~i;i=edge[i].nt)
    {
        int v=edge[i].v;
        if(v==pre) continue;
        if(!vis[v])
        {
            p[v]=u;
            tarjan(v,u);
            low[u]=min(low[u],low[v]);
            if(dfn[u]>=low[v]) Union(u,v);
            if(dfn[u]<low[v]&&!edge[i].flag)
                ans[nbridge++]=edge[i].id;
        }
        else low[u]=min(low[u],dfn[v]);
    }
}
 
void GetConnection()
{
    for(int i=0;i<=n;i++) f[i]=i;
    nbridge=0,dep=1;
    memset(vis,0,sizeof vis);
    tarjan(1,0);
}
 
int lca(int x,int y)
{
    if(dfn[x]<dfn[y]) swap(x,y);
    while(dfn[x]>dfn[y])
    {
        if(Find(x)!=Find(p[x]))
          nbridge--,f[Find(x)]=Find(p[x]);
        x=p[x];
    }
    while(y!=x)
    {
        if(Find(y)!=Find(p[y]))
          nbridge--,f[Find(y)]=Find(p[y]);
        y=p[y];
    }
    return nbridge;
}
 
int main()
{
    int cas=1;
    while(~scanf("%d%d",&n,&m)&&(n+m))
    {
        printf("Case %d:\n",cas++);
        memset(s,-1,sizeof s);
        cnt=0;
        for(int i=0; i<m; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            AddEdge(u,v,i),AddEdge(v,u,i);
        }
        GetConnection();
        int q;
        scanf("%d",&q);
        while(q--)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            printf("%d\n",lca(u,v));
        }
        printf("\n");
    }
    return 0;
}