HDU-1232 畅通工程

来源:互联网 发布:mac应用强制退出 编辑:程序博客网 时间:2024/06/16 21:51
#include <iostream>#include <cstdio>using namespace std;const int maxn = 1000 + 5;int n, m;int streat[maxn];int Find(int x){    return x == streat[x] ? x : streat[x] = Find(streat[x]);}int main(){    while(~scanf("%d", &n) && n)    {        scanf("%d",& m);        int ans = n - 1;        for(int i = 1; i <= n; i ++)            streat[i] = i;        while(m --)        {            int city_x, city_y;            scanf("%d %d", & city_x, & city_y);            int x, y;            x = Find(city_x);            y = Find(city_y);            if(x != y)            {                streat[y] = x;                ans --;            }        }        printf("%d\n", ans);    }    return 0;}

题意:
任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?
题解:
早上阿信喋很快地灌输了并查集外加最小生成树Kruskal算法。这些简单题也是写的飞起..写多了 就觉得只是套了个模板,一直在默写,在写题的时候并没有关注细节..

0 0
原创粉丝点击