跳到正文

个人记录

并查集(总结)

adolphshi约 1 分钟
原文
文章信息与版本

文章信息

发布
2022年1月19日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 1 分钟
获赞
1

版本

  1. v1当前

大概是这样

下面是主要部分的代码实现

查找+路径压缩

C++
int b[N];//b[]数组为并查集数组
int find(int x)
{
	if(b[x]==x)//递归出口
	{
		return x;
	}
	return b[x]=find(b[x]);//此处为路径压缩(递归)
}

合并

C++
void uni(int x,int y)
{
	int fx,fy;
	fx=find(x);//查找x的祖先
	fy=find(y);//查找y的祖先
	if(fx!=fy)//判断是否相等
	{
		b[fy]=fx;
	} 
}
原文