码迷,mamicode.com
首页 > Web开发 > 详细

POJ 3694——Network——————【连通图,LCA求桥】

时间:2015-11-06 22:18:14      阅读:346      评论:0      收藏:0      [点我收藏+]

标签:

Network
Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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 andB.

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

 

题目大意:n个点,m条边。q次询问,问你新加入无向边ui,vi后,图中的桥还有多少。

解题思路:求桥,标记桥,LCA过程中消去桥。

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
using namespace std;
const int maxn = 100100;
struct Edge{
    int from,to,dist,next;
    Edge(){}
    Edge(int _to,int _next):to(_to),next(_next){}
}edges[maxn*4];
int dfn[maxn];
int fa[maxn],bridge[maxn],dep[maxn];
int head[maxn];
int tot, brinum;
int dfs_clock;
void init(){
    tot = 0;
    dfs_clock = 0;
    brinum = 0;
    memset(head,-1,sizeof(head));
    memset(bridge,0,sizeof(bridge));
    memset(dfn,0,sizeof(dfn));
}
void AddEdge(int _u,int _v){
    edges[tot].to = _v;
    edges[tot].next = head[_u];
    head[_u] = tot++;
}
int dfs(int u,int f){
    int lowu = dfn[u] = ++dfs_clock;
    int child = 0;
    for(int i = head[u]; i != -1; i = edges[i].next){
        int v = edges[i].to;
        if(!dfn[v]){
            child++;
            fa[v] = u;
            int lowv = dfs(v,i);
            lowu = min(lowv,lowu);
            if(lowv > dfn[u]){  //标记桥
                bridge[v] = 1;
                brinum++;
            }
        }else if(dfn[v] < dfn[u] && (f^1) != i){
            lowu = min(lowu,dfn[v]);
        }
    }
   // low[u] = lowu;
    return lowu;
}
void LCA(int u,int v){      //简化的LCA,把求LCA过程中的桥减掉
    while(dfn[u] < dfn[v]){
        if(bridge[v] == 1){ 
            brinum--;
            bridge[v] = 0;
        }
        v = fa[v];
    }
    while(dfn[u] > dfn[v]){
        if(bridge[u] == 1){
            brinum--;
            bridge[u] = 0;
        }
        u = fa[u];
    }
    while( u != v ){
        if(bridge[u]) {
            brinum--;
            bridge[u] = 0;
        }
        if(bridge[v]){
            brinum--;
            bridge[v] = 0;
        }
        u = fa[u];
        v = fa[v];
    }
}
int main(){
    int n, m, q, cas = 0;
    while(scanf("%d%d",&n,&m)!=EOF&&(n+m)){
        int a,b;
        init();
        for(int i = 0; i < m; i++){
            scanf("%d%d",&a,&b);
            AddEdge(a,b);
            AddEdge(b,a);
        }
        for(int i = 1; i <= n; i++){
            if(!dfn[i])
            dfs(i,-1);
        }
        printf("Case %d:\n",++cas);
        scanf("%d",&q);
        for(int i = 0; i < q; i++){
            scanf("%d%d",&a,&b);
            LCA(a,b);
            printf("%d\n",brinum);
        }puts("");
    }
    return 0;
}

  

POJ 3694——Network——————【连通图,LCA求桥】

标签:

原文地址:http://www.cnblogs.com/chengsheng/p/4943705.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!