poj3694(割边,LCA)

Network
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 8578   Accepted: 3138

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次询问,每次询问都要添加一条边,然后问你有多少条割边。

思路:首先用tarjan预处理出来所有的割边,查询的时候就直接用LCA来实现查询就好。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
const int N=1000010;
struct data
{
    int to,next;
} tu[N*2];
int head[N];
int ip;
int dfn[N], low[N];///dfn[]表示深搜的步数,low[u]表示u或u的子树能够追溯到的最早的栈中节点的次序号
int f[N],bridge[N],Dfn[N];
int step,n,m,cnt;
void init()
{
    ip=step=cnt=0;
    memset(head,-1,sizeof(head));
    memset(dfn, 0, sizeof(dfn));
    memset(Dfn, 0, sizeof(Dfn));
    memset(bridge, 0, sizeof(bridge));
    for(int i = 1; i <= n; i++) f[i] = i;
}
void add(int u,int v)
{
    tu[ip].to=v,tu[ip].next=head[u],head[u]=ip++;
}
void dfs(int u)
{
    dfn[u] = low[u] = ++step;
    Dfn[u]=Dfn[f[u]]+1;
    for (int i = head[u]; i !=-1; i=tu[i].next)
    {
        int v = tu[i].to;
        if (!dfn[v])
        {
            f[v]=u;
            dfs(v);
            low[u] = min(low[u], low[v]);
            if(low[v]>dfn[u])
            {
                cnt++;
                bridge[v]=1;
            }
        }
        else if(v!=f[u])
            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 = f[u];
    }
    while(Dfn[v] > Dfn[u])
    {
        if(bridge[v]) cnt--, bridge[v] = 0;
        v = f[v];
    }
    while(u != v)
    {
        if(bridge[u]) cnt--, bridge[u] = 0;
        if(bridge[v]) cnt--, bridge[v] = 0;
        u = f[u];
        v = f[v];
    }
}

int main()
{
    int o=1;
    while(~scanf("%d%d",&n,&m)&&m+n)
    {
        init();
        printf("Case %d:\n",o++);
        while(m--)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            add(x,y);
            add(y,x);
        }
        dfs(1);
        int q;
        scanf("%d",&q);
        while(q--)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            lca(x,y);
            printf("%d\n",cnt);
        }
        puts("");
    }
    return 0;
}

posted @ 2016-05-11 15:37  martinue  阅读(172)  评论(0编辑  收藏  举报