本文共 546 字,大约阅读时间需要 1 分钟。
真真就是个并查集模板
#include#include using namespace std;int n, m, fa[10010], c, x, y, xx, yy;int find(int x) { if(fa[x] != x) { fa[x] = find(fa[x]); return fa[x]; } return x;}int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) fa[i] = i; for (int i = 1; i <= m; i++) { scanf("%d %d %d", &c, &x, &y); if(c == 1) { xx = find(x), yy = find(y); if(xx != yy) fa[xx] = yy; } else { xx = find(x), yy = find(y); if(xx != yy) printf("N\n"); else printf("Y\n"); } }}
转载地址:http://gyiq.baihongyu.com/