• poj3352


    Road Construction
    Time Limit: 2000MSMemory Limit: 65536K
    Total Submissions: 7980Accepted: 4014

    Description

    It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads that lead between the various tourist attractions on the island.

    The roads themselves are also rather interesting. Due to the strange customs of the island, the roads are arranged so that they never meet at intersections, but rather pass over or under each other using bridges and tunnels. In this way, each road runs between two specific tourist attractions, so that the tourists do not become irreparably lost.

    Unfortunately, given the nature of the repairs and upgrades needed on each road, when the construction company works on a particular road, it is unusable in either direction. This could cause a problem if it becomes impossible to travel between two tourist attractions, even if the construction company works on only one road at any particular time.

    So, the Road Department of Remote Island has decided to call upon your consulting services to help remedy this problem. It has been decided that new roads will have to be built between the various attractions in such a way that in the final configuration, if any one road is undergoing construction, it would still be possible to travel between any two tourist attractions using the remaining roads. Your task is to find the minimum number of new roads necessary.

    Input

    The first line of input will consist of positive integers n and r, separated by a space, where 3 ≤ n ≤ 1000 is the number of tourist attractions on the island, and 2 ≤ r ≤ 1000 is the number of roads. The tourist attractions are conveniently labelled from 1 to n. Each of the following r lines will consist of two integers, v and w, separated by a space, indicating that a road exists between the attractions labelled v and w. Note that you may travel in either direction down each road, and any pair of tourist attractions will have at most one road directly between them. Also, you are assured that in the current configuration, it is possible to travel between any two tourist attractions.

    Output

    One line, consisting of an integer, which gives the minimum number of roads that we need to add.

    Sample Input

    Sample Input 1 10 12 1 2 1 3 1 4 2 5 2 6 5 6 3 7 3 8 7 8 4 9 4 10 9 10  Sample Input 2 3 3 1 2 2 3 1 3

    Sample Output

    Output for Sample Input 1 
     Output for Sample Input 2
     0
    题意:给出一个图求出至少添加多少边才能将其变为一个双联通图。
    sl:缩点之后得到一个DAG,求出DAG图所有的叶子节点,可以通过low数组记录下每个节点所属的联通分量,然后
    枚举每个节点的子节点low值是不是一样,不一样则其中一个节点的入度加1然后就是图论的小知识点了。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<vector>
     5 using namespace std;
     6 const int MAX = 1010 ;
     7 vector<int> G[MAX];
     8 int pre[MAX],low[MAX],deg[MAX];
     9 int dfs_clock;
    10 void add_edge(int from,int to)
    11 {
    12     G[from].push_back(to);
    13     G[to].push_back(from);
    14 }
    15 
    16 int dfs(int u,int fa)
    17 {
    18     int lowu=pre[u]=++dfs_clock;
    19     for(int i=0;i<G[u].size();i++)
    20     {
    21         int v=G[u][i];
    22         if(!pre[v])
    23         {
    24             int lowv=dfs(v,u);
    25             lowu=min(lowu,lowv);
    26         }
    27         else if(pre[u]>pre[v]&&v!=fa)
    28         {
    29             lowu=min(lowu,pre[v]);
    30         }
    31     }
    32     low[u]=lowu;
    33 //    printf("%d ",lowu);
    34     return lowu;
    35 }
    36 void solve(int n)
    37 {
    38     memset(pre,0,sizeof(pre));
    39     memset(low,0,sizeof(low));
    40     memset(deg,0,sizeof(deg)); int ans=0;
    41     dfs(1,-1);
    42     for(int i=1;i<=n;i++)
    43     {
    44         for(int j=0;j<G[i].size();j++)
    45         {
    46            // printf("!!%d %d ",low[i],low[G[i][j]]);
    47             if(low[i]!=low[G[i][j]])
    48             deg[low[i]]++;
    49         }
    50     }
    51     for(int i=1;i<=n;i++)
    52     if(deg[i]==1) ans++;
    53     ans=(ans+1)>>1;
    54     printf("%d ",ans);
    55 }   
    56 int main()
    57 {
    58     int n,m; int a,b;
    59     while(scanf("%d %d",&n,&m)==2)
    60     {
    61         for(int i=0;i<=n;i++) G[i].clear();
    62         dfs_clock=0;
    63         for(int i=0;i<m;i++)
    64         {
    65             scanf("%d %d",&a,&b);
    66             add_edge(a,b);
    67         }
    68         solve(n);
    69     }
    70     return 0;
    71 }

  • 相关阅读:
    Windows 下使用 GNUstep 编译并运行 Objective-C 程序
    【Objective-C】Windows下Objective-C开发环境配置
    Windows远程桌面连接Mac OS X
    Windows下编译objective-C
    自动更新开奖数据的excel文件,供大家下载
    总结一下这几天学习django的心得
    Windows上python开发--2安装django框架
    Centos 如何安装Django环境
    Centos 6.4 python 2.6 升级到 2.7
    centos启用ftp功能
  • 原文地址:https://www.cnblogs.com/acvc/p/3612433.html
Copyright © 2020-2023  润新知