POJ3694-Network(LCA+边联通分量+桥)

Network
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 9714   Accepted: 3603

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

题意:一个无向图可以有重边,下面q个操作,每次在两个点间连接一条有向边,每次连接后整个无向图还剩下多少桥(注意要考虑之前连了的边,每次回答是在上一次的基础之上)

解题思路:边连通分量+LCA。首先运行一次tarjan,求出桥和缩点,那么无向图将缩点为一棵树,树边正好是原来的桥。每次连接两点,看看这两点是不是在同一个缩点内,如果是,那么缩点后的树没任何变化,如果两点属于不同的缩点,那么连接起来,然后找这两个缩点的LCA,因为从点u到LCA再到点v再到点u,将形成环,里面的树边都会变成不是桥。计数的时候注意


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

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]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

你可能感兴趣的:(----最近公共祖先,----连通图,----模板,POJ)