zoukankan      html  css  js  c++  java
  • hdu 1232 畅通工程

    题目连接

    http://acm.hdu.edu.cn/showproblem.php?pid=1232

     畅通工程

    Description

    某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路? 

    Input

    测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M;随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1到N编号。 
    注意:两个城市之间可以有多条道路相通,也就是说
    3 3
    1 2
    1 2
    2 1
    这种输入也是合法的
    当N为0时,输入结束,该用例不被处理。 

    Output

    对每个测试用例,在1行里输出最少还需要建设的道路数目。 

    Sample Input

    4 2
    1 3
    4 3
    3 3
    1 2
    1 3
    2 3
    5 2
    1 2
    3 5
    999 0
    0

    Sample Output

    1
    0
    2
    998

    并查集。。。

     1 #include<algorithm>
     2 #include<iostream>
     3 #include<cstdlib>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<vector>
     7 #include<map>
     8 using std::cin;
     9 using std::cout;
    10 using std::endl;
    11 using std::find;
    12 using std::sort;
    13 using std::map;
    14 using std::pair;
    15 using std::vector;
    16 #define pb(e) push_back(e)
    17 #define sz(c) (int)(c).size()
    18 #define mp(a, b) make_pair(a, b)
    19 #define all(c) (c).begin(), (c).end()
    20 #define iter(c) decltype((c).begin())
    21 #define cls(arr,val) memset(arr,val,sizeof(arr))
    22 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
    23 #define rep(i, n) for (int i = 1; i <= (int)(n); i++)
    24 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
    25 const int N = 1010;
    26 typedef unsigned long long ull;
    27 struct UnionFind {
    28     int par[N], rank[N];
    29     inline void init(int n) {
    30         rep(i, n) {
    31             par[i] = i;
    32             rank[i] = 0;
    33         }
    34     }
    35     inline int find(int x) {
    36         while (x != par[x]) {
    37             x = par[x] = par[par[x]];
    38         }
    39         return x;
    40     }
    41     inline void unite(int x, int y) {
    42         x = find(x), y = find(y);
    43         if (rank[x] < rank[y]) {
    44             par[x] = y;
    45         } else {
    46             par[y] = x;
    47             if (rank[x] == rank[y]) rank[x]++;
    48         }
    49     }
    50     inline void result(int n) {
    51         int tot = 0;
    52         rep(i, n) { if (par[i] == i) tot++; }
    53         printf("%d
    ", tot - 1);
    54     }
    55 }go;
    56 int main() {
    57 #ifdef LOCAL
    58     freopen("in.txt", "r", stdin);
    59     freopen("out.txt", "w+", stdout);
    60 #endif
    61     int n, m, a, b;
    62     while (~scanf("%d", &n) && n) {
    63         go.init(n);
    64         scanf("%d", &m);
    65         rep(i, m) scanf("%d %d", &a, &b), go.unite(a, b);
    66         go.result(n);
    67     }
    68     return 0;
    69 }
    View Code
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    C#语言 循环语句
    C#语言基础语句
    抛出异常不会终止程序:
    Giew与checkBox的结合
    js去掉空格
    转:label标签的特殊用法
    LINQ Operators之过滤(Filtering)
    转:设置session过期时间
    转:Bat命令学习
    收藏:锁(待阅)
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4607232.html
Copyright © 2011-2022 走看看