zoukankan      html  css  js  c++  java
  • poj3177 Redundant Paths

    Redundant Paths
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 7749   Accepted: 3377

    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

    Hint

    Explanation of the sample:

    One visualization of the paths is:
       1   2   3
       +---+---+  
           |   |
           |   |
     6 +---+---+ 4
          / 5
         / 
        / 
     7 +
    Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions.
       1   2   3
       +---+---+  
       :   |   |
       :   |   |
     6 +---+---+ 4
          / 5  :
         /     :
        /      :
     7 + - - - - 
    Check some of the routes:
    1 – 2: 1 –> 2 and 1 –> 6 –> 5 –> 2
    1 – 4: 1 –> 2 –> 3 –> 4 and 1 –> 6 –> 5 –> 4
    3 – 7: 3 –> 4 –> 7 and 3 –> 2 –> 5 –> 7

    Every pair of fields is, in fact, connected by two routes.

    It's possible that adding some other path will also solve the problem (like one from 6 to 7). Adding two paths, however, is the minimum.

    Source

    双连通分量,题目就是要求加几个边,可以成为一个双连通图,,主要先用tarjan缩点建图,可以用并查集缩图,得到一树,那么答案就是(叶子数+1)/2;但是要注意,这题,是可能有重边的,如果有重边,对于邻接表的话,要去重处理!
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    #define mem(a,b) memset(a,b,sizeof(a))
    #define N 10050
    #define E 50000
    int dfn[N],in[N],fa[N],bridge_m,bridge[E][2],low[N],vis[N],clock_m,father[N],head[N],vec[E],next[E],edge_m;
    int addedge(int a,int b){
        vec[edge_m]=b;next[edge_m]=head[a];head[a]=edge_m++;
    }
    int init(int n){
        mem(dfn,0);mem(low,0);mem(head,-1);mem(vis,0);mem(in,0);
        for(int i=0;i<=n;i++)father[i]=i;
        edge_m=0;clock_m=0;bridge_m=0;
    }
    int find(int x){
        if(x!=father[x])
        father[x]=find(father[x]);
        return father[x];
    }
    int add(int s,int e){
        int x=find(s),y=find(e);
        if(x!=y)
        father[x]=y;
    }
    int tarjan(int x,int pre){
        dfn[x]=low[x]=++clock_m;
        vis[x]=1;
        int j;
        for(j=head[x];j!=-1;j=next[j]){
            if(vec[j]==pre)
                continue;
            if(!vis[vec[j]]){
                tarjan(vec[j],x);
                low[x]=min(low[x],low[vec[j]]);
                if(dfn[x]<low[vec[j]]){
                    bridge[bridge_m][0]=x,bridge[bridge_m++][1]=vec[j];
                }
                else {
                    add(vec[j],x);
                }
            }
            else {
                low[x]=min(low[x],dfn[vec[j]]);
            }
        }
    }
    int main()
    {
        int n,m,i,s,e;
        while(scanf("%d%d",&n,&m)!=EOF){
            init(n);
            for(i=0;i<m;i++){
                scanf("%d%d",&s,&e);
                addedge(s,e);
                addedge(e,s);
            }
            tarjan(1,-1);
            int scnt=0;
            for(i=1;i<=n;i++){
                if(find(i)==i)
                fa[i]=scnt++;
            }
            for(i=0;i<bridge_m;i++){
                s=find(bridge[i][0]),e=find(bridge[i][1]);
                in[fa[s]]++;in[fa[e]]++;
            }
            int ans=0;
            for(i=0;i<scnt;i++){
               if(in[i]==1){
                    ans++;
                }
            }
            printf("%d
    ",(ans+1)/2);
        }
        return 0;
    }
    


  • 相关阅读:
    前缀和
    hdu6290奢侈的旅行
    make_pair
    New Year and Buggy Bot
    STL next_permutation 算法原理和自行实现
    前端面试题集合
    node设置cookie
    黑客与geek
    xss
    node async
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3331232.html
Copyright © 2011-2022 走看看