zoukankan      html  css  js  c++  java
  • poj3352添加多少条边可成为双向连通图

    Road Construction
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 13311   Accepted: 6715

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

    这题其实就是缩点后看有多少个度为1的点。其实和poj1236 相似 有向图添加多少条边变成强连通图

    这题就用来学习无向图缩点 和求度吧 这些都是基本操作

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <string>
     4 #include <algorithm>
     5 #include <queue>
     6 using namespace std;
     7 
     8 const int maxn = 3e5 + 10;
     9 int n, m, u, v, tot, top, cnt, flag;
    10 struct node {
    11     int u, v, next;
    12 } edge[maxn];
    13 int head[maxn], instack[maxn], s[maxn];
    14 int dfn[maxn], low[maxn], belong[maxn];
    15 void init() {
    16     tot = cnt = top = flag = 0;
    17     memset(s, 0, sizeof(s));
    18     memset(head, -1, sizeof(head));
    19     memset(dfn, 0, sizeof(dfn));
    20     memset(instack, 0, sizeof(instack));
    21     memset(belong, 0, sizeof(belong));
    22     memset(low, 0, sizeof(low));
    23     memset(edge, 0, sizeof(edge));
    24 }
    25 void add(int u, int v) {
    26     edge[tot].v = v;
    27     edge[tot].u = u;
    28     edge[tot].next = head[u];
    29     head[u] = tot++;
    30 }
    31 void tarjin(int v, int fa) {
    32     dfn[v] = low[v] = ++flag;
    33     instack[v] = 1;
    34     s[top++] = v;
    35     for (int i = head[v] ; i != -1 ; i = edge[i].next) {
    36         int j = edge[i].v;
    37         if (j == fa) continue;
    38         if (!instack[j]) {
    39             tarjin(j, v);
    40             low[v] = min(low[v], low[j]);
    41         } else if (instack[j] == 1) low[v] = min(low[v], dfn[j]);
    42     }
    43     if (dfn[v] == low[v]) {
    44         cnt++;
    45         int t;
    46         do {
    47             t = s[--top];
    48             instack[t] = 2;
    49             belong[t] = cnt;
    50         } while(t != v) ;
    51     }
    52 }
    53 int du[maxn];
    54 int main() {
    55     while(scanf("%d%d", &n, &m) != EOF) {
    56         init();
    57         memset(du, 0, sizeof(du));
    58         for (int i = 1 ; i <= m ; i++) {
    59             int x, y;
    60             scanf("%d%d", &x, &y);
    61             add(x, y);
    62             add(y, x);
    63         }
    64         for (int i = 1  ; i <= n ; i++)
    65             if (!instack[i]) tarjin(i, -1);
    66         for(int i = 0; i <= tot; i += 2) {
    67             if(belong[edge[i].u] != belong[edge[i].v]) {
    68                 du[belong[edge[i].u]]++;
    69                 du[belong[edge[i].v]]++;
    70             }
    71         }
    72         int ans = 0;
    73         for (int i = 1 ; i <= cnt ; i++)
    74             if (du[i] == 1) ans++;
    75         printf("%d
    ", (ans + 1) / 2);
    76     }
    77     return 0;
    78 }
  • 相关阅读:
    Mysql问题1862
    S3TC IAP15F2K61S2点亮一个发光二极管keil和stc-isp软件操作
    .NET练习计算平方根
    求一个整数以内的素数(函数实现)
    判断一个数是不是素数(函数实现)
    #号在进制输出值的作用,美化输出
    分类——决策树模型(附有决策树生成步骤)
    分类:贝叶斯分类之新闻组数据组学习(查看数据类型的方法)(环境:Pycharm)
    分类:K-近邻分类之鸢尾花数据集学习(包含数据预处理中的标准化)(环境:Pycharm)
    编写一个程序,求2~n间的素数,n由键盘输入,循环变量分别 从2到n、2到(int)sqrt(n),分别测出两个循环的所用时间。
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9084438.html
Copyright © 2011-2022 走看看