当前位置:网站首页>模板系列-并查集
模板系列-并查集
2022-08-02 14:10:00 【老顽固也可爱】
并查集模板
1、初始化
void init{
for(int i=1;i<=n;i++){
fa[i]=i;//把结点i的集合号初始化为其自身编号
}
}
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]){
//当x不等于它的爸爸时(当它是祖先时,它没有爸爸)
fa[x] = find(fa[x]);//继续找他的爸爸的爸爸
}
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;//祖先和祖先结为父子(谁是父亲谁是儿子都可以)
}
}//合并
边栏推荐
- pygame draw arc
- FP7195芯片PWM转模拟调光至0.1%低亮度时恒流一致性的控制原理
- Win11 keeps popping up User Account Control how to fix it
- Mysql connection error solution
- 使用libcurl将Opencv Mat的图像上传到文件服务器,基于post请求和ftp协议两种方法
- In-depth understanding of Golang's Map
- win10无法直接用照片查看器打开图片怎么办
- 使用npx -p @storybook/cli sb init安装失败,手把手搭建专属的storybook
- golang之GMP调度模型
- 总结计算机网络超全面试题
猜你喜欢

Open the door of power and electricity "Circuit" (2): Power Calculation and Judgment

镜像法求解接地导体空腔电势分布问题

How to set the win10 taskbar does not merge icons

刷卡芯片CI520可直接PIN对PIN替换CV520支持SPI通讯接口

Win10 Settings screen out from lack of sleep?Win10 set the method that never sleep

win10怎么设置不睡眠熄屏?win10设置永不睡眠的方法

Win11系统找不到dll文件怎么修复

Mysql之MVCC

GMP scheduling model of golang

A clean start Windows 7?How to load only the basic service start Windows 7 system
随机推荐
STM32LL库——USART中断接收不定长信息
Mysql连接错误解决
define #使用
总结计算机网络超全面试题
如何用硬币模拟1/3的概率,以及任意概率?
Binder机制(中篇)
The overlapping effect of the two surfaceviews is similar to the video and handout practice in the live effect
FP7195大功率零压差全程无频闪调光DC-DC恒流芯片(兼容调光器:PWM调光,无极调光,0/1-10V调光)
DP4056电源保护芯片锂电池pin对pinTP4056
推开机电的大门《电路》(二):功率计算与判断
[STM32 Learning 1] Basic knowledge and concepts are clear
DP1332E刷卡芯片支持NFC内置mcu智能楼宇/终端poss机/智能门锁
小T成长记-网络篇-1-什么是网络?
基于51单片机和物联网的智能家居系统(ESP8266物联网模块)
Spark及相关生态组件安装配置——快速回忆
2.4G无线小模块CI24R1超低成本
MATLAB图形加标注的基本方法入门简介
Binder ServiceManager解析
Mysql lock
Win11电脑一段时间不操作就断网怎么解决