您的位置:首页 > 其它

HDU-1232 畅通工程

2015-08-28 19:01 330 查看
#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算法。这些简单题也是写的飞起..写多了 就觉得只是套了个模板,一直在默写,在写题的时候并没有关注细节..
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: