POJ3694 Network(求橋的數目,lca,Tarjan)


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

思路

題目給出一個無向圖,然后要往無向圖中加邊,一共加q次,每次都詢問加了這條邊后,圖中有多少個橋。

先求出該無向圖的橋的數目count和邊雙連通分量,縮點,每次加邊(u,v),判斷若u,v屬於同一個雙連通分量,則橋的數目不變,否則,橋的數目必定會減少,這時橋減少的數目明顯和最近公共祖先lca有關,用裸的lca就行了,每次u和v向父節點回退,如果該節點是橋的端點,則cnt–,直到u==v為止。
有個優化:其實不用縮點,只要在求出橋的時候標記一下,然后在用lca求減少橋的數目時,利用dfn[]就行了,因為u,v的最近公共祖先dfn[]一定相等。

代碼

#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
#define inf 1000000
#define mem(a,b) memset(a,b,sizeof(a))
const int N=100000+7;
const int M=400000+5;
int dfn[N],low[N],tot,times;
int first[N],pre[N];
int bridge[N],cnt;
int n,m,k,q=1;
struct edge
{
    int v,next;
} e[M];
void add_edge(int u,int v)
{
    e[tot].v=v;
    e[tot].next=first[u];
    first[u]=tot++;
}
void init()
{
    mem(first,-1);
    mem(dfn,0);
    mem(low,0);
    mem(bridge,0);
    mem(pre,0);
    tot=0;
    cnt=0;
    times=0;
}
void tarjan(int u,int fa)
{
    low[u]=dfn[u]=++times;
    int flag=0;
    for(int i=first[u]; ~i; i=e[i].next)
    {
        int v=e[i].v;
        if(v==fa&&!flag)
        {
            flag=1;
            continue;
        }
        if(!dfn[v])
        {
            pre[v]=u;
            tarjan(v,u);
            low[u]=min(low[u],low[v]);
            if(low[v]>dfn[u])
            {
                cnt++;
                bridge[v]=1;
            }
        }
        else
            low[u]=min(low[u],dfn[v]);
    }
}

void lca(int u,int v)
{
    while(dfn[u]>dfn[v])
    {
        if(bridge[u])
        {
            cnt--;
            bridge[u]=0;
        }
        u=pre[u];
    }
    while(dfn[v]>dfn[u])
    {
        if(bridge[v])
        {
            cnt--;
            bridge[v]=0;
        }
        v=pre[v];
    }
    while(u!=v)
    {
        if(bridge[u])
        {
            cnt--;
            bridge[u]=0;
        }
        if(bridge[v])
        {
            cnt--;
            bridge[v]=0;
        }
        u=pre[u];
        v=pre[v];
    }
}

int main()
{
    int u,v;
    while(scanf("%d%d",&n,&m)&&(n||m))
    {
        init();
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d",&u,&v);
            add_edge(u,v);
            add_edge(v,u);
        }
        for(int i=1; i<=n; i++)
            if(!dfn[i])
                tarjan(i,-1);
        printf("Case %d:\n",q++);
        scanf("%d",&k);
        while(k--)
        {
            scanf("%d%d",&u,&v);
            lca(u,v);
            printf("%d\n",cnt);
        }
        puts("");
    }
    return 0;
}


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
  © 2014-2022 ITdaan.com