模板 并查集
作者:
lrx2020
,
2023-02-01 08:32:55
,
所有人可见
,
阅读 195
模板 并查集
#include <iostream>
using namespace std;
const int N = 100010;
int p[N];
int find(int x){
if(p[x]==x) return x;
else return p[x] = find(p[x]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int n,m;
cin >> n >> m;
for(int i=1;i<=n;i++) p[i] = i;
while(m--) {
int a,b,c;
cin >> a >> b >> c;
if(a==1) {
p[find(b)] = find(c);
}
if(a==2) {
if(find(b) == find(c)) cout << "Y" << endl;
else cout << "N" << endl;
}
}
return 0;
}