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算法。这些简单题也是写的飞起..写多了 就觉得只是套了个模板,一直在默写,在写题的时候并没有关注细节..
相关文章推荐
- NodeJs文件操作备注
- VIBE君,放过MOTO吧
- nodejs初步了解
- POJ 3181 Dollar Dayz
- Linux启动管理-启动过程/grub配置/Grub加密
- java多线程使用学习笔记
- POJ 题目3581 Sequence(后缀数组+离散化)
- xml使用
- 闭包 Closure
- 分享bitcraze的管理理念--Self organizing
- 九度OJ 题目1139:最大子矩阵
- Android GridView中设置了Button以后就不能响应OnItemClick()
- 判断[]是否是数组的两个方法
- 我的100个愿望
- Linux-2.6.32.67内核裁剪与移植之DM9000网卡驱动移植(3)
- maven 工程启动找不到 Spring ContextLoaderListener 的解决办法
- LeetCode-Single Number-解题报告
- collections接口用法
- OpenCv算法编程优化
- 最简MacOs10.8安装