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; }
查看全文
相关阅读:
DynamicLibrary动态查询需要的一个CS 文件下载
指定的命名连接在配置中找不到、非计划用于 EntityClient 提供程序或者无效
ACCESS的一个相对好用的数据库连接字符串
ASP.NET 入门 博客园文章 索引篇
c# 最小化到系统栏,时钟,随机语句,程序发布 读书笔记本 (三)
poj 2817 WordStack (状态dp)
hdu 4380 Farmer Greedy (计算几何 2012 MultiUniversity Training Contest 9 )
hdu 4353 Finding Mine (计算几何 2012 MultiUniversity Training Contest 6 )
poj 3735 Training little cats (矩阵快速幂)
hdu 4374 One hundred layer (dp +单调队列 2012 MultiUniversity Training Contest 8 )
原文地址:https://www.cnblogs.com/windmissing/p/2559888.html
最新文章
屏幕捕获程序
今天被DropDownList难住了
pthon自动化之路编写登录接口
nginx location 正则匹配
centos7 php性能调优
linux AB web 性能测试工具
Apache的Order Allow,Deny 详解
SQLServer的数据类型
小球弹跳
Android游戏开发的开源框架
热门文章
Eclipse中常用的快捷键
如何把docx转成doc
游戏音效下载网站大全
Android APK反编译详解(附图)
visual studio如何用低版本打开高版本项目
读取WebConfig中的连接字符串
泛型通用动态查询(LinQ+Ajax)
SQL 2005 中的 "级联"
自写的 c# 锚点,书签,行高 读书笔记本 (二)
读博客笔记一: smart is the new sexy
Copyright © 2011-2022 走看看