#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 == 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()
&& DSUNode(nod, dsu[nod].sd, dsu[nod].su) < *dsu[nod].extended_neighbours.begin())
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
16000 KB |
Output is correct |
2 |
Correct |
10 ms |
16000 KB |
Output is correct |
3 |
Correct |
10 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 |
73 ms |
33952 KB |
Output is correct |
7 |
Correct |
105 ms |
43128 KB |
Output is correct |
8 |
Correct |
136 ms |
51992 KB |
Output is correct |
9 |
Correct |
143 ms |
51960 KB |
Output is correct |
10 |
Correct |
139 ms |
51960 KB |
Output is correct |
11 |
Correct |
11 ms |
16000 KB |
Output is correct |
12 |
Correct |
138 ms |
51996 KB |
Output is correct |
13 |
Correct |
137 ms |
51960 KB |
Output is correct |
14 |
Correct |
137 ms |
52088 KB |
Output is correct |
15 |
Correct |
142 ms |
52088 KB |
Output is correct |
16 |
Correct |
143 ms |
51960 KB |
Output is correct |
17 |
Correct |
141 ms |
51960 KB |
Output is correct |
18 |
Correct |
139 ms |
51960 KB |
Output is correct |
19 |
Correct |
142 ms |
60664 KB |
Output is correct |
20 |
Correct |
134 ms |
51960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
16000 KB |
Output is correct |
2 |
Correct |
10 ms |
16000 KB |
Output is correct |
3 |
Correct |
10 ms |
16000 KB |
Output is correct |
4 |
Correct |
272 ms |
35820 KB |
Output is correct |
5 |
Correct |
271 ms |
35692 KB |
Output is correct |
6 |
Correct |
273 ms |
35820 KB |
Output is correct |
7 |
Correct |
267 ms |
35692 KB |
Output is correct |
8 |
Correct |
270 ms |
35692 KB |
Output is correct |
9 |
Correct |
269 ms |
35692 KB |
Output is correct |
10 |
Correct |
275 ms |
35692 KB |
Output is correct |
11 |
Correct |
272 ms |
35692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 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 |
18 ms |
16896 KB |
Output is correct |
6 |
Correct |
266 ms |
36332 KB |
Output is correct |
7 |
Correct |
262 ms |
36332 KB |
Output is correct |
8 |
Correct |
266 ms |
36460 KB |
Output is correct |
9 |
Correct |
265 ms |
36336 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 |
267 ms |
36332 KB |
Output is correct |
15 |
Correct |
263 ms |
36332 KB |
Output is correct |
16 |
Correct |
271 ms |
36568 KB |
Output is correct |
17 |
Correct |
266 ms |
36332 KB |
Output is correct |
18 |
Correct |
262 ms |
36332 KB |
Output is correct |
19 |
Correct |
260 ms |
36332 KB |
Output is correct |
20 |
Correct |
263 ms |
36332 KB |
Output is correct |
21 |
Correct |
260 ms |
36368 KB |
Output is correct |
22 |
Correct |
273 ms |
36260 KB |
Output is correct |
23 |
Correct |
263 ms |
36208 KB |
Output is correct |
24 |
Correct |
260 ms |
36332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
16000 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
16000 KB |
Output is correct |
2 |
Incorrect |
11 ms |
16000 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
16000 KB |
Output is correct |
2 |
Correct |
10 ms |
16000 KB |
Output is correct |
3 |
Correct |
10 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 |
73 ms |
33952 KB |
Output is correct |
7 |
Correct |
105 ms |
43128 KB |
Output is correct |
8 |
Correct |
136 ms |
51992 KB |
Output is correct |
9 |
Correct |
143 ms |
51960 KB |
Output is correct |
10 |
Correct |
139 ms |
51960 KB |
Output is correct |
11 |
Correct |
11 ms |
16000 KB |
Output is correct |
12 |
Correct |
138 ms |
51996 KB |
Output is correct |
13 |
Correct |
137 ms |
51960 KB |
Output is correct |
14 |
Correct |
137 ms |
52088 KB |
Output is correct |
15 |
Correct |
142 ms |
52088 KB |
Output is correct |
16 |
Correct |
143 ms |
51960 KB |
Output is correct |
17 |
Correct |
141 ms |
51960 KB |
Output is correct |
18 |
Correct |
139 ms |
51960 KB |
Output is correct |
19 |
Correct |
142 ms |
60664 KB |
Output is correct |
20 |
Correct |
134 ms |
51960 KB |
Output is correct |
21 |
Correct |
10 ms |
16000 KB |
Output is correct |
22 |
Correct |
10 ms |
16000 KB |
Output is correct |
23 |
Correct |
10 ms |
16000 KB |
Output is correct |
24 |
Correct |
272 ms |
35820 KB |
Output is correct |
25 |
Correct |
271 ms |
35692 KB |
Output is correct |
26 |
Correct |
273 ms |
35820 KB |
Output is correct |
27 |
Correct |
267 ms |
35692 KB |
Output is correct |
28 |
Correct |
270 ms |
35692 KB |
Output is correct |
29 |
Correct |
269 ms |
35692 KB |
Output is correct |
30 |
Correct |
275 ms |
35692 KB |
Output is correct |
31 |
Correct |
272 ms |
35692 KB |
Output is correct |
32 |
Correct |
10 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 |
18 ms |
16896 KB |
Output is correct |
37 |
Correct |
266 ms |
36332 KB |
Output is correct |
38 |
Correct |
262 ms |
36332 KB |
Output is correct |
39 |
Correct |
266 ms |
36460 KB |
Output is correct |
40 |
Correct |
265 ms |
36336 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 |
267 ms |
36332 KB |
Output is correct |
46 |
Correct |
263 ms |
36332 KB |
Output is correct |
47 |
Correct |
271 ms |
36568 KB |
Output is correct |
48 |
Correct |
266 ms |
36332 KB |
Output is correct |
49 |
Correct |
262 ms |
36332 KB |
Output is correct |
50 |
Correct |
260 ms |
36332 KB |
Output is correct |
51 |
Correct |
263 ms |
36332 KB |
Output is correct |
52 |
Correct |
260 ms |
36368 KB |
Output is correct |
53 |
Correct |
273 ms |
36260 KB |
Output is correct |
54 |
Correct |
263 ms |
36208 KB |
Output is correct |
55 |
Correct |
260 ms |
36332 KB |
Output is correct |
56 |
Incorrect |
10 ms |
16000 KB |
Output isn't correct |
57 |
Halted |
0 ms |
0 KB |
- |