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; }
查看全文
相关阅读:
C# 字符串转为DateTime类型
多线程的注意事项
linux 安装中文支持
发布网站遇到的坑
配置iis支持json解析,配置ssi
SEO之图片优化
SEO之面包屑导航
SEO之HTML标签
SEO之优化代码
SEO之网站内部结构优化
原文地址:https://www.cnblogs.com/windmissing/p/2559888.html
最新文章
购物车
正则校验
单向链表
Rectangle Area
第一次刷题,一不小心这个题好像看过,题目为Two Sum
Java Swing中有关事件机制
android笔记--Cannot execute task: the task has already been executed (a task can be executed only once)
android笔记--解决SkImageDecoder::Factory returned null
android笔记--Sqlite使用
Markdown使用
热门文章
sorted() 详解
python 练手程序 文件备份
转 神经网络
转 机器学习ex1
matlab fprintf输出矩阵
迭代器_装饰器_生成器_推导式
装饰器
优化读取纯真IP数据库QQWry.dat获取地区信息
webapi xml序列化删除<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">标签
小程序的坑
Copyright © 2011-2022 走看看