本文共 977 字,大约阅读时间需要 3 分钟。
#include #include #include #include #include #include #define re registerusing 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;}
转载于:https://www.cnblogs.com/Stephen-F/p/9931872.html