算法导论 第21章 21-2 深度确定
2012-11-27 20:14
106 查看
一、题目
二、代码
/* UnionFindSet.h 并查集,非递归方法,含路径压缩,数组从0开始 */ #include <iostream> using namespace std; #define MAXN 30005 class UFS { public: int n; int p[MAXN+1];//集合根结点 int rank[MAXN+1]; //集合中点的个数 int depth[MAXN+1]; public: UFS(int size = MAXN); void clear(); int Find_Set(int x); //a并入b中,不区分大小 void Union(int x, int y); void Make_Set(int x); void Link(int x, int y); void Graft(int r, int v); }; UFS::UFS(int size):n(size) { //必须从0开始 for(int i = 0; i <= n; i++) Make_Set(i); } void UFS::Make_Set(int x) { p[x] = x; rank[x] = 0; depth[x] = 0; } void UFS::clear() { for(int i = 0; i <= n; i++) Make_Set(i); } int UFS::Find_Set(int x) { int temp = x,sum = 0,ans; while(temp != p[temp]) { sum = sum + depth[temp]; temp = p[temp]; } ans = temp; while(x != ans) { sum -= depth[x]; depth[x] += sum; temp = p[x]; p[x] = ans; x = temp; } return ans; } void UFS::Union(int x, int y) { if(x == y) return ; Link(x, y); } void UFS::Link(int x, int y) { p[x] = y; depth[x] = 1; rank[y] += rank[x]; } void UFS::Graft(int r, int v) { int x = Find_Set(r); int y = Find_Set(v); if(x < y) Union(x, y); else Union(y, x); }
相关文章推荐
- 算法导论 第21章 用于不相交集合的数据结构
- 《算法导论》笔记 第21章 21.1 不相交集合上的操作
- 深度剖析WinPcap之(八)――打开与关闭适配器(21)
- CNTK API文档翻译(21)——深度卷积GAN处理MSIST数据基础
- 贪心算法(1)——算法导论(21)
- 算法导论笔记:21用于不相交集合的数据结构
- 深度剖析WinPcap之(八)——打开与关闭适配器(21)
- 《算法导论》笔记 第21章 21.2 不相交集合的链表表示
- [置顶] 【DL--21】深度学习57个专业术语
- 《模型汇总-21》深度学习背后的秘密:初学者指南-深度学习激活函数大全
- 算法导论习题解-第21章用于不相交集合的数据结构
- 算法导论代码 第21章 用于不相交集合的数据结构
- 该怎样迅速确定php多维数组的深度?
- 《算法导论》笔记 第21章 21.3 不相交集合森林
- 《算法导论》笔记 第22章 22.3 深度优先搜索
- 深度分析NandFlash—控制器参数TACLS、TWRPH0和TWRPH1的确定
- 《Python学习手册》学习笔记(21)之第21章模块:宏伟蓝图(关键词:编程语言/Python/模块)
- 算法导论第21章 不相交集合 链表表示
- 算法导论第十四章顺序统计量树确定一个元素的秩