zoukankan      html  css  js  c++  java
  • POJ 3352 Road Construction

    Road Construction

    Time Limit: 2000ms
    Memory Limit: 65536KB
    This problem will be judged on PKU. Original ID: 3352
    64-bit integer IO format: %lld      Java class name: Main
     

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

    Source

     
    解题:引用Byvoid的一些资料。https://www.byvoid.com/blog/biconnect/
     

    [构造双连通图]

    一个有桥的连通图,如何把它通过加边变成边双连通图?方法为首先求出所有的桥,然后删除这些桥边,剩下的每个连通块都是一个双连通子图。把每个双连通子图收缩为一个顶点,再把桥边加回来,最后的这个图一定是一棵树,边连通度为1。

    统计出树中度为1的节点的个数,即为叶节点的个数,记为leaf。则至少在树上添加(leaf+1)/2条边,就能使树达到边二连通,所以至少添加的边数就是(leaf+1)/2。具体方法为,首先把两个最近公共祖先最远的两个叶节点之间连接一条边,这样可以把这两个点到祖先的路径上所有点收缩到一起,因为一个形成的环一定是双连通的。然后再找两个最近公共祖先最远的两个叶节点,这样一对一对找完,恰好是(leaf+1)/2次,把所有点收缩到了一起。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 const int maxn = 1010;
    18 int dfn[maxn],low[maxn],belong[maxn],cnt,scc;
    19 int n,m,d[maxn];
    20 vector<int>g[maxn];
    21 bool instack[maxn];
    22 stack<int>stk;
    23 void tarjan(int u,int fa){
    24     dfn[u] = low[u] = ++cnt;
    25     stk.push(u);
    26     instack[u] = true;
    27     for(int i = 0; i < g[u].size(); i++){
    28         if(g[u][i] == fa) continue;
    29         if(!dfn[g[u][i]]){
    30             tarjan(g[u][i],u);
    31             low[u] = min(low[u],low[g[u][i]]);
    32         }else if(instack[g[u][i]]) low[u] = min(low[u],dfn[g[u][i]]);
    33     }
    34     if(low[u] == dfn[u]){
    35         scc++;
    36         int v;
    37         do{
    38             v = stk.top();
    39             stk.pop();
    40             belong[v] = scc;
    41             instack[v] = false;
    42         }while(v != u);
    43     }
    44 }
    45 int main(){
    46     int i,j,u,v,ans;
    47     while(~scanf("%d %d",&n,&m)){
    48         for(i = 0; i <= n; i++){
    49             dfn[i] = low[i] = belong[i] = 0;
    50             g[i].clear();
    51             instack[i] = false;
    52             d[i] = 0;
    53         }
    54         while(!stk.empty()) stk.pop();
    55         for(i = 0; i < m; i++){
    56             scanf("%d %d",&u,&v);
    57             g[u].push_back(v);
    58             g[v].push_back(u);
    59         }
    60         cnt = scc = 0;
    61         for(i = 1; i <= n; i++) if(!dfn[i]) tarjan(i,-1);
    62         for(i = 1; i <= n; i++){
    63             for(j = 0; j < g[i].size(); j++){
    64                 if(belong[i] != belong[g[i][j]]){
    65                     d[belong[i]]++;
    66                     d[belong[g[i][j]]]++;
    67                 }
    68             }
    69         }
    70         ans = 0;
    71         for(i = 1; i <= scc; i++)
    72             if(d[i] == 2) ans++;
    73         printf("%d
    ",(ans+1)>>1);
    74     }
    75     return 0;
    76 }
    View Code
  • 相关阅读:
    用SecureCRT 查看Linux下日志的简单命令
    性能测试知多少---并发用户
    性能测试指标的基本概念
    软件测试基本理论
    Selenium 入门视频
    零基础学软件测试
    装饰器作业
    <python全栈开发基础>学习过程笔记【16d】装饰器(含time模块)
    【搬家】我的CSDN博客地址http://my.csdn.net/myloveprogrmming
    《Python全栈开发》学习过程笔记【3】
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3939555.html
Copyright © 2011-2022 走看看