1107. Social Clusters (30) 并查集
2018-02-11 03:24
423 查看
#include <iostream> #include <vector> #include <algorithm> #include <stack> using namespace std; int father[1001]; int hobby[1001]; int gnum[1001]; int find(int x){ while (x != father[x]) { x = father[x]; } return x; } void Union(int a, int b){ a = find(a); b = find(b); if(a < b) father[b] = a; else father[a] = b; } void init(){ for (int i = 0; i < 1001; i++) { father[i] = i; } } int main(){ init(); int n; cin >> n; for (int i = 1; i <= n; i++) { int k; scanf("%d:", &k); for (int j = 0; j < k; j++) { int t; scanf("%d", &t); if(!hobby[t]) hobby[t] = i; Union(i, find(hobby[t])); } } for (int i = 1; i <= n; i++) { gnum[find(i)]++; } int cnt = 0; for (int i = 1; i <= n; i++) { if(gnum[i] != 0) cnt++; } sort(gnum, gnum + 1001); cout << cnt << endl; for (int i = 1000; i > 1000 - cnt; i--) { printf("%d%c", gnum[i], i == 1000-cnt+1 ? '\n' : ' '); } return 0; }
相关文章推荐
- [并查集] 1107. Social Clusters (30)
- PAT - 1107. Social Clusters (30) 并查集
- PAT (Advanced Level) Practise 1107. Social Clusters (30) 并查集
- PAT - 甲级 - 1107. Social Clusters (30) (并查集)
- PAT 1107. Social Clusters (30) 并查集
- PAT 1107. Social Clusters (30) 特殊问题+并查集+数量信息并查集
- PAT (Advanced Level)1107. Social Clusters (30)并查集
- 1107. Social Clusters (30)[并查集]
- PAT甲题题解-1107. Social Clusters (30)-PAT甲级真题(并查集)
- PAT (Advanced Level) 1107. Social Clusters (30)
- Pat(A) 1107. Social Clusters (30)
- pat甲级 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- PAT (Advanced Level) Practise 1107 Social Clusters (30)
- PAT--1107. Social Clusters(并查集)
- PAT 1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 【PAT】【Advanced Level】1107. Social Clusters (30)
- 1107. Social Clusters (30)
- 1107. Social Clusters (30)