https://www.patest.cn/contests/gplt/L2-024
题解:部落是并查集模板题。
社交集群用并查集暴力有23分
坑:写了半天,发现自己并查集没怎么学明白,现在才搞懂;
find :return f[x]=find(f[x]);
询问:if(find(x)==find(y))
合并:xx=find(x),yy=find(y);f[xx]=yy;
#include<iostream> #include<algorithm> #include<stdio.h> #include<string> #include<map> #include<vector> #define _for(i, a, b) for (int i = (a); i<(b); ++i) using namespace std; typedef long long ll; const int N = 1e4 + 5; int f[N]; int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }; void un(int x, int y) { int xx = find(x); int yy = find(y); f[xx]= yy ; }; int main() { int n; int mn = 0; cin >> n; _for(i, 0, N) f[i] = i; while (n--) { int x; cin >> x; int y1; cin >> y1; mn = max(mn, y1); _for(i, 0, x - 1) { int y; cin >> y; mn = max(mn, y); un(y1, y); } } int cnt = 0; _for(i, 0, mn) { if (f[i+1] == i+1)cnt++; } cout << mn << ' ' << cnt << endl; int q; cin >> q; while (q--) { int x, y; cin >> x >> y; if (find(x) == find(y))cout << "Y" << endl; else cout << "N" << endl; } }