当前位置:网站首页>Template series-union set
Template series-union set
2022-08-02 16:08:00 【Stubborn and cute】
并查集模板
1、初始化
void init{
for(int i=1;i<=n;i++){
fa[i]=i;//把结点iThe set number of is initialized to its own number
}
}
2、查找
版本一:
int find(int i){
while(i!=pi[i]){
i=pi[i]=pi[pi[i]];
}
return pi[i];
}
版本二:
int find(int x) {
if(x != fa[x]){
//当xNot equal to its dad(when it is an ancestor,it has no dad)
fa[x] = find(fa[x]);//Keep looking for his dad's dad
}
return fa[x];//返回祖先
}//查找
3、合并
void unity(int x, int y){
int r1 = find(x);//找到x的祖先
int r2 = find(y);//找到y的祖先
if(r1!=r2){
fa[r1] = r2;//Ancestors and ancestors become father and son(Anyone can be a father or a son)
}
}//合并
边栏推荐
猜你喜欢

字符数组/字符串数组|数组指针/指针数组/

Apache ShardingSphere 5.1.2 发布|全新驱动 API + 云原生部署,打造高性能数据网关...

implement tcp bbr on ns3 (在ns3上实现TCP BBR)

VirtualLab Fusion中的可视化设置

SkyWalking Agent数据采集和上报原理浅析

项目管理模块-项目权限功能开发

三方对接接口数据安全问题

光导布局设计工具

抽象类和接口 基本知识点复习

CDH (computational Diffie-Hellman) problem and its differences with discrete logarithm and DDH problems
随机推荐
HCIE学习记录——数通网络基础
golang的内存相关内容
win10无法识别蓝牙麦克风
Feign Client 超时时间配置不生效
内存和硬盘、磁盘的区别
【线程】 理解线程(并行)线程同步的处理(信号量,互斥锁,读写锁,条件变量)
锥形相位掩模的Talbot图像
在mininet中测试arp欺骗
使用1D-1D EPE的光波导布局设计工具
Redis 学习part one
Mobile copy constructor
【Solidity智能合约基础】-- 基础运算与底层位运算
光栅区域衍射级数和效率的规范
关于分布式的一些知识点
OpenPose 运行指令 ([email protected])
如何编辑VirtualLab Fusion结果的格式
Oauth2.0 authentication server adds verification code login method
【数组】查表法(闰年)
关于推荐系统的随想
Evaluate multipath BBR congestion control on ns3