-
Notifications
You must be signed in to change notification settings - Fork 0
/
1107.cpp
71 lines (55 loc) · 1.48 KB
/
1107.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <cstdio>
#include <vector>
#include <set>
#include <algorithm>
#include <cstring>
const int MAXN = 1000 + 10;
struct UnionFindSet {
int fa[MAXN], s[MAXN];
int size;
UnionFindSet(int size) : size(size) {
for (int i = 0; i < size; i++) {
s[i] = 1;
fa[i] = i;
}
}
int find(int x) {
if (fa[x] == x) return x;
else return fa[x] = find(fa[x]);
}
void merge(int r1, int r2) {
int x = find(r1);
int y = find(r2);
fa[x] = y;
s[x] = s[y] = (s[x] + s[y]);
}
};
int main() {
int n;
scanf("%d", &n);
UnionFindSet ufs(n);
std::vector<std::set<int>> v(n);
for (int i = 0; i < n; i++) {
int num;
scanf("%d:", &num);
for (int j = 0; j < num; j++) {
int x;
scanf("%d", &x);
v[i].insert(x);
for (int k = 0; k < i; k++) {
if (v[k].count(x) && ufs.find(k) != ufs.find(i)) ufs.merge(k, i);
}
}
}
std::vector<int> ans;
for (int i = 0; i < n; i++) {
if (ufs.fa[i] == i) ans.push_back(ufs.s[i]);
}
std::sort(ans.begin(), ans.end(), [](int a, int b)->bool{return a > b;});
printf("%d\n", ans.size());
for (int i = 0; i < ans.size(); i++) {
if (i == ans.size() - 1) printf("%d\n", ans[i]);
else printf("%d ", ans[i]);
}
return 0;
}