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; }
查看全文
相关阅读:
正则表达式尽量写的精简,提高运行效率
IndexError:string index out of range
Python3 字符编码
python3字符编码错误
pip
正则贪婪和非贪婪
[Ss]+ 可以匹配多行html,最常用的还是.*?
正则,分组,字符集,使用场景
使用jodis连接codis的时候报异常:Exception in thread "main" redis.clients.jedis.exceptions.JedisException: Proxy list empty
codis 的dashboard服务无法启动 提示pid已经运行
原文地址:https://www.cnblogs.com/windmissing/p/2559888.html
最新文章
Download google drive public shared file in terminal
hadoop用户和权限
spark MLlib的 pipeline方式
ycache中redis主备功能设计及使用说明
hive优化
笔记:YSmart: Yet Another SQL-to-MapReduce Translator
笔记:Hive的主要技术改进(Major Technical Advancements in Apache Hive)
cache magic对pms模块的cache访问模式分析结果
ionic2如何升级到最新版本、配置开发环境
input=text数字问题
热门文章
sencha touch的开源插件和例子
闲聊——浅谈前端js模块化演变
号称21世纪的编辑器Atom
常用的sublime text 3插件(很爽哦)
AndroidAnnotations(Code Diet)android快速开发框架
Eclipse导入到最新版Android Studio详解
大话css之display的Block未解之谜(一)
常见css垂直自适应布局(css解决方法)
pycharm快捷键及一些常用设置
用PDB库调试Python程序
Copyright © 2011-2022 走看看