#include<iostream> #include<vector> using namespace std; vector<int> fa{ -1 }; int find(int i) { if (fa[i] != i) fa[i] = find(fa[i]); return fa[i]; } void merge(int i, int j) { fa[find(i)] = find(j); } int main() { int n, m, p; cin >> n >> m >> p; for (int i = 1; i <= n; i++) fa.push_back(i); for (int i = 0, a, b; i < m; i++) { cin >> a >> b; merge(a, b); } string res; for (int i = 0, a, b; i < p; i++) { cin >> a >> b; res += (find(a) == find(b) ? "Yes\n" : "No\n"); } cout << res; return 0; }
标签:merge,int,res,查集,亲戚,fa,find From: https://www.cnblogs.com/xlege/p/17781621.html