并查集
来源:互联网 发布:php bindvalue 编辑:程序博客网 时间:2024/06/09 13:03
并查集在解决图的连通性非常高效,hdu1232采用并查集,可以很快解决,代码如下:
/* * hdu1232.cpp * * Created on: 2012-5-10 * Author: ict */#include<iostream>#include<cstring>#include<cstdio>#include<cmath>#include<algorithm>using namespace std;#define CLR(arr, what) memset(arr, what, sizeof(arr))#define N 1001int pre[N];int Find_Set(int x) //递归+路径压缩{return pre[x] == -1 ? x : (pre[x] = Find_Set(pre[x]));}void Make_Set(int x, int y) //无按秩合并{int root1 = Find_Set(x);int root2 = Find_Set(y);if(root1 != root2)pre[root2] = root1;}int main(){int i, j;int n, m;int a, b;int count;while(1){scanf("%d", &n);if(!n)break;scanf("%d", &m);CLR(pre, -1);for(i = 0; i < m; i++){scanf("%d %d", &a, &b);Make_Set(a, b);}count = 0;for(i = 1; i <= n; i++){if(pre[i] == -1)count++;}printf("%d\n", count - 1);}return 0;}
- HDU3938 并查集 并查集
- 并查集(集并查)
- HDU1232 并查集<并>
- 并查集
- 数据结构-并查集
- 并查集
- 并查集!
- 并查集
- 并查集
- 并查集
- 并查集
- 并查集总结
- 并查集学习
- 并查集
- 并查集
- 并查集
- 所谓并查集
- 并查集
- 初步认识knockoutjs
- Installing Indy for Delphi XE
- decodeURIComponent URI malformed 错误
- c++动态联编与静态联编
- QT多线程中undefined reference to 'pthread_create'错误的解决
- 并查集
- java获取区间随机数
- 我的OpenCV学习笔记(15):利用反向投影直方图查找特定的内容
- 通过Flash上传文件提示出错:onHTTPError
- Mock RequestContextListener
- C++编译错误 error C2143 error C2146
- UINavigationController
- 如何编译并打开WinCE Bluetooth Stack的LOG
- OpenR2 呼叫流程