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; }
查看全文
相关阅读:
Delphi2007下cxComboBox乱码.
DelphiIOCP 学习笔记<六>=====IO内存池和扩展套接字(ClientContext)
DelphiIOCP学习笔记<三>====工作线程和Listener
DelphiIOCP学习笔记<二>====IOCP基本函数介绍和理解
word比较两个文件
SQLite3 简记
DXP_protel2004_原理图设计基础_新建和添加原理图库文件_元件编辑范例
DXP_protel2004_原理图设计基础_新建和添加原理图库文件
网页表格颜色搭配
excle密码破解
原文地址:https://www.cnblogs.com/windmissing/p/2559888.html
最新文章
为什么程序员不擅长估算时间
影响Web开发者职业发展的六大障碍
JQuery EasyUI Message
Exception from HRESULT: 0x8007000B 异常
JQuery EasyUI Windows
My interested stuff(20080622)
My interested stuff(20080620)
(原创)SQL Server中几种特殊的“字符串到列表”的处理函数
10 Tools Which I Still Use, Even I Have VSTS 2008
再次发布SQL Prompt 3.8的新的patch,解决了不能格式化TSQL的问题
热门文章
My interested stuff(20080628)
VS 2008 Tracepoints – Debug output without touching your code (Say goodbye to Debug.Write ?)
My interested stuff(20080618)
Map SQL Server Profiler EventClass ID to its name in a saved trace table
设置不定宽高的元素垂直水平居中 Y
DelphiIOCP学习笔记<一>====从零开始学习IOCP
sql删除临时表
DelphiIOCP学习笔记<五>===测试IOCP工作线程处理队列顺序
DelphiIOCP学习笔记<四>===小结<IOCP的简单例子>
sql 备份到网上邻居上
Copyright © 2011-2022 走看看