zoukankan      html  css  js  c++  java
  • poj3694 连通无向图图加边后有多少桥

    Network
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 10261   Accepted: 3807

    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≤ AB ≤ 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 ≤ ABN), 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


    我怎么能这么菜


    因为会有如下图

    1-2
    2-3
    3-4
    4-5
    5-6
    6-7
    7-4
    5-8
    8-1
    那么实际上1.2.3.4.5.8的low值为1
    而6.7的low值为4,如果下面标有必须这么写的地方换成low[v]>low[u]就会造成误判


     

    这份代码有个bug

    数据 3 3

    1 2

    2 1

    2 3

    1

    3 2

    可以看出他不能判断消除的话要记录一下反向边,然后强行将flag搞成0就可以了吧 因为没有更严格的数据评测也无法判断

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int N=100008;
    int head[N],dfn[N],low[N],dep[N],fa[N];
    int tot,cnt,ans,n,m;
    bool flag[N];
    struct node{
       int to,next;
    }e[N<<2];
    void init(){
       memset(head,-1,sizeof(head));
       memset(dfn,0,sizeof(dfn));
       memset(flag,0,sizeof(flag));
       fa[1]=1;
       tot=cnt=ans=0;
    }
    void add(int u,int v){
       e[tot].to=v;
       e[tot].next=head[u];
       head[u]=tot++;
    }
    void Tajan(int u,int pre){
       dep[u]=dep[pre]+1;
       dfn[u]=low[u]=++cnt;
       for(int i=head[u];i+1;i=e[i].next){
        int v=e[i].to;
        if(v==pre) continue;
        if(!dfn[v]) {
            fa[v]=u;
            Tajan(v,u);
            low[u]=min(low[u],low[v]);
                if(low[v]>dfn[u])  //必须这么写
                {
                    flag[v]=1;
                    ++ans;
                }
        }
        else low[u]=min(low[u],dfn[v]);
       }
    }
    void LCA(int a,int b){
        if(dep[a]<dep[b]) swap(a,b);
        while(dep[a]>dep[b]){
            if(flag[a]) --ans,flag[a]=0;
            a=fa[a];
        }
        while(a!=b){
            if(flag[a]) --ans,flag[a]=0;
            if(flag[b]) --ans,flag[b]=0;
            a=fa[a],b=fa[b];
        }
    }
    int main(){
        int a,b,T=1;
        while(scanf("%d%d",&n,&m),n+m){
            init();
            while(m--){
                scanf("%d%d",&a,&b);
                add(a,b);
                add(b,a);
            }
            Tajan(1,0);
            scanf("%d",&m);
            printf("Case %d:
    ",T++);
            while(m--){
                scanf("%d%d",&a,&b);
                if(ans) LCA(a,b);
                printf("%d
    ",ans);
            }
            puts("");
        }
    }
  • 相关阅读:
    subprocess 子进程模块
    3.5 魔法方法
    ThinkPHP中,display和assign用法详解
    linux常用指令
    退出当前Mysql使用的db_name 的方式
    PHP中GD库是做什么用的? PHP GD库介绍11111111
    include跟include_once 以及跟require的区别
    全局变量跟局部变量
    关于define
    创建、删除索引---高级部分
  • 原文地址:https://www.cnblogs.com/mfys/p/7284657.html
Copyright © 2011-2022 走看看