当前位置:网站首页>模板系列-并查集
模板系列-并查集
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;//祖先和祖先结为父子(谁是父亲谁是儿子都可以)
}
}//合并
边栏推荐
猜你喜欢

FP7126降压恒流65536级高辉无频闪调光共阳极舞台灯RGB驱动方案

FP7128内置MOS降压恒流调光深度0.01%高辉共阳调光方案

Use tencent cloud builds a personal blog

Binder机制(中篇)

IPV4和IPV6是什么?

What is Win10 God Mode for?How to enable God Mode in Windows 10?

A clean start Windows 7?How to load only the basic service start Windows 7 system

FP7122降压恒流内置MOS耐压100V共正极阳极PWM调光方案原理图

推开机电的大门《电路》(二):功率计算与判断

推开机电的大门《电路》(一):电压,电流,参考方向
随机推荐
The overlapping effect of the two surfaceviews is similar to the video and handout practice in the live effect
设备驱动框架简介
Win10 computer can't read U disk?Don't recognize U disk how to solve?
Failed to install using npx -p @storybook/cli sb init, build a dedicated storybook by hand
casbin模型
基于51单片机和物联网的智能家居系统(ESP8266物联网模块)
推开机电的大门《电路》(三):说说不一样的电阻与电导
DP1332E刷卡芯片支持NFC内置mcu智能楼宇/终端poss机/智能门锁
Win10系统设置application identity自动提示拒绝访问怎么办
【我的电赛日记(二)】ADF4351锁相环模块
2021-10-14
利用plot_surface命令绘制复杂曲面入门详解
Mysql之MVCC
Please make sure you have the correct access rights and the repository exists.问题解决
Win11电脑一段时间不操作就断网怎么解决
Binder ServiceManager解析
CI24R1小模块2.4G收发模块无线通信低成本兼容si24r1/XN297超低功耗
Open the door to electricity "Circuit" (3): Talk about different resistance and conductance
How to update Win11 sound card driver?Win11 sound card driver update method
TCP三次握手、四次挥手