题目情境#
题目链接:https://www.luogu.com.cn/problem/P3367 ↗
题目描述
如题,现在有一个并查集,你需要完成合并和查询操作。
输入格式
第一行包含两个整数 ,表示共有 个元素和 个操作。
接下来 行,每行包含三个整数 。
当 时,将 与 所在的集合合并。
当 时,输出 与 是否在同一集合内,是的输出
Y ;否则输出 N 。
输出格式
对于每一个 的操作,都有一行输出,每行包含一个大写字母,为 Y 或者 N 。
输入 #1
4 7
2 1 2
1 1 2
2 1 2
1 3 4
2 1 4
1 2 3
2 1 4plaintext输出 #1
N
Y
N
Yplaintext对于 的数据,,,,。
算法解析#
一般我们使用“合并”即可,按秩合并不太需要



Python代码实现#
n, m = map(int, input().split())
pa = [i for i in range(n + 1)]
def find(x):
if pa[x] == x:
return x
pa[x] = find(pa[x])
return pa[x]
def union(x, y):
pa[find(x)] = find(y)
for _ in range(m):
z, x, y = map(int, input().split())
if z == 1:
union(x, y)
else:
if find(x) == find(y):
print("Y")
else:
print("N")pythonC++代码实现#
//并查集 路径压缩
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=10005;
int n,m,x,y,z;
int pa[N];
int find(int x){
if(pa[x]==x) return x;
return pa[x]=find(pa[x]);
}
void unset(int x,int y){
pa[find(x)]=find(y);
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++) pa[i]=i;
while(m --){
cin>>z>>x>>y;
if(z==1) unset(x,y);
else{
if(find(x)==find(y)) puts("Y");
else puts("N");
}
}
}c