标签:main 并查集 div for void ++ turn ret names
并查集的模板题:
#include<iostream> #include<cstdio> using namespace std; const int maxn = 2e4+5; int fa[maxn]; int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } void unite(int x, int y) { int x1 = find(x); int y1 = find(y); if (x1 != y1)fa[x1] = y1; } int main() { int n, m, x, y; scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i)fa[i] = i; while (m--) { scanf("%d%d", &x, &y); unite(x, y); } scanf("%d", &m); while (m--) { scanf("%d%d", &x, &y); if (find(x) == find(y))printf("Yes\n"); else printf("No\n"); } }
标签:main 并查集 div for void ++ turn ret names
原文地址:https://www.cnblogs.com/ALINGMAOMAO/p/10088755.html