poj3694 D - Network LCA离线算法(Trajan算法)

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

题意:给你一张连通无向图,向图中加Q次边,问你每次加边后,图中有几条割边。是可以有重边的,比如原图中边(1,2)是一条割边,再加一条边(1,2) ,就消除掉了一条割边。

#include <iostream>
#include<vector>
#include<stack>
#include<queue>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N=300000;
vector<int>g[N];
int n,m;
int low[N],dfn[N];
int Dfn[N];int pre[N];
int sum,bridge,k;int tot;
int p;int uu[N];
void init()
{
    tot=bridge=k=0;p=0;
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(pre,0,sizeof(pre));
    memset(Dfn,0,sizeof(Dfn));
    memset(uu,0,sizeof(uu));
    for(int i=0;i<=n;i++)
        g[i].clear();
}

void Trajan(int u,int fa)
{
    low[u]=dfn[u]=++k;
    Dfn[u]=Dfn[fa]+1;//理解这里
    int flag=0;
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if(v==fa&&!flag)
        {
            flag++;
            continue;
        }
        if(!dfn[v])
        {
            pre[v]=u;//建立祖先关系
            Trajan(v,u);
            low[u]=min(low[u],low[v]);
            if(low[v]>dfn[u])
            {
                bridge++;
                uu[v]=1;
            }
        }
        else
            low[u]=min(low[u],dfn[v]);
    }
}
void lac(int u,int v)//LCA离线算法
{
    while(Dfn[u]>Dfn[v])
    {
        if(uu[u])
        {
            bridge--;
            uu[u]=0;
        }
         u=pre[u];
    }
    while(Dfn[u]<Dfn[v])
    {
        if(uu[v])
        {
            bridge--;
            uu[v]=0;
        }
        v=pre[v];
    }
    while(u!=v)
    {
        if(uu[u])
        {
            uu[u]=0;
            bridge--;
        }
        if(uu[v])
        {
            uu[v]=0;
            bridge--;
        }
        v=pre[v];u=pre[u];
    }
}
void solve()
{
    for(int i=1;i<=n;i++)
        if(!dfn[i])
           Trajan(i,-1);
}
int main()
{
    int pp=0;
   while(~scanf("%d%d",&n,&m))
   {
       if(n==0&&m==0)
        return 0;
       int a,b;
       init();
       for(int i=0;i<m;i++)
       {
           scanf("%d%d",&a,&b);
           g[a].push_back(b);
           g[b].push_back(a);
       }
       int u,o=0;
       scanf("%d",&u);
       Trajan(1,1);
       printf("Case %d:\n",++pp);
       for(int i=0;i<u;i++)
       {
           scanf("%d%d",&a,&b);
           lac(a,b);//直接调用lca函数就行
           printf("%d\n",bridge);
       }

       printf("\n");
   }
   return 0;
}

猜你喜欢

转载自blog.csdn.net/Kuguotao/article/details/84036777