3.1并查集

3.1并查集

知识点

  1. 定义:O(1)实现以下两种操作
find(x);//查找一个元素属于那个集合
Merge(x,y);//合并两个集合

路径压缩和按秩合并

  1. 路径压缩:
    在find()时将每个元素的 f a x fa_x fax设为代表元素
inline int find(int x){
	return fa[x]==x?x:fa[x]=find(fa[x]);
}
  1. 按秩合并:在Merge()时将最大深度较小的集合并到大集合上
inline void Merge(int x,int y){
	x=find(x),y=find(y);
	if(si[x]>si[y])	swap(x,y);
	fa[x]=y;
	si[y]+=si[x];
  1. 例题1:【模版】P3367[模板]并查集
#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 1e4+100;
int fa[N],si[N],n,m;

inline int find(int x){
	if(fa[x]==x){
		return x;
	}
	return fa[x]=find(fa[x]);
	//另一种高级写法
 	//return fa[x]==x?x:fa[x]=find(fa[x]);
}

inline void merge(int x,int y){
	x=find(x),y=find(y);
	if(si[x]>si[y])	swap(x,y);
	fa[x]=y;
	si[y]+=si[x];
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);
#endif
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		fa[i]=i;
		si[i]=1;
	}
	for(int i=1;i<=m;i++){
		int z,x,y;
		scanf("%d%d%d",&z,&x,&y);
		if(z==1){
			merge(x,y);
		}else{
			if(find(x)==find(y)){
				printf("Y\n");
			}else{
				printf("N\n");
			}
		}
	}
	return 0;
}

例题

例题2:P1196 [NOI2002] 银河英雄传说

  • 扩展用法:带边权的并查集
  1. 思路:新增dis[]数组,tot[]数组

dis[x]:表示节点x到它的父亲之间的节点数(不包括x)

tot[x]:表示以x为代表元素的集合中有多少元素(当x不是代表元素时 t o t x tot_x totx无意义)

1.1 查询:
先判断i,j是否在同一集合内,若在,答案为i,j到它们的代表元素之间的节点数相减再-1,否则为-1,显然,-1的原因是相减时不会减去i,j中dis更小的那一个点

inline int find(int x){
	if(fa[x]==x){
		return x;
	}
	int fath = find(fa[x]);
	dis[x]+=dis[fa[x]];
	return fa[x]=fath; 
}

else if(c=='C'){
	if(find(i)==find(j)){
     	printf("%d\n",abs(dis[i]-dis[j])-1);
	}else{
		puts("-1");
	}
}
  • 对于路径压缩,因为将 f a x fa_x fax更改为集合代表元素,所以更新 d i s x dis_x disx为:
dis[x]+=dis[fa[x]];

1.2 合并:记i、j的代表元素为x、y,令 f a x = y fa_x=y fax=y,只需更新 d i s x dis_x disx,所以用 t o t x tot_x totx更新 d i s x dis_x disx,再将 t o t y + = t o t x tot_y+=tot_x toty+=totx即可

inline void Merge(int x,int y){
	int xx=find(x),yy=find(y);
//	if(si[xx]>si[yy])	swap(xx,yy);
	dis[xx]=tot[yy];
	tot[yy]+=tot[xx];//更新tot,dis
	tot[xx]=0;
	fa[xx]=yy; 
//	si[yy]+=si[xx];
}
  • 注意:本题貌似不能使用按秩合并
  1. 代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;

const int N = 3e4+5;
int fa[N],si[N],dis[N],tot[N],t;

inline int find(int x){
	if(fa[x]==x){
		return x;
	}
	int fath = find(fa[x]);
	dis[x]+=dis[fa[x]];
	return fa[x]=fath; 
}

inline void Merge(int x,int y){
	int xx=find(x),yy=find(y);
//	if(si[xx]>si[yy])	swap(xx,yy);
	dis[xx]=tot[yy];
	tot[yy]+=tot[xx];//更新tot,dis
	tot[xx]=0;
	fa[xx]=yy; 
//	si[yy]+=si[xx];
}

inline void init(){
	for(int i=1;i<=30000;i++){
		fa[i]=i;
//		si[i]=1;
//		dis[i]=1;
		tot[i]=1;
	}
}

int main(){
//	freopen("a.in","r",stdin);
//	freopen("a.out","w",stdout);
	scanf("%d",&t);
	init();
	while(t--){
		char c;
		int i,j;
		cin>>c;
		scanf("%d%d",&i,&j);
		if(c=='M')	Merge(i,j);
		else if(c=='C'){
			if(find(i)==find(j)){
				printf("%d\n",abs(dis[i]-dis[j])-1);
			}else{
				puts("-1");
			}
		}
	}
	return 0;
}

例题3: 食物链洛谷P2024 [NOI2001] 食物链

  • 本题考察:状态并查集
  1. 思路:
  • 状态并查集经典题
  • 除了每个动物本身之外,再额外维护 2n 个点,表示每个动物的天敌与猎物。
  • 那么如果两只动物同类,那就把它们本身合并,天敌合并,猎物合并。
  • 如果X吃Y,就把的猎物和Y合并,X的天敌和Y的猎物合并,X和Y的天敌合并。
  • 矛盾也就自然好判了,声称同类时发现它们有猎物关系,或者声称猎物关系时发现是同类/反向猎物关系,则矛盾。
  • 注意分清fa数组中各个区间代表的含义
  1. 代码
#include<bits/stdc++.h>
using namespace std;

const int N = 5e4+10;
int fa[3*N],n,k,ans=0;
//fa 1-n是动物,n+1-2*n为天敌,2n+1-3n为猎物 

inline int find(int x){
	return fa[x]==x?x:fa[x]=find(fa[x]);
}

inline void Merge(int x,int y){
	x=find(x),y=find(y);
	if(x==y)	return ;
	else fa[x]=y;
}

inline bool check(int x,int y){
	return find(x)==find(y);
}

int main()
{
//	freopen("a.in","r",stdin);
//	freopen("a.out","w",stdout);
	
	scanf("%d%d",&n,&k);
	for(int i=1;i<=3*n;i++){
		fa[i]=i;
	}
	
	for(int i=1;i<=k;i++){
		int a,b,c;
		scanf("%d%d%d",&c,&a,&b);
		if(a>n||b>n){
			ans++;
		}
		else if(c==1){
			if(check(a,b+n)||check(a,b+(n<<1))){
				ans++;
			}
			else{
				Merge(a,b);
				Merge(a+n,b+n);
				Merge(a+(n<<1),b+(n<<1));
			}
		}else if(c==2){
			if(check(a,b)||check(a,b+(n<<1))){
				ans++;
			}
			else {
				Merge(a,b+n);//a与b的天敌是同类 
				Merge(a+(n<<1),b);//a的猎物与b是同类 
				Merge(a+n,b+(n<<1));//a的天敌与b的猎物是同类 
			}
		}
	}
	
	printf("%d",ans);
	return 0; 
}

例题4:超市购物

  • 考察:贪心+并查集
  1. 贪心策略:优先选取收益最大的商品售卖以取得最大总收益
  2. 实现:
  • 按收益降序排序,对第i件商品,若期限内有一天没售卖商品,则在这一天售卖,且保证时间最晚
  • 使用并查集储存每一天之前没有售卖东西的最后一天,每次O(1)查询可售卖日期,售卖后将这一天与前一天合并
  • 连续输入:while(scanf("%d",&n){...}
  1. 代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e4+5;
ll fa[N],siz[N],ans=0,maxn=0;
int n;
struct box{
	ll p,d;
	bool operator < (const box& b)const{
		return p>b.p;
	} 
}box[N];

inline ll find(ll x){
	return fa[x]==x?x:fa[x]=find(fa[x]);
}

inline void Merge(ll x,ll y){
	ll xx=find(x),yy=find(y);
//	if(siz[xx]>siz[yy])	swap(xx,yy);
	fa[xx]=yy;
//	siz[yy]+=siz[xx];
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);
#endif
	while(scanf("%d",&n)){
		maxn=0,ans=0;
		for(int i=1;i<=n;i++){
			int a,b;
			scanf("%d%d",&a,&b);
//			scanf("%lld%lld",&box[i].p,&box[i].d);
//			cerr<<a<<' '<<b<<endl;
			box[i].p=a,box[i].d=b;
			maxn=max(maxn,box[i].d);
		}
		for(ll i=1;i<=maxn;i++){
			fa[i]=i;
//			siz[i]=1;
		}
		sort(box+1,box+1+n);
		for(int i=1;i<=n;i++){
			int xx=find(box[i].d);
			if(xx){
				ans+=box[i].p;
				Merge(xx,find(xx-1));
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

例题5:P2700 逐个击破

  1. 思路:删边不好考虑,那么就考虑添边
  • 该问题可以转化为:在地图中添加尽可能多(边权大)的边,且保证连上的边为以连边或不能将两个被占领的城市被连通
  • 贪心策略显然:将边按边权降序排列,依次判断该边是否可以连上,记录使用边的边权总和,最后用总边权和与其相减即为答案
  1. 问题:使用朴素算法判断边是否合法,时间复杂度最坏为 O ( m 2 ) O(m^2) O(m2),无法通过
  2. 解决:使用并查集维护连通块
  • 使一个集合的代表元素为该连通块中的特殊点,若两点代表元素相同,证明两点连通
  • 判断时只需判断两点的 f a x fa_x fax是否相同,相同可连,若不相同但其中 f a x fa_x fax为特殊点的点数不超过1个则也可连
  • 判断是否为特殊点,只需维护一个l[]数组,将下标为特殊点的值设为1,判断时判断是否为1即可
  1. 注意事项:
  • 要开long long否则会WA
  • 本题下标从0开始,输入后要将点的序号+1
  1. 代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
const int M = 5e5+10;
#define int long long
int n,m,k,l[N],fa[N],ans=0;

struct edge{
	int u,v,w;
	bool operator < (const edge& b)const{
		return w>b.w;
	}
}edge[M];

inline int find(int x){
	return fa[x]==x?x:fa[x]=find(fa[x]);
}

inline void Merge(int x,int y){
	int xx=find(x),yy=find(y);
	fa[xx]=y;
}

inline void init(){
	for(int i=1;i<=n;i++){
		fa[i]=i;
	}
}

signed main(){
#ifndef ONLINE_JUDGE
	freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);
#endif
	scanf("%lld%lld",&n,&k);
	m=n-1;
	init();
	for(int i=1;i<=k;i++){
		int x;
		scanf("%lld",&x);
		l[x+1]=1;
	}
	for(int i=1;i<=m;i++){
		scanf("%lld%lld%lld",&edge[i].u,&edge[i].v,&edge[i].w);
		edge[i].u++,edge[i].v++;
		ans+=edge[i].w;
	}
	sort(edge+1,edge+1+m);
	for(int i=1;i<=m;i++){
		int uu=find(edge[i].u),vv=find(edge[i].v);
		if(uu==vv){
			ans-=edge[i].w;
		}
		else if(!l[uu]||!l[vv]){
			ans-=edge[i].w;
			l[uu]?fa[vv]=uu:fa[uu]=vv;
		}
	}
	printf("%lld",ans);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值