跳到正文

算法·理论

提高级-算法总结--并查集1

fly_bamboo约 10 分钟
原文
文章信息与版本

文章信息

发布
2024年8月18日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 10 分钟
获赞
1

版本

  1. v1当前

(。・∀・)ノ゙嗨飞竹小课堂第3232课开课喽!
敌无了敌无了,今天我们学传说中古老的算法,并查集!
呜呜呜,又要回到树的噩梦了QWQ
我们假设现在有一个编程机构叫来追竹,有陈,杨,蒋 33 位老师
我们有 44 位学生,徐,李,刘,郭。
一开始,徐和李在蒋的班级里上课
刘和郭在陈的班级里上课,而杨的班级并没有学生
如下:

可等学生们到了省选时,陈和蒋的实力不足以再带着他们学习,于是带着他们一起到了杨的班级听课

那么这下我们就做了一次树的合并!
这,就是并查集的并!
他还可以查找,比如现在我问李:你的老师是谁?
他一定会说:杨
可缺点是…
你问他:你之前的老师是谁
李说:不知道…

这就是并查集的思想
那怎么用代码实现呢?
如果说合并,我们可以通过树的表示法之父亲表示法来表示, daida_i 表示 ii 的父亲是谁
但需要特判一下,只有当两人的老师不一样的时候,才进行合并
说到这,我们也要说一下初始化,一开始,我们把每个人的老师都设为自己
这就是并查集的并
为了方便,我们就直接把这个人接在另一个人的屁股后面,反正都把树压成一个数组了,还管什么顺序?

接着是查找,相信大家都想到了,递归,启动!
如果递归到了自己的老师是自己,那么就是遍历到了根节点,停止。

小试牛刀一波,模板题上!
模板并查集
easyeasy 吧,手切!
没有什么细节!

C++
#include<bits/stdc++.h>
using namespace std;
int da[1000005];
int n,m;
int find_tree(int x)
{
	if(x==da[x])
	{
		return x;
	}
	da[x]=find_tree(da[x]);
	return da[x];
}
void uni(int x,int y)
{
	int xx=find_tree(x);
	int yy=find_tree(y);
	if(xx!=yy)
	{
		da[xx]=yy;
	}
	return;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		da[i]=i;
	}
	for(int i=1;i<=m;i++)
	{
		int z,x,y;
		cin>>z>>x>>y;
		if(z==1)
		{
			uni(x,y);
		}
		else
		{
			if(find_tree(x)==find_tree(y))
			{
				cout<<"Y"<<endl;
			}
			else
			{
				cout<<"N"<<endl;
			}
		}
	}
	return 0;
}

一中校运会
也是十分的简单,我们直接把数组编号改为 mapmap stringstring 就行了,拿捏,模板!

C++
#include<bits/stdc++.h>
using namespace std;
map<string,string> da;
int n,m,k;
string find_tree(string x)
{
	if(da[x]==x)
	{
		return x;
	}
	da[x]=find_tree(da[x]);
	return da[x];
}
void uni(string x,string y)
{
	string xx=find_tree(x);
	string yy=find_tree(y);
	if(xx!=yy)
	{
		da[xx]=yy;
	}
	return;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		string s;
		cin>>s;
		da[s]=s;
	}
	for(int i=1;i<=m;i++)
	{
		string x,y;
		cin>>x>>y;
		uni(x,y);
	}
	cin>>k;
	for(int i=1;i<=k;i++)
	{
		string x,y;
		cin>>x>>y;
		if(find_tree(x)==find_tree(y))
		{
			cout<<"Yes."<<endl;
		}
		else cout<<"No."<<endl;
	}
	return 0;
}

最佳小组
好的也是十分的简单,跟上一题几乎没什么区别
我们把所有规定存起来,并查集完后再判断就可以了
可当你 submitsubmit 时,TLE!
当你关了同步流时,TLE!
怎么办呢?
事实证明 mapmap 太慢了,字符串拿来拿去,真的很浪费时间
所以,我们只能用字符串的编号进行并查集,于是有了一个新的 mapmap idid 用来表示这个字符串的编号,直接上代码!

C++
#include<bits/stdc++.h>
using namespace std;
map<string,int> id;
int cnt=0;
int da[300005];
struct node1
{
	string name1,name2;
}s1[300005];
struct node2
{
	string name1,name2;
}s2[300005];
int n,m,k;
int find_tree(int x)
{
	if(x==da[x])
	{
		return x;
	}
	da[x]=find_tree(da[x]);
	return da[x];
}
void uni(int x,int y)
{
	int xx=find_tree(x);
	int yy=find_tree(y);
	if(xx!=yy)
	{
		da[xx]=yy;
	}
	return;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>s1[i].name1>>s1[i].name2;
	}
	cin>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>s2[i].name1>>s2[i].name2;
	}
	cin>>k;
	int ans=0;
	for(int i=1;i<=k;i++)
	{
		string x,y,z;
		cin>>x>>y>>z;
		if(id[x]==0)
		{
			cnt++;
			id[x]=cnt;
			da[id[x]]=id[x];
		}
		if(id[y]==0)
		{
			cnt++;
			id[y]=cnt;
			da[id[y]]=id[x];
		}
		if(id[z]==0)
		{
			cnt++;
			id[z]=cnt;
			da[id[z]]=id[x];
		} 
		uni(id[x],id[y]);
		uni(id[y],id[z]);
	}
	for(int i=1;i<=n;i++)
	{
		int x=find_tree(id[s1[i].name1]);
		int y=find_tree(id[s1[i].name2]);
		if(x!=y)
		{
			ans++;
		}
	}
	for(int i=1;i<=m;i++)
	{
		int x=find_tree(id[s2[i].name1]);
		int y=find_tree(id[s2[i].name2]);
		if(x==y)
		{
			ans++;
		}
	}
	cout<<ans;
	return 0;
}

炸铁路
这道题稍微有一丢丢难
我们可以先枚举炸哪条铁路
可并查集怎么删边呢?
简单粗暴,重建一次!
可并查集怎么在递归的时候处理环呢?
在 uniuni 函数里不就判过了吗?
那么最后判断所有的点的父节点都是一样的不就行了吗? 简单粗暴,拿下!
注意:这道题很坑,输出的时候不仅要按从小到大排序,还要 xx < yy ,
kkk 太良心了!
代码:

C++
#include<bits/stdc++.h>
using namespace std;
int da[1000005];
struct node{
	int x,y;
}s[1000005];
int n,m;
bool cmp(node q,node h)
{
	if(q.x==h.x) return q.y<h.y;
	return q.x<h.x;
}
int find_tree(int x)
{
	if(x==da[x])
	{
		return x;
	}
	da[x]=find_tree(da[x]);
	return da[x];
}
void uni(int x,int y)
{
	int xx=find_tree(x);
	int yy=find_tree(y);
	if(xx!=yy)
	{
		da[xx]=yy;
	}
	return;
}
bool check(int x,int y)
{
	for(int i=1;i<=n;i++)
	{
		da[i]=i;
	}
	for(int i=1;i<=m;i++)
	{
		if(s[i].x==x&&s[i].y==y)
		{
			continue;
		}
		uni(s[i].x,s[i].y);
	}
	int u=find_tree(1);
	for(int i=2;i<=n;i++)
	{
		if(u!=find_tree(i))
		{
			return false;
		}
	}
	return true;
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>s[i].x>>s[i].y;
		if(s[i].x>s[i].y)
		{
			swap(s[i].x,s[i].y);
		}
	}
	sort(s+1,s+1+m,cmp);
	for(int i=1;i<=m;i++)
	{
		if(check(s[i].x,s[i].y)==false)
		{
			cout<<s[i].x<<" "<<s[i].y<<endl;
		}
	}
	return 0;
}

嗯,今天的并查集也是直接搞定
冰茶几
下课!
ヾ( ̄▽ ̄)ByeBye

原文