当前位置:网站首页>模板系列-并查集
模板系列-并查集
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;//祖先和祖先结为父子(谁是父亲谁是儿子都可以)
}
}//合并
边栏推荐
- 5. Use RecyclerView to elegantly achieve waterfall effect
- STM32LL库使用——SPI通信
- GICv3/v4-软件概述
- DP4301无线收发SUB-1G芯片兼容CC1101智能家居
- General code for pytorch model to libtorch and onnx format
- FP7195转模拟恒流调光芯片在机器视觉光源的应用优势
- Win11没有本地用户和组怎么解决
- STM32LL库——USART中断接收不定长信息
- cmake配置libtorch报错Failed to compute shorthash for libnvrtc.so
- 蓝牙温度检测系统(基于BT08-B蓝牙模块)
猜你喜欢

FP7195大功率零压差全程无频闪调光DC-DC恒流芯片(兼容调光器:PWM调光,无极调光,0/1-10V调光)

FP6195耐压60V电流降压3.3V5V模块供电方案

win10任务栏不合并图标如何设置

Win11 keeps popping up User Account Control how to fix it

source /build/envsetup.sh和lunch)

Mysql connection error solution

FP6293电池升压5V-12V大电流2APWM模式升压方案

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

Win11电脑一段时间不操作就断网怎么解决

蓝牙温度检测系统(基于BT08-B蓝牙模块)
随机推荐
golang之GMP调度模型
Open the door to electricity "Circuit" (3): Talk about different resistance and conductance
FP6296锂电池升压 5V9V12V内置 MOS 大功率方案原理图
Makefile容易犯错的语法
FP7122降压恒流内置MOS耐压100V共正极阳极PWM调光方案原理图
Use libcurl to upload the image of Opencv Mat to the file server, based on two methods of post request and ftp protocol
What should I do if I install a solid-state drive in Win10 and still have obvious lags?
Use tencent cloud builds a personal blog
Win11系统找不到dll文件怎么修复
win10任务栏不合并图标如何设置
General code for pytorch model to libtorch and onnx format
Lightweight AlphaPose
casbin模型
Actual combat Meituan Nuxt +Vue family bucket, server-side rendering, mailbox verification, passport authentication service, map API reference, mongodb, redis and other technical points
pygame绘制弧线
DP1101兼容CC1101是SUB1GHz无线收发芯片应用于智能家居
Win10电脑需要安装杀毒软件吗?
Article pygame drag the implementation of the method
Mapreduce环境详细搭建和案例实现
MATLAB制作简易小动画入门详解