#include "job.h"
#include <vector>
#include <set>
#include <cstdio>
const int MAX_N = 200000;
long long res;
std::vector<int> graph[MAX_N];
struct DSUNode {
int nod, sd, su;
DSUNode(int _nod, int _sd, int _su) {
nod = _nod;
sd = _sd;
su = _su;
}
bool operator< (const DSUNode x) const {
return (long long)sd * x.su < (long long)su * x.sd ||
((long long)sd * x.su == (long long)su * x.sd && nod < x.nod);
}
};
struct DSU {
int sd, su;
std::set<DSUNode> extended_neighbours;
} dsu[MAX_N];
void join(int a, int b) {
res = res + (long long)dsu[a].sd * dsu[b].su;
dsu[a].sd += dsu[b].sd;
dsu[a].su += dsu[b].su;
dsu[a].extended_neighbours.erase({b, dsu[b].sd, dsu[b].su});
if(dsu[a].extended_neighbours.size() < dsu[b].extended_neighbours.size())
dsu[a].extended_neighbours.swap(dsu[b].extended_neighbours);
for(auto it: dsu[b].extended_neighbours)
dsu[a].extended_neighbours.insert(it);
dsu[b].extended_neighbours.clear();
}
void dfs(int nod) {
for(auto it: graph[nod]) {
dfs(it);
dsu[nod].extended_neighbours.insert({it, dsu[it].sd, dsu[it].su});
}
while(!dsu[nod].extended_neighbours.empty()
&& *dsu[nod].extended_neighbours.begin() < DSUNode(nod, dsu[nod].sd, dsu[nod].su))
join(nod, dsu[nod].extended_neighbours.begin()->nod);
}
long long scheduling_cost(std::vector<int> p, std::vector<int> u, std::vector<int> d) {
int n = u.size();
for(int i = 0; i < n; ++i) {
dsu[i] = {d[i], u[i]};
res = res + (long long)u[i] * d[i];
}
for(int i = 1; i < n; ++i)
graph[p[i]].push_back(i);
dfs(0);
while(!dsu[0].extended_neighbours.empty())
join(0, dsu[0].extended_neighbours.begin()->nod);
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16000 KB |
Output is correct |
2 |
Correct |
11 ms |
16000 KB |
Output is correct |
3 |
Correct |
11 ms |
16000 KB |
Output is correct |
4 |
Correct |
11 ms |
16128 KB |
Output is correct |
5 |
Correct |
42 ms |
24952 KB |
Output is correct |
6 |
Correct |
74 ms |
33912 KB |
Output is correct |
7 |
Correct |
106 ms |
43004 KB |
Output is correct |
8 |
Correct |
139 ms |
51960 KB |
Output is correct |
9 |
Correct |
136 ms |
51964 KB |
Output is correct |
10 |
Correct |
141 ms |
52040 KB |
Output is correct |
11 |
Correct |
10 ms |
16000 KB |
Output is correct |
12 |
Correct |
138 ms |
51960 KB |
Output is correct |
13 |
Correct |
135 ms |
51936 KB |
Output is correct |
14 |
Correct |
137 ms |
52008 KB |
Output is correct |
15 |
Correct |
139 ms |
52088 KB |
Output is correct |
16 |
Correct |
140 ms |
52088 KB |
Output is correct |
17 |
Correct |
141 ms |
51960 KB |
Output is correct |
18 |
Correct |
138 ms |
51960 KB |
Output is correct |
19 |
Correct |
137 ms |
52148 KB |
Output is correct |
20 |
Correct |
149 ms |
60792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16000 KB |
Output is correct |
2 |
Correct |
10 ms |
16000 KB |
Output is correct |
3 |
Correct |
11 ms |
16000 KB |
Output is correct |
4 |
Correct |
277 ms |
34048 KB |
Output is correct |
5 |
Correct |
270 ms |
34028 KB |
Output is correct |
6 |
Correct |
280 ms |
34028 KB |
Output is correct |
7 |
Correct |
269 ms |
34028 KB |
Output is correct |
8 |
Correct |
274 ms |
33904 KB |
Output is correct |
9 |
Correct |
270 ms |
34028 KB |
Output is correct |
10 |
Correct |
268 ms |
34028 KB |
Output is correct |
11 |
Correct |
282 ms |
34028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16000 KB |
Output is correct |
2 |
Correct |
10 ms |
16000 KB |
Output is correct |
3 |
Correct |
11 ms |
16000 KB |
Output is correct |
4 |
Correct |
11 ms |
16000 KB |
Output is correct |
5 |
Correct |
20 ms |
16896 KB |
Output is correct |
6 |
Correct |
267 ms |
34028 KB |
Output is correct |
7 |
Correct |
267 ms |
34028 KB |
Output is correct |
8 |
Correct |
273 ms |
34028 KB |
Output is correct |
9 |
Correct |
270 ms |
34156 KB |
Output is correct |
10 |
Correct |
11 ms |
16000 KB |
Output is correct |
11 |
Correct |
12 ms |
16128 KB |
Output is correct |
12 |
Correct |
17 ms |
16768 KB |
Output is correct |
13 |
Correct |
18 ms |
16896 KB |
Output is correct |
14 |
Correct |
264 ms |
34028 KB |
Output is correct |
15 |
Correct |
271 ms |
34028 KB |
Output is correct |
16 |
Correct |
266 ms |
34028 KB |
Output is correct |
17 |
Correct |
268 ms |
34028 KB |
Output is correct |
18 |
Correct |
265 ms |
34028 KB |
Output is correct |
19 |
Correct |
269 ms |
34028 KB |
Output is correct |
20 |
Correct |
267 ms |
34028 KB |
Output is correct |
21 |
Correct |
267 ms |
34156 KB |
Output is correct |
22 |
Correct |
272 ms |
34056 KB |
Output is correct |
23 |
Correct |
281 ms |
34032 KB |
Output is correct |
24 |
Correct |
278 ms |
34028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16000 KB |
Output is correct |
2 |
Correct |
139 ms |
42616 KB |
Output is correct |
3 |
Correct |
135 ms |
42744 KB |
Output is correct |
4 |
Correct |
136 ms |
42616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16000 KB |
Output is correct |
2 |
Correct |
11 ms |
16000 KB |
Output is correct |
3 |
Correct |
11 ms |
16000 KB |
Output is correct |
4 |
Correct |
11 ms |
16000 KB |
Output is correct |
5 |
Correct |
11 ms |
16000 KB |
Output is correct |
6 |
Correct |
11 ms |
16000 KB |
Output is correct |
7 |
Correct |
11 ms |
16128 KB |
Output is correct |
8 |
Correct |
11 ms |
16000 KB |
Output is correct |
9 |
Correct |
11 ms |
16000 KB |
Output is correct |
10 |
Correct |
11 ms |
16000 KB |
Output is correct |
11 |
Correct |
11 ms |
16000 KB |
Output is correct |
12 |
Correct |
11 ms |
16000 KB |
Output is correct |
13 |
Correct |
11 ms |
16000 KB |
Output is correct |
14 |
Correct |
11 ms |
16000 KB |
Output is correct |
15 |
Correct |
11 ms |
16000 KB |
Output is correct |
16 |
Correct |
11 ms |
16000 KB |
Output is correct |
17 |
Correct |
11 ms |
16000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16000 KB |
Output is correct |
2 |
Correct |
11 ms |
16000 KB |
Output is correct |
3 |
Correct |
11 ms |
16000 KB |
Output is correct |
4 |
Correct |
11 ms |
16128 KB |
Output is correct |
5 |
Correct |
42 ms |
24952 KB |
Output is correct |
6 |
Correct |
74 ms |
33912 KB |
Output is correct |
7 |
Correct |
106 ms |
43004 KB |
Output is correct |
8 |
Correct |
139 ms |
51960 KB |
Output is correct |
9 |
Correct |
136 ms |
51964 KB |
Output is correct |
10 |
Correct |
141 ms |
52040 KB |
Output is correct |
11 |
Correct |
10 ms |
16000 KB |
Output is correct |
12 |
Correct |
138 ms |
51960 KB |
Output is correct |
13 |
Correct |
135 ms |
51936 KB |
Output is correct |
14 |
Correct |
137 ms |
52008 KB |
Output is correct |
15 |
Correct |
139 ms |
52088 KB |
Output is correct |
16 |
Correct |
140 ms |
52088 KB |
Output is correct |
17 |
Correct |
141 ms |
51960 KB |
Output is correct |
18 |
Correct |
138 ms |
51960 KB |
Output is correct |
19 |
Correct |
137 ms |
52148 KB |
Output is correct |
20 |
Correct |
149 ms |
60792 KB |
Output is correct |
21 |
Correct |
11 ms |
16000 KB |
Output is correct |
22 |
Correct |
10 ms |
16000 KB |
Output is correct |
23 |
Correct |
11 ms |
16000 KB |
Output is correct |
24 |
Correct |
277 ms |
34048 KB |
Output is correct |
25 |
Correct |
270 ms |
34028 KB |
Output is correct |
26 |
Correct |
280 ms |
34028 KB |
Output is correct |
27 |
Correct |
269 ms |
34028 KB |
Output is correct |
28 |
Correct |
274 ms |
33904 KB |
Output is correct |
29 |
Correct |
270 ms |
34028 KB |
Output is correct |
30 |
Correct |
268 ms |
34028 KB |
Output is correct |
31 |
Correct |
282 ms |
34028 KB |
Output is correct |
32 |
Correct |
11 ms |
16000 KB |
Output is correct |
33 |
Correct |
10 ms |
16000 KB |
Output is correct |
34 |
Correct |
11 ms |
16000 KB |
Output is correct |
35 |
Correct |
11 ms |
16000 KB |
Output is correct |
36 |
Correct |
20 ms |
16896 KB |
Output is correct |
37 |
Correct |
267 ms |
34028 KB |
Output is correct |
38 |
Correct |
267 ms |
34028 KB |
Output is correct |
39 |
Correct |
273 ms |
34028 KB |
Output is correct |
40 |
Correct |
270 ms |
34156 KB |
Output is correct |
41 |
Correct |
11 ms |
16000 KB |
Output is correct |
42 |
Correct |
12 ms |
16128 KB |
Output is correct |
43 |
Correct |
17 ms |
16768 KB |
Output is correct |
44 |
Correct |
18 ms |
16896 KB |
Output is correct |
45 |
Correct |
264 ms |
34028 KB |
Output is correct |
46 |
Correct |
271 ms |
34028 KB |
Output is correct |
47 |
Correct |
266 ms |
34028 KB |
Output is correct |
48 |
Correct |
268 ms |
34028 KB |
Output is correct |
49 |
Correct |
265 ms |
34028 KB |
Output is correct |
50 |
Correct |
269 ms |
34028 KB |
Output is correct |
51 |
Correct |
267 ms |
34028 KB |
Output is correct |
52 |
Correct |
267 ms |
34156 KB |
Output is correct |
53 |
Correct |
272 ms |
34056 KB |
Output is correct |
54 |
Correct |
281 ms |
34032 KB |
Output is correct |
55 |
Correct |
278 ms |
34028 KB |
Output is correct |
56 |
Correct |
10 ms |
16000 KB |
Output is correct |
57 |
Correct |
139 ms |
42616 KB |
Output is correct |
58 |
Correct |
135 ms |
42744 KB |
Output is correct |
59 |
Correct |
136 ms |
42616 KB |
Output is correct |
60 |
Correct |
10 ms |
16000 KB |
Output is correct |
61 |
Correct |
11 ms |
16000 KB |
Output is correct |
62 |
Correct |
11 ms |
16000 KB |
Output is correct |
63 |
Correct |
11 ms |
16000 KB |
Output is correct |
64 |
Correct |
11 ms |
16000 KB |
Output is correct |
65 |
Correct |
11 ms |
16000 KB |
Output is correct |
66 |
Correct |
11 ms |
16128 KB |
Output is correct |
67 |
Correct |
11 ms |
16000 KB |
Output is correct |
68 |
Correct |
11 ms |
16000 KB |
Output is correct |
69 |
Correct |
11 ms |
16000 KB |
Output is correct |
70 |
Correct |
11 ms |
16000 KB |
Output is correct |
71 |
Correct |
11 ms |
16000 KB |
Output is correct |
72 |
Correct |
11 ms |
16000 KB |
Output is correct |
73 |
Correct |
11 ms |
16000 KB |
Output is correct |
74 |
Correct |
11 ms |
16000 KB |
Output is correct |
75 |
Correct |
11 ms |
16000 KB |
Output is correct |
76 |
Correct |
11 ms |
16000 KB |
Output is correct |
77 |
Correct |
10 ms |
15896 KB |
Output is correct |
78 |
Correct |
11 ms |
16000 KB |
Output is correct |
79 |
Correct |
11 ms |
16000 KB |
Output is correct |
80 |
Correct |
10 ms |
16000 KB |
Output is correct |
81 |
Correct |
12 ms |
16000 KB |
Output is correct |
82 |
Correct |
11 ms |
16000 KB |
Output is correct |
83 |
Correct |
11 ms |
16000 KB |
Output is correct |
84 |
Correct |
10 ms |
16000 KB |
Output is correct |
85 |
Correct |
11 ms |
16000 KB |
Output is correct |
86 |
Correct |
10 ms |
16000 KB |
Output is correct |
87 |
Correct |
10 ms |
16000 KB |
Output is correct |
88 |
Correct |
15 ms |
16384 KB |
Output is correct |
89 |
Correct |
18 ms |
16640 KB |
Output is correct |
90 |
Correct |
20 ms |
16768 KB |
Output is correct |
91 |
Correct |
61 ms |
20344 KB |
Output is correct |
92 |
Correct |
123 ms |
24788 KB |
Output is correct |
93 |
Correct |
271 ms |
33528 KB |
Output is correct |
94 |
Correct |
274 ms |
33528 KB |
Output is correct |
95 |
Correct |
273 ms |
33400 KB |
Output is correct |
96 |
Correct |
271 ms |
33400 KB |
Output is correct |
97 |
Correct |
271 ms |
33276 KB |
Output is correct |
98 |
Correct |
256 ms |
34512 KB |
Output is correct |
99 |
Correct |
297 ms |
35564 KB |
Output is correct |
100 |
Correct |
271 ms |
33400 KB |
Output is correct |
101 |
Correct |
231 ms |
32376 KB |
Output is correct |
102 |
Correct |
324 ms |
39544 KB |
Output is correct |
103 |
Correct |
230 ms |
31096 KB |
Output is correct |
104 |
Correct |
210 ms |
30456 KB |
Output is correct |
105 |
Correct |
173 ms |
30968 KB |
Output is correct |
106 |
Correct |
305 ms |
37148 KB |
Output is correct |