-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1114.cpp
More file actions
75 lines (67 loc) · 1.74 KB
/
1114.cpp
File metadata and controls
75 lines (67 loc) · 1.74 KB
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
72
73
74
75
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct Family {
int id, count;
double sets, area;
};
const int N = 10005;
int sets[N], area[N];
Family families[N];
struct UnionFindSet {
vector<int> id;
UnionFindSet(int size) : id(size + 1) {
for (int i = 0; i <= size; i++)
id[i] = i;
}
int find(int f) {
if (id[f] == f) return f;
return id[f] = find(id[f]);
}
bool connected(int f, int y) {
return find(f) == find(y);
}
void union_sets(int f, int y) {
int i = find(f), j = find(y);
if (i < j) id[j] = i;
else id[i] = j;
}
};
int main() {
int n;
cin >> n;
UnionFindSet uf(N);
while (n--) {
int id, father, mather, child, k;
cin >> id >> father >> mather >> k;
if (father != -1) uf.union_sets(id, father);
if (mather != -1) uf.union_sets(id, mather);
while (k--) {
cin >> child;
uf.union_sets(id, child);
}
cin >> sets[id] >> area[id];
}
for (int i = 0; i < N; i++) {
int id = uf.find(i);
families[id].id = id;
families[id].count++;
families[id].sets += sets[i];
families[id].area += area[i];
}
vector<Family> ans;
for (auto family : families) {
if (!family.sets) continue;
family.sets /= family.count;
family.area /= family.count;
ans.push_back(family);
}
sort(ans.begin(), ans.end(), [](Family a, Family b) {
return a.area != b.area ? a.area > b.area : a.id < b.id;
});
printf("%lu\n", ans.size());
for (auto f : ans)
printf("%04d %d %.3f %.3f\n", f.id, f.count, f.sets, f.area);
return 0;
}