POJ 3694 Network (求橋,邊雙連通分支縮點,lca)


Network
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 5619   Accepted: 1939

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

 
 
 
 
 
 
這題是給了一個連通圖。
 
問再加入邊的過程中,橋的個數。
 
 
先對原圖進行雙連通分支縮點。可以形成一顆樹。
 
 
這顆樹的邊都是橋。
然后加入邊以后,查詢LCA,LCA上的橋都減掉。
 
標記邊為橋不方便,直接標記橋的終點就可以了。
 
 
具體看代碼吧!
很好的題目
 
 
 
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
#include <vector>
using namespace std;

const int MAXN = 100010;
const int MAXM = 400010;

struct Edge
{
    int to,next;
    bool cut;
}edge[MAXM];
int head[MAXN],tot;
int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];//Belong數組的值是1~block
int Index,top;
int block;
bool Instack[MAXN];
int bridge;

void addedge(int u,int v)
{
    edge[tot].to = v;edge[tot].next = head[u];edge[tot].cut = false;
    head[u] = tot++;
}
void Tarjan(int u,int pre)
{
    int v;
    Low[u] = DFN[u] = ++Index;
    Stack[top++] = u;
    Instack[u] = true;
    for(int i = head[u];i != -1;i = edge[i].next)
    {
        v = edge[i].to;
        if( v == pre )continue;
        if( !DFN[v] )
        {
            Tarjan(v,u);
            if(Low[u] > Low[v])Low[u] = Low[v];
            if(Low[v] > Low[u])
            {
                bridge++;
                edge[i].cut = true;
                edge[i^1].cut = true;
            }
        }
        else if(Instack[v] && Low[u] > DFN[v])
             Low[u] = DFN[v];
    }
    if(Low[u] == DFN[u])
    {
        block++;
        do
        {
            v = Stack[--top];
            Instack[v] = false;
            Belong[v] = block;
        }
        while( v != u );
    }
}
void init()
{
    tot = 0;
    memset(head,-1,sizeof(head));
}

vector<int>vec[MAXN];
int father[MAXN];
int dep[MAXN];
int a[MAXN];
void lca_bfs(int root)
{
    memset(dep,-1,sizeof(dep));
    dep[root] = 0;
    a[root] = 0;//橋的標記,標記橋的一個點
    father[root] = -1;
    queue<int>q;
    q.push(root);
    while(!q.empty())
    {
        int tmp = q.front();
        q.pop();
        for(int i = 0;i < vec[tmp].size();i++)
        {
            int v = vec[tmp][i];
            if(dep[v]!=-1)continue;
            dep[v] = dep[tmp]+1;
            a[v] = 1;
            father[v] = tmp;
            q.push(v);
        }
    }
}
int ans;
void lca(int u,int v)
{
    if(dep[u]>dep[v])swap(u,v);
    while(dep[u]<dep[v])
    {
        if(a[v])
        {
            ans--;
            a[v] = 0;
        }
        v = father[v];
    }
    while(u != v)
    {
        if(a[u])
        {
            ans--;
            a[u] = 0;
        }
        if(a[v])
        {
            ans--;
            a[v] = 0;
        }
        u = father[u];
        v = father[v];
    }
}
void solve(int N)
{
    memset(DFN,0,sizeof(DFN));
    memset(Instack,false,sizeof(Instack));
    Index = top = block = 0;
    Tarjan(1,1);
    for(int i = 1;i <= block;i++)
      vec[i].clear();
    for(int u = 1;u <= N;u++)
        for(int i = head[u];i != -1;i = edge[i].next)
           if(edge[i].cut)
           {
               int v = edge[i].to;
               vec[Belong[u]].push_back(Belong[v]);
               vec[Belong[v]].push_back(Belong[u]);
           }
    lca_bfs(1);
    ans = block - 1;
    int Q;
    int u,v;
    scanf("%d",&Q);
    while(Q--)
    {
        scanf("%d%d",&u,&v);
        lca(Belong[u],Belong[v]);
        printf("%d\n",ans);
    }
    printf("\n");
}
int main()
{
    int n,m;
    int u,v;
    int iCase = 0;
    while(scanf("%d%d",&n,&m)==2)
    {
        iCase++;
        if(n==0 && m == 0)break;
        init();
        while(m--)
        {
            scanf("%d%d",&u,&v);
            addedge(u,v);
            addedge(v,u);
        }
        printf("Case %d:\n",iCase);
        solve(n);
    }
    return 0;
}

 

 
 
 
 
 

注意!

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



 
  © 2014-2022 ITdaan.com 联系我们: