-
Notifications
You must be signed in to change notification settings - Fork 2
/
1107.cpp
58 lines (51 loc) · 1.36 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
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct UnionFind {
vector<int> id, sz;
UnionFind(int n) : id(n), sz(n, 1) {
for (int i = 0; i < n; i++)
id[i] = i;
}
int find(int x) {
if (id[x] == x) return x;
return id[x] = find(id[x]);
}
void union_sets(int x, int y) {
int i = find(x), j = find(y);
if (i == j) return;
if (sz[i] >= sz[j]) { id[j] = i; sz[i] += sz[j]; }
else { id[i] = j; sz[j] += sz[i]; }
}
void print() {
vector<int> ans;
for (int i = 0; i < id.size(); i++)
if (id[i] == i) ans.push_back(sz[i]);
sort(ans.begin(), ans.end(), greater<int>());
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i++)
cout << ans[i] << (i < ans.size() - 1 ? ' ' : '\n');
}
};
const int MAX_H = 1005;
int main() {
int n, k, h;
scanf("%d", &n);
vector<int> hobbies[MAX_H];
UnionFind uf(n);
for (int i = 0; i < n; i++) {
scanf("%d:", &k);
while (k--) {
scanf("%d", &h);
hobbies[h].push_back(i);
}
}
for (auto hobby : hobbies) {
if (hobby.empty()) continue;
for (auto person : hobby)
uf.union_sets(person, hobby.front());
}
uf.print();
return 0;
}