并查集Union-Find Sets
来源:互联网 发布:如何制作常用算法演示 编辑:程序博客网 时间:2024/06/06 04:16
vector<int> father(n);vector<int> rank(n);/*** 并查集初始化:* 数组father中的元素初始为独立的树,father的下标i表示节点。* father[i]的值表示i节点的父节点。* rank[i]=1表示初始时所有结点的高度为1.*/void init(){ for (int i = 0; i < n; i++) { father[i] = i; rank[i] = 1; }}/*** 路径压缩:* 每次调用getFather()时将元素压缩成最原始父节点的直接子节点。*/int getFather(int son){ if (father[son] == son) return son; else { father[son] = getFather(father[son]); return father[son]; }}/*** 合并两不相交的集合:*/void union1(int x, int y){ int fx = getFather(x); int fy = getFather(y); if (fx != fy) father[fx] = fy;}/*** 利用rank数组启发式合并:*/void Union2(int x, int y){ int fx = getFather(x); int fy = getFather(y); if (fx == fy) return; //rank[fx]越大,说明越靠近根节点 if (rank[fx] > rank[fy]) father[fy] = fx; else { if (rank[fx] == rank[fy]) rank[fx]++; father[fx] = fy; }}
0 0
- 并查集 (Union-Find Sets)
- 并查集 (Union-Find Sets)
- 并查集 (Union-Find Sets)
- 并查集Union-Find Sets
- 并查集-(union-find sets)
- 并查集 (Union-Find Sets)
- 并查集 (Union-Find Sets)
- 并查集Union-Find Sets
- 并查集(Union-find Sets)
- 并查集(union-find sets)
- 常用类之四---并查集(Union-Find Sets)
- 并查集 (Union-Find Sets)及其应用
- 并查集 (Union-Find Sets)及其应用
- 并查集 (Union-Find Sets)及其应用
- 并查集 (Union-Find Sets)及其应用
- 并查集 (Union-Find Sets)及其应用
- 并查集(Union-Find Sets)及其应用
- POJ1611 The Suspects [ 数据结构-并查集 union-find sets]
- php创建word
- flooxs编译
- 算法之-----BM位图排序算法
- cocos2dx-3.12包含cocos-ext.h编译不过的问题
- iOS开发动画总结(Animation)
- 并查集Union-Find Sets
- 数字化时代下研发模式的变革
- Android 连接Wifi和创建Wifi热点 Demo
- 基于Linux的及时通信软件
- 解决WEB对js、css缓存问题的一种可行方案
- 通用类型系统(CTS)
- DOM操作节点
- linux开放端口命令
- 复合索引 优化和适用范围