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; }
查看全文
相关阅读:
tomcat解压版环境变量配置
十天冲刺(第二阶段)
十天冲刺(第二阶段)
十天冲刺
十天冲刺第二阶段()
十天冲刺(第二阶段)
十天冲刺(第二阶段)
十天冲刺(第一阶段)
十天冲刺(第一阶段)
十天冲刺(第一阶段)
原文地址:https://www.cnblogs.com/windmissing/p/2559888.html
最新文章
(课)阅读笔记2_1
利用python爬取王者荣耀英雄皮肤图片
Python——初识网络爬虫(网页爬取)
Python——pip的安装与使用
Python——集合
Python算法题(三)——经典函数题
Python算法题(二)——国际象棋棋盘(排列组合问题,最小的K个数)
Python算法题(一)——青蛙跳台阶
Python习题之快乐的数字
一行python能干什么?
热门文章
Python——用turtle画一个月饼
10.3总结
10.2总结
10.1总结
Jquery $.post $.get“异步”请求导致获取不到返回值
9.25总结
9.21总结
zTree插件setting配置详解
linux修改终端默认python版本
使用bat一键修改IP地址
Copyright © 2011-2022 走看看