题解
2025-08-06 09:44:26
发布于:上海
1阅读
0回复
0点赞
纯模板,背下来就行,封装了
#include <iostream>
using namespace std;
const int N = 2e5 + 5;
struct set{
int size;
int f[N] = {};
public:void init(){
for(int i=1; i<=size; i++){
f[i] = i;
}
}
public:int find(int x){
if(f[x] == x) return x;
return f[x] = find(f[x]);
}
public:void unite(int x, int y){
int rootx = find(x);
int rooty = find(y);
if(rootx != rooty) f[rootx] = rooty;
}
public:bool inset(int x, int y){
if(find(x) == find(y)) return true;
else return false;
}
};
int main(){
int m;
set s;
cin >> s.size >> m;
s.init();
for(int i=1; i<=m; i++){
int op, x, y;
cin >> op >> x >> y;
if(op == 1){
s.unite(x, y);
}
else{
if(s.inset(x, y)){
cout << 'Y' << endl;
}
else{
cout << 'N' << endl;
}
}
}
return 0;
}
这里空空如也
有帮助,赞一个