并查集

一、模板题

How Many Tables

How Many Tables

#include <iostream>
using namespace std;
const int MAXN = 1005;
int t, n, m;
int fa[MAXN];
void init() {
	for (int i = 1; i <= n; i++) {
		fa[i] = i;
	}
}
int get(int x) {
	if (x == fa[x]) return x;
	//路径压缩
	return fa[x] = get(fa[x]);
}
void merge(int x, int y) {
	fa[get(x)] = get(y);
}
int main() {
	cin >> t;
	while (t--) {
		cin >> n >> m;
		init();
		for (int i = 1; i <= m; i++) {
			int a, b;
			cin >> a >> b;
			merge(a, b);
		}
		int ans = 0;
		for (int i = 1; i <= n; i++) {
			if (fa[i] == i) ans++;
		}
		cout << ans << endl;
	}

}

二、附带集合大小的并查集

More is better

More is better

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 1e7 + 5;
int fa[maxn];
int size[maxn];
int ans;
void init() {
	for (int i = 1; i <= 1e7; i++) {
		fa[i] = i;
		//size数组集合大小
		size[i] = 1;
	}
}
int get(int x) {
	if (x == fa[x]) return x;
	return fa[x] = get(fa[x]);
}
void merge(int x, int y) {
	x = get(x);
	y = get(y);
	//注意x和y要不等
	if (x != y) {
		fa[x] = y;
		//x集合并到y上,y集合大小要加上x集合大小
		size[y] += size[x];
	}

}
int main() {
	int n;
	while (~scanf("%d", &n)) {
		init();
		ans = 0;
		for (int i = 1; i <= n; i++) {
			int a, b;
			scanf("%d%d", &a, &b);
			merge(a, b);
		}
		for (int i = 1; i <= 1e7; i++) {
			if (size[i] > ans) {
				ans = size[i];
			}
		}
		printf("%d\n", ans);
	}
}

三、边带权并查集

Dragon Balls

Dragon Balls

#include <iostream>
using namespace std;
const int maxn = 10005;
int t,n,q;
int root[maxn];
int size[maxn];
int d[maxn];
void init() {
	for(int i=1; i<=n; i++) {
		root[i]=i;
		size[i]=1;
		d[i]=0;
	}
}
int get(int x) {
	if(x==root[x]) return x;
	int r=root[x];
	root[x]=get(root[x]);
	d[x]+=d[r];
	return root[x];
}
void merge(int x,int y) {
	x=get(x);
	y=get(y);
	if(x != y) {
		root[x]=y;
		size[y]+=size[x];
		d[x]=1;
	}
}
int main() {
	scanf("%d", &t);
	for(int z=1; z<=t; z++) {
		scanf("%d%d", &n, &q);
		getchar();
		init();
		char ch;
		int flag=0;
		for(int i=1; i<=q; i++) {
			scanf("%c", &ch);
			if(ch=='T') {
				int a,b;
				scanf("%d%d", &a, &b);
				getchar();
				merge(a,b);
			}
			else {
				int a;
				scanf("%d", &a);
				getchar();
				if(flag==0) {
					printf("Case %d:\n", z);
					flag=1;
				}
				int r=get(a);
				printf("%d %d %d\n", r, size[r], d[a]);
			}
		}
	}
} 

四、扩展域并查集

食物链

食物链

#include <iostream>
using namespace std;
const int maxn = 3*50000+5;
int fa[maxn];
void init() {
	for(int i=1; i<=3*50000; i++) {
		fa[i]=i;
	}
}
int get(int x) {
	if(fa[x]==x) return x;
	return fa[x]=get(fa[x]);
}
void merge(int x,int y) {
	fa[get(x)]=get(y);
}
bool judge(int x, int y) {
	if(get(x)==get(y)) return true;
	return false;
}
int main() {
	int n,k;
	scanf("%d%d", &n, &k);
	init();
	int ans=0;
	for(int i=1; i<=k; i++) {
		int d,x,y;
		scanf("%d%d%d", &d, &x, &y);
		if( x<1 || x>n || y<1 || y>n) {
			ans++;
			continue;
		}
		if(d==1) {
			if(judge(x,y+n) || judge(x,y+2*n)) ans++;
			else {
				merge(x,y);
				merge(x+n,y+n);
				merge(x+2*n,y+2*n);				
			}
		}
		else {
			if(judge(x,y) || judge(x,y+n) || x==y) ans++;
			else {
				merge(x,y+2*n);
				merge(x+n, y);
				merge(x+2*n, y+n);				
			}

		}
	}
	printf("%d\n", ans);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值