当前位置:网站首页>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)
}
}//合并
边栏推荐
猜你喜欢
随机推荐
golang内存相关文章-收集
[Inter-process communication]: pipe communication/named/unnamed
Redis 学习part one
转行软件测试,从零收入到月薪过万,人生迎来新转折
内存和硬盘、磁盘的区别
HCIE学习记录——OSI参考模型
光学好书推荐
Doubly linked list (normal iterators and const iterators)
Google AdSense注册流程
Unity Line-Renderer
如何编辑VirtualLab Fusion结果的格式
Unity-3D数学
关于分布式的一些知识点
【软件测试】进阶篇
基类和派生类的关系【继承】/多态和虚函数/【继承和多态】抽象类和简单工厂
灵活的区域定义
OpenPose Basic Philosophy
系统性能和TCP/UDP网络优化-学习大杂烩
光波导k域布局可视化(“神奇的圆环”)
EastWave:垂直腔表面激光器









