并查集笔记
动态维护若干个不相关的集合:查询、合并
- 最普通的并查集
#include <bits/stdc++.h>
using namespace std;
# define N 1000//元素的上限个数
int father[N];//存储每个元素父亲的下标
void init(int n){
//最开始的时候,每一个元素单独构建一个集合
for(int i=0;i<n;i++)
father[i]=i; //每个元素都是一棵树的根
}
int find(int x){
if(x!=father[x]){//若x不为树的根
//递归向上寻找
return find(father[x]);
}
return father[x]; //x就是树的根
}
void union(int x,int y){
//合并两棵树,找到y的祖先,把y的祖先的父亲设为x的祖先
father[find(y)]=find(x);
}
- 路径压缩
#include <bits/stdc++.h>
using namespace std;
# define N 1000//元素的上限个数
int father[N];//存储每个元素父亲的下标
int height[N];//存储某个根的树的高度
void init(int n){
//最开始的时候,每一个元素单独构建一个集合
for(int i=0;i<n;i++){
father[i]=i; //每个元素都是一棵树的根
height[i]=1;//每个树初始高度均为1
}
}
int find(int x){
if(x!=father[x]){//若x不为树的根
//递归向上寻找
//find路径压缩,找到祖先后先不返回 ,而是设为自己的新父亲
father[x]=find(father[x]);
}
return father[x]; //x就是树的根
}
void union(int x,int y){
//合并两棵树,找到y的祖先,把y的祖先的父亲设为x的祖先
//father[find(y)]=find(x);
//union路径压缩:大树合并小树
x=find(x);//找到树的根位置
y=find(y);
//小树的祖先的父亲设为大树的祖先
if(height[x]<height[y]);
father[x]=y;
else if(height[x]>height[y])
father[y]=x;
else {//如果大树和小树高度相同,则随意合并,但新的树的高度加一
father[y]=x;
height[x]++;
}
}