-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFamily Property.cpp
117 lines (111 loc) · 2.47 KB
/
Family Property.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <algorithm>
#include <iomanip>
#include <iostream>
int father[10010];
struct node
{
double num = 1;
double set = 0, are = 0;
} flag[10010];
int visited[10010] = {0};
struct Ans
{
int name;
double num;
double set, are;
} ans[10010];
int count = 0;
bool cmp(Ans a, Ans b)
{
if (a.are / a.num == b.are / b.num)
return a.name < b.name;
return a.are / a.num > b.are / b.num;
}
int findfather(int v)
{
while (father[v] != v)
{
v = father[v];
}
return v;
}
int Union(int x, int y)
{
int a = findfather(x);
int b = findfather(y);
if (a != b)
{
if (a < b)
father[b] = a;
else
father[a] = b;
}
return 0;
}
int main()
{
for (int i = 0; i < 10010; i++)
{
father[i] = i;
}
int n;
std::cin >> n;
for (int i = 0; i < n; i++)
{
int temp, f;
std::cin >> temp >> f;
visited[temp] = 1;
if (f != -1)
{
visited[f] = 1;
Union(temp, f);
}
std::cin >> f;
if (f != -1)
{
visited[f] = 1;
Union(temp, f);
}
int m;
std::cin >> m;
for (int j = 0; j < m; j++)
{
int f;
std::cin >> f;
visited[f] = 1;
Union(temp, f);
}
std::cin >> flag[temp].set >> flag[temp].are;
}
for (int i = 0; i < 10010; i++)
{
if (visited[i] == 1 && father[i] != i)
{
flag[findfather(i)].num++;
flag[findfather(i)].set += flag[i].set;
flag[findfather(i)].are += flag[i].are;
}
}
for (int i = 0; i < 10010; i++)
{
if (visited[i] == 1 && father[i] == i)
{
ans[count].name = i;
ans[count].num = flag[i].num;
ans[count].set = flag[i].set;
ans[count].are = flag[i].are;
count++;
}
}
std::sort(ans, ans + count, cmp);
std::cout << count << std::endl;
for (int i = 0; i < count; i++)
{
printf("%04d", ans[i].name);
std::cout << " "
<< std::setiosflags(std::ios::fixed) << std::setprecision(0) << ans[i].num << " "
<< std::setiosflags(std::ios::fixed) << std::setprecision(3) << ans[i].set / ans[i].num << " "
<< std::setiosflags(std::ios::fixed) << std::setprecision(3) << ans[i].are / ans[i].num << std::endl;
}
return 0;
}