并查集
来源:互联网 发布:分布式数据库的优势 编辑:程序博客网 时间:2024/06/06 12:45
【一次做cf,用递归版的超时了,非递归版的就ac了】
递归版
#include <bits/stdc++.h>using namespace std;const int N=1e3;int fa[N];int find(int x){ return fa[x]==x?x:find(fa[x]); } void Union(int x, int y){ int fx = find(x); int fy = find(y); fa[fy] = fx; }
非递归版
#include <bits/stdc++.h>using namespace std;const int N=1e3;int fa[N];int find(int x){ int i=x, r=x, j; while(fa[r]!=r) r=fa[r]; while(fa[i]!=r) { j=fa[i]; fa[i]=r; i=j; } return r;}void Union(int a,int b){ int fx=find(a); int fy=find(b); if(fx!=fy) fa[fx]=fy;}
0 0
- HDU3938 并查集 并查集
- 并查集(集并查)
- HDU1232 并查集<并>
- 并查集
- 数据结构-并查集
- 并查集
- 并查集!
- 并查集
- 并查集
- 并查集
- 并查集
- 并查集总结
- 并查集学习
- 并查集
- 并查集
- 并查集
- 所谓并查集
- 并查集
- 类与对象的使用2
- mac-os use apt-get or yum
- java学习
- vs2013+opencv配置
- GITHUB自学系列之五「GIT 进阶」
- 并查集
- 《锋利的jQuery》第四章、 jQuery中的事件和动画
- js的异步加载(requirejs)
- 关于 web.xml 3.1 以及 jsp2.0下使用fmt标签 异常的问题
- python安装第三方的库文件
- IntelliJ IDEA15导入jar包
- hduoj 1257
- 【LeetCode-421】Maximum XOR of Two Numbers in an Array
- 共享整个磁盘