P3367 【模板】并查集

【模板】并查集

题目描述

如题,现在有一个并查集,你需要完成合并和查询操作。

输入格式

第一行包含两个整数 N,MN,MN,M ,表示共有 NNN 个元素和 MMM 个操作。

接下来 MMM 行,每行包含三个整数 Zi,Xi,YiZ_i,X_i,Y_iZi,Xi,Yi

Zi=1Z_i=1Zi=1 时,将 XiX_iXiYiY_iYi 所在的集合合并。

Zi=2Z_i=2Zi=2 时,输出 XiX_iXiYiY_iYi 是否在同一集合内,是的输出
Y ;否则输出 N

输出格式

对于每一个 Zi=2Z_i=2Zi=2 的操作,都有一行输出,每行包含一个大写字母,为 Y 或者 N

样例 #1

样例输入 #1

4 7
2 1 2
1 1 2
2 1 2
1 3 4
2 1 4
1 2 3
2 1 4

样例输出 #1

N
Y
N
Y

提示

对于 30%30\%30% 的数据,N≤10N \le 10N10M≤20M \le 20M20

对于 70%70\%70% 的数据,N≤100N \le 100N100M≤103M \le 10^3M103

对于 100%100\%100% 的数据,1≤N≤1041\le N \le 10^41N1041≤M≤2×1051\le M \le 2\times 10^51M2×1051≤Xi,Yi≤N1 \le X_i, Y_i \le N1Xi,YiNZi∈{1,2}Z_i \in \{ 1, 2 \}Zi{1,2}

一道并查集的模版
我们需要一个数组 f ,他存储的是第i的点的祖先(就是可以查到的最早的节点)
在程序初始化的时候,因为现在还没有进行任何操作,所以 f1f_1f1~fnf_nfn的值都是自己
找祖先我们写一个函数find

int find(int x){
	if(f[x]!=x)f[x]=find(f[x]);//当他的祖先不是自己,就往上找
	return f[x];//返回查找的结果
}

接下来来看m次操作
先来说zzz为1的时候,把xxxyyy合并的时候,其实就可以理解为将xxx的祖先的祖先设为yyy的祖先

f[find(x)]=find(y);

zzz为2是,就查一下xxxyyy的祖先是否一样就行了

#include<bits/stdc++.h>
//#define int long long
//#define int unsigned long long
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef unsigned long long ull;
//const int mod=
const int N=1e6+5;
const int M=1e3+5;
int f[N];
int n,m;
int T;
int find(int x){
	if(f[x]!=x)f[x]=find(f[x]);
	return f[x];
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cin>>n>>m;
	for(int i=1;i<=n;i++)f[i]=i;
	while(m--){
		int op;
		cin>>op;
		int x,y;
		cin>>x>>y;
		if(op==1){
			f[find(x)]=find(y);
		}
		else{
			if(f[find(x)]==f[find(y)])cout<<"Y\n";
			else cout<<"N\n";
		}
	}
}
并查集是一种用于处理不相交集合的合并与查询问题的数据结构。以下为几种不同形式的并查集C++代码模板: ### 简单数组实现模板 ```cpp #include<iostream> using namespace std; const int N=1e5+10; int p[N],num[N]; int find(int x) { if(p[x] != x) { p[x] = find(p[x]); } return p[x]; } int main() { int n, m; cin >> n >> m; for(int i = 1;i <= n;i++) { p[i] = i; num[i] = 1; } while(m--) { string act; cin >> act; int a, b; if(act == "C") { scanf("%d%d", &a, &b); if(find(a) == find(b)) { continue; } num[find(a)] += num[find(b)]; p[find(b)] = find(a); } else if(act == "Q1") { scanf("%d%d", &a, &b); if(find(a) == find(b)) { printf("Yes\n"); } else { printf("No\n"); } } else if(act == "Q2") { scanf("%d", &a); printf("%d\n", num[find(a)]); } } return 0; } ``` 此模板可处理合并集合(`C`操作)、判断两个元素是否在同一集合(`Q1`操作)以及查询某个元素所在集合的元素数量(`Q2`操作)的问题 [^1]。 ### 二维转换为一维的模板 ```cpp #include <bits/stdc++.h> using namespace std; const int N = 40010; int n, m; int f[N]; void init() { for (int i = 0; i < N; i++) f[i] = i; } int get(int x, int y) { return x * n + y; } int find(int x) { if (x != f[x]) f[x] = find(f[x]); return f[x]; } int main() { cin >> n >> m; init(); int res = 0; for (int i = 0; i < m; i++) { int x, y; char c; cin >> x >> y >> c; x--, y--; int a = get(x, y); int b; if (c == 'D') b = get(x + 1, y); else if (c == 'R') b = get(x, y + 1); int pa = find(a), pb = find(b); if (pa == pb) { res = i; break; } if (pa != pb) f[pb] = pa; } if (!res) puts("draw"); else cout << res + 1 << endl; return 0; } ``` 该模板把二维坐标转换为一维,适用于处理二维平面上的集合合并与查找问题 [^2]。 ### 类封装模板 ```cpp #include <iostream> #include <vector> #include <numeric> class unionFind { public: std::vector<int> father; std::vector<int> size; int setCount; int n; unionFind(int _n) : father(_n), size(_n, 1), n(_n), setCount(_n) { std::iota(father.begin(), father.end(), 0); } int find(int x) { if (father[x] == x) return x; return father[x] = find(father[x]); } bool unite(int x, int y) { x = find(x); y = find(y); if (x == y) { std::cout << x << "和" << y << "已经相连" << std::endl; return false; } if (size[x] <= size[y]) std::swap(x, y); father[y] = x; size[x] += size[y]; --setCount; return true; } }; ``` 这个模板以类的形式封装了并查集的操作,包含初始化、查找和合并等功能,还能维护每个集合的大小和连通分量的数量 [^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值