并查集,例题 疏通道路
来源:互联网 发布:chipgenius软件 编辑:程序博客网 时间:2024/04/29 05:39
/*某省调查城镇交通状况,得到现有城镇道路统计表,
表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是
使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,
只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?*/
表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是
使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,
只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?*/
#include<iostream>#include<string.h>#include<stdio.h>#include<ctype.h>#include<algorithm>#include<stack>#include<queue>#include<set>#include<math.h>#include<vector>#include<deque>#include<list>using namespace std;//#include "stdio.h"int bin[1002];int findx(int x){ int r=x; while(bin[r] !=r) r=bin[r]; return r;}void merge(int x,int y){ int fx,fy; fx = findx(x); fy = findx(y); if(fx != fy) bin[fx] = fy;}int main(){ int n,m,i,x,y,count; while(scanf("%d",&n),n) { for(i=1;i<=n;i++) bin[i] = i; for(scanf("%d",&m);m>0;m--) { scanf("%d %d",&x,&y); merge(x,y); } for(count=-1, i=1;i<=n;i++) if(bin[i] == i) count ++; printf("%d\n",count); }}
0 0
- 并查集,例题 疏通道路
- 并查集的例题
- 并查集例题一
- 并查集例题二
- 并查集例题:亲戚
- 并查集 bnuoj51275 道路修建 Large
- 【jzoj3886】【道路维护】【并查集】
- HDU 1232 畅通道路 【并查集】
- 并查集及例题题解
- 并查集及例题题解
- 并查集的简单例题
- 并查集例题-The Suspects
- HDU 1213 (并查集 例题)
- 并查集加深[三道例题]
- 并查集的具体例题
- 并查集学习有例题
- 并查集 例题:HDU 1829
- 并查集的使用例题uva208
- 在编译的时候会提示 File not found:'*.dcu'解决办法
- javascript判断字符长度
- 能让你成为更优秀程序员的10个C语言资源
- Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
- Java RMI详解
- 并查集,例题 疏通道路
- [LeetCode] Reverse Nodes in k-Group
- Merge k Sorted Lists -- LeetCode
- 内存操作函数memcpy,memccpy,memmove,memchr,memcmp,memicmp,memset
- kmp算法详解
- Miracast技术详解
- 消息钩子
- Linux系统灾难恢复技术和方法
- OGRE学习笔记之_配置文件_Resources.cfg