zoukankan
html css js c++ java
并查集
/* UnionFindSet.h 并查集,非递归方法,含路径压缩,数组从0开始 合并时,前者合并入后者,不区分大小 */ #include <iostream> using namespace std; #define MAXN 30005 class UFS { public: int n; int father[MAXN+1];//集合根结点 int rank[MAXN+1]; //集合中点的个数 int depth[MAXN+1]; //每个结点改变一次所属的集合,增加一些值 public: UFS(int size = MAXN); void clear(); int Find(int x); //a并入b中,不区分大小 //value表示:如果a并入b中,a中r所有元素能获得的值 void Union(int a, int b, int value = 0); }; UFS::UFS(int size):n(size) { //必须从0开始 for(int i = 0; i <= n; i++) { father[i] = i; rank[i] = 1; depth[i] = 0; } } void UFS::clear() { for(int i = 0; i <= n; i++) { father[i] = i; rank[i] = 1; depth[i] = 0; } } int UFS::Find(int x) { int temp = x,sum = 0,ans; while(temp != father[temp]) { sum = sum + depth[temp]; temp = father[temp]; } ans = temp; while(x != ans) { sum -= depth[x]; depth[x] += sum; temp = father[x]; father[x] = ans; x = temp; } return ans; } void UFS::Union(int a, int b,int value) { int x = Find(a); int y = Find(b); if(x == y) return ; if(value >= 0) depth[x] = value; else depth[x] = rank[y]; rank[y] += rank[x]; father[x] = y; }
查看全文
相关阅读:
IBatisNet不常用到的配置(Dao.config ConnectionTimeout),居然不起作用(前辈留给我们的坑)
随机数 字母 数字
证书文件(pfx)读取时报 “指定的网络密码不正确”
SQL多结果集导出Excel
Uva514
PAT乙级1012
栈-41
位运算-89
PAT乙级1028
PAT乙级1029
原文地址:https://www.cnblogs.com/windmissing/p/2559888.html
最新文章
1338. 停车困境
1343. 两字符串和
1209. 构造矩形(经典)
697. 判断是否为平方数之和(经典)
1206. 下一个更大的数 I
windows服务程序的编写
ubuntu16.04如何安装多个版本的CUDA
ubuntu16.04如何安装多个版本的CUDA
Waiting for another flutter command to release the startup lock...
Waiting for another flutter command to release the startup lock...
热门文章
9.9元学生云服务器学生专享及常见问题
《将博客搬至CSDN》
《将博客搬至CSDN》
MySQL索引的类型
MySQL索引的类型
tesseract .net 中使用历程
ASP.NET IIS System.UnauthorizedAccessException: 对路径“C:......xls”的访问被拒绝。
VS2015 中使用 MVC4
NuGet 安装EntityFramework5 历程
正则判断 中横线(-) 两边是否输入的是正整数 和 正浮点数 或者 只输入一个正整或浮点数
Copyright © 2011-2022 走看看