zoukankan      html  css  js  c++  java
  • poj3177 无向连通图加多少条边变成边双连通图

    Redundant Paths
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 15752   Accepted: 6609

    Description

    In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forced to take a particular path and want to build some new paths so that they will always have a choice of at least two separate routes between any pair of fields. They currently have at least one route between each pair of fields and want to have at least two. Of course, they can only travel on Official Paths when they move from one field to another.

    Given a description of the current set of R (F-1 <= R <= 10,000) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way.

    There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.

    Input

    Line 1: Two space-separated integers: F and R

    Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.

    Output

    Line 1: A single integer that is the number of new paths that must be built.

    Sample Input

    7 7
    1 2
    2 3
    3 4
    2 5
    4 5
    5 6
    5 7

    Sample Output

    2
    注意重边
    不能通过low值判断两个点在一个连通分量内,
    因为这样的数据 1-6都在一个联通块内,但6的low值和其他不同。
    6 7
    1 2
    2 3
    3 4
    4 6
    6 2
    4 5
    5 1
    但是貌似 没有影响?。因为虽然6的low值有变化,但是他不可能是叶节点,所以用low值水过去也是可以的吧


    就是缩点后找叶子结点的数目+1/2就是答案
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int N=5050;
    bool mp[N][N];
    int head[N],dfn[N],low[N],cont[N];
    int tot,cnt;
    struct node{
       int next,to;
    }e[N<<1];
    void add(int u,int v){
       e[tot].to=v;e[tot].next=head[u];head[u]=tot++;
       e[tot].to=u;e[tot].next=head[v];head[v]=tot++;
    }
    void Tajan(int u,int fa){
       dfn[u]=low[u]=++cnt;
       for(int i=head[u];i+1;i=e[i].next){
        int v=e[i].to;
        if(v==fa) continue;
        if(!dfn[v]) {
            Tajan(v,u);
            low[u]=min(low[v],low[u]);
        }
        else low[u]=min(low[u],dfn[v]);
       }
    }
    int main(){
        int n,m;
        while(scanf("%d%d",&n,&m)!=EOF){
            int u,v;
            memset(head,-1,sizeof(head));
            memset(mp,0,sizeof(mp));
            memset(cont,0,sizeof(cont));
            tot=cnt=0;
            while(m--){
                scanf("%d%d",&u,&v);
                if(mp[u][v]) continue;
                add(u,v);
                mp[u][v]=mp[v][u]=1;
            }
            Tajan(1,0);
            for(int i=1;i<=n;++i)
            for(int j=head[i];j+1;j=e[j].next){
                int v=e[j].to;
                if(low[v]!=low[i])
                    ++cont[low[i]];
            }
            int ans=0;
            for(int i=1;i<=n;++i)
                if(cont[i]==1) ++ans;
                printf("%d
    ",(ans+1)/2);
        }
    }
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int N=5050;
    bool mp[N][N];
    int head[N],dfn[N],low[N],cont[N],bl[N],q[N];
    int tot,cnt,scnt,l;
    struct node{
       int next,to;
    }e[N<<1];
    void add(int u,int v){
       e[tot].to=v;e[tot].next=head[u];head[u]=tot++;
       e[tot].to=u;e[tot].next=head[v];head[v]=tot++;
    }
    void Tajan(int u,int fa){
        q[l++]=u;
       dfn[u]=low[u]=++cnt;
       for(int i=head[u];i+1;i=e[i].next){
        int v=e[i].to;
        if(v==fa) continue;
        if(!dfn[v]) {
            Tajan(v,u);
            low[u]=min(low[v],low[u]);
        }
        else low[u]=min(low[u],dfn[v]);
       }
       if(dfn[u]==low[u]){
        int t;
        ++scnt;
        do{
            t=q[--l];
            bl[t]=scnt;
        }while(t!=u);
       }
    }
    int main(){
        int n,m;
        while(scanf("%d%d",&n,&m)!=EOF){
            int u,v;
            memset(head,-1,sizeof(head));
            memset(mp,0,sizeof(mp));
            memset(cont,0,sizeof(cont));
            l=scnt=tot=cnt=0;
            while(m--){
                scanf("%d%d",&u,&v);
                if(mp[u][v]) continue;
                add(u,v);
                mp[u][v]=mp[v][u]=1;
            }
            Tajan(1,0);
            for(int i=1;i<=n;++i)
            for(int j=head[i];j+1;j=e[j].next){
                int v=e[j].to;
                if(bl[v]!=bl[i])
                    ++cont[bl[i]];
            }
            int ans=0;
            for(int i=1;i<=n;++i)
                if(cont[i]==1) ++ans;
                printf("%d
    ",(ans+1)/2);
        }
    }
  • 相关阅读:
    【转】Oracle学习系列
    昏昏昏昏昏昏,怎么变成这样了。:(
    SQLServer 2K 安装重复出现挂起问题解决办法
    ORM iBATIS 学习,没弄清楚。
    MagicLinux让我用我一个方便的方法引导你吧。
    SOA大赛初赛文档已经提交.心中大石掉下.
    继续一下目标。
    ORM已经理解了.
    Spot the Bug Episode 2 中BUG的修改
    MaglicLinux启动加入了BOO.INI搞定.这个方便了.
  • 原文地址:https://www.cnblogs.com/mfys/p/7274105.html
Copyright © 2011-2022 走看看