当前位置:网站首页>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)
}
}//合并
边栏推荐
猜你喜欢
分布式一致性协议-Raft
The relationship between base classes and derived classes [inheritance] / polymorphism and virtual functions / [inheritance and polymorphism] abstract classes and simple factories
基类和派生类的关系【继承】/多态和虚函数/【继承和多态】抽象类和简单工厂
idea同时修改相同单词
Vert.x web 接收请求时反序列化对象 Failed to decode 如何解决?
C#实现简单的计算器
泰伯效应的建模
【网络安全】学习笔记 --00
嵌入式学习硬件篇------初识ARM
如何编辑VirtualLab Fusion结果的格式
随机推荐
字符数组/字符串数组|数组指针/指针数组/
char array/string array|array pointer/pointer array/
unity 和C# 一些官方优化资料
The relationship between base classes and derived classes [inheritance] / polymorphism and virtual functions / [inheritance and polymorphism] abstract classes and simple factories
Google AdSense注册流程
【软件测试】性能测试理论
仿真结果的格式&定制
如何编辑VirtualLab Fusion结果的格式
消息队列的技术选型
OpenPose run command ([email protected])
Unity-Post Processing
Doubly linked list (normal iterators and const iterators)
【无标题】
mininet hosts talk to real internet
【软件测试】禅道的简要介绍
关于机组的部分知识点随笔
2021-03-12
项目管理模块-项目权限功能开发
【网络安全】学习笔记 --00
Qt | 定时器的使用 QTimer