Network HDU - 2460

題目:

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

題目大意:

給定一個圖,在這個圖裏面加邊,求出每加一條邊後,圖中橋的個數

思路:

先用tarjan求出原來圖中的割邊。
由於在u,v之間加一條邊 假設u,v的最近公共祖先u_v 那麼加邊會使得圖中形成一個u,v,u_v的一個環 此時如果環上有割邊那麼他們將失去割邊特性。
所以加邊時一邊求LCA一邊判斷所經過的邊是不是割邊並統計即可。

代碼:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int maxn = 456789;
int T,n,m,tot,time,scc;
int num_birge,order;
int no,q;
int head[maxn],dfn[maxn],low[maxn];
int isbrige[maxn];
int fa[maxn];

struct DEGE
{
    int v;
    int next;
}edge[maxn];

void init()
{
    num_birge=time=scc=tot=0;
    memset(head,-1,sizeof(head));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(isbrige,0,sizeof(isbrige));
}

void add_edge(int u,int v)
{
    tot++;
    edge[tot].v=v;
    edge[tot].next=head[u];
    head[u]=tot;
}

void tarjan(int u,int f)
{
    fa[u]=f;
    dfn[u]=low[u]=++order;
    int flag=0;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(!dfn[v])
        {
            fa[v]=u;
            tarjan(v,u);
            if(low[v]>dfn[u])
            {
                num_birge++;
                isbrige[v]=1;
            }
            low[u]=min(low[u],low[v]);
        }
        else if(v!=fa[u])
            low[u]=min(low[u],dfn[v]);
    }
}

int find_f(int u,int v)
{
    while(dfn[u]>dfn[v])
    {
        if(isbrige[u])
        {
            num_birge--;
            isbrige[u]=0;
        }
        u=fa[u];
    }
    return u;
}

void lca(int u,int v)
{
    u=find_f(u,v);
    v=find_f(v,u);
    while(u!=v)
    {
        if(isbrige[u])
        {
            num_birge--;
            isbrige[u]=0;
        }
        if(isbrige[v])
        {
            num_birge--;
            isbrige[v]=0;
        }
        v=fa[v];
        u=fa[u];
    }
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0||m==0)
            break;
        init();
        for(int i=0;i<m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            add_edge(u,v);
            add_edge(v,u);
        }
        tarjan(1,-1);
        scanf("%d",&q);
        printf("Case %d:\n",++no);
        while(q--)
        {
            int u,v;
            scanf("%d %d",&u,&v);
            lca(u,v);
            printf("%d\n",num_birge);
        }
        printf("\n");
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章