• POJ3352-Road Construction(边连通分量)


    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, vand 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
    2
    
    Output for Sample Input 2
    0

    题解: 
      给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图。
      裸题吧,只需要边双联通后,判断统计度为一的点,然后(cnt+1)/2
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<vector>
     5 #include<algorithm>
     6 
     7 using namespace std;
     8 
     9 const int N=5005;
    10 vector<int>g[N];
    11 int dfn[N],low[N],dg[N],tim;
    12 bool vis[N],map[N][N];
    13 int n,r;
    14 
    15 void tarjan(int u,int fa)
    16 {
    17     dfn[u]=low[u]=++tim;
    18     vis[u]=1;
    19     for (int i=0;i<g[u].size();i++)
    20     {
    21         int v=g[u][i]; 
    22         if (v==fa) continue;
    23         if (!dfn[v])
    24         {
    25             tarjan(v, u); 
    26             low[u]=min(low[u],low[v]);
    27         }
    28         else if (vis[v]) low[u]=min(low[u],dfn[v]); 
    29     }
    30 }
    31 void init()
    32 {
    33     memset(dfn,0,sizeof(dfn)); 
    34     memset(low,0,sizeof(low)); 
    35     memset(vis,0,sizeof(vis)); 
    36     tim=0;
    37     tarjan(1,-1);
    38 }
    39 int main()
    40 {
    41     while (~scanf("%d%d",&n,&r))
    42     {
    43         for (int i=1;i<=n;i++) 
    44             g[i].clear();
    45         memset(map,0,sizeof(map));
    46         int u,v;
    47         for (int i=0;i<r;i++)
    48         {
    49             scanf("%d%d",&u,&v); 
    50             if (!map[u][v])
    51             {
    52                 g[u].push_back(v);
    53                 g[v].push_back(u);
    54                 map[u][v]=map[v][u]=1;
    55             }
    56         }
    57 
    58         init();
    59         memset(dg,0,sizeof(dg));
    60         
    61         for (int u=1;u<=n;u++)
    62             for (int i=0;i<g[u].size();i++)
    63             {
    64                 int v=g[u][i]; 
    65                 if (low[u]!=low[v]) dg[low[u]]++;  
    66             }
    67 
    68         int cnt=0;
    69         for (int i=1;i<=n;i++)
    70             if (dg[i]==1) cnt++;
    71         printf("%d
    ",(cnt+1)/2);
    72     }
    73 }
    
    
  • 相关阅读:
    kubectl 命令详解
    codeforce344 C report
    poj3041 建图+最小顶点覆盖(最大匹配数)
    poj1637 混合欧拉回路的判定
    poj1149 最大流好题 难在建图 好题
    targan 算法模板
    poj2186 强连通分量 targan算法的应用
    poj2723 2分 + 2-sat
    poj3061 尺取法
    poj3207 2-sat基础题
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/7747708.html
Copyright © 2020-2023  润新知