洛谷P3367并查集

时间:2021-03-11 15:34:07

传送门

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#define re register
using namespace std;
const int maxn = 10005;
const int maxm = 200005; inline int read(){
char ch = getchar();
int f = 1 , x = 0 ;
while(ch > '9' || ch < '0'){if(ch == '-') f = -1;ch = getchar();}
while(ch >= '0' && ch <= '9') {x = (x << 1) + (x << 3) + ch - '0'; ch = getchar();}
return x * f;
} int n,m,z,x,y;
int fa[maxn]; inline int find(int x){
if(x != fa[x]) fa[x] = find(fa[x]);
return fa[x];
} inline void init(){
for(re int i = 1 ; i <= n ; ++i)
fa[i] = i;
} int main(){
n = read(); m = read();
init();
for(re int i = 1 ; i <= m ; ++i) {
z = read(); x = read(); y = read();
if(z == 1) {
int f1 = find(x) , f2 = find(y);
if(f1 != f2)
fa[f1] = f2;
}
else {
int f1 = find(x) , f2 = find(y);
if(f1 == f2) printf("Y\n");
else printf("N\n");
}
}
return 0;
}