#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;
}
};
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 |
11 ms |
16000 KB |
Output is correct |
3 |
Correct |
11 ms |
16128 KB |
Output is correct |
4 |
Correct |
11 ms |
16184 KB |
Output is correct |
5 |
Correct |
43 ms |
25720 KB |
Output is correct |
6 |
Correct |
74 ms |
35552 KB |
Output is correct |
7 |
Correct |
107 ms |
45432 KB |
Output is correct |
8 |
Correct |
149 ms |
55288 KB |
Output is correct |
9 |
Correct |
145 ms |
55164 KB |
Output is correct |
10 |
Correct |
143 ms |
55164 KB |
Output is correct |
11 |
Correct |
11 ms |
16000 KB |
Output is correct |
12 |
Correct |
140 ms |
55184 KB |
Output is correct |
13 |
Correct |
139 ms |
55160 KB |
Output is correct |
14 |
Correct |
137 ms |
55160 KB |
Output is correct |
15 |
Correct |
143 ms |
55316 KB |
Output is correct |
16 |
Correct |
139 ms |
55288 KB |
Output is correct |
17 |
Correct |
139 ms |
55160 KB |
Output is correct |
18 |
Correct |
141 ms |
55288 KB |
Output is correct |
19 |
Correct |
149 ms |
63864 KB |
Output is correct |
20 |
Correct |
134 ms |
55160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
16000 KB |
Output is correct |
2 |
Correct |
11 ms |
16000 KB |
Output is correct |
3 |
Incorrect |
10 ms |
16000 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
16020 KB |
Output is correct |
2 |
Correct |
11 ms |
16000 KB |
Output is correct |
3 |
Correct |
11 ms |
16000 KB |
Output is correct |
4 |
Correct |
12 ms |
16000 KB |
Output is correct |
5 |
Incorrect |
18 ms |
16896 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
11 ms |
16000 KB |
Output is correct |
2 |
Incorrect |
10 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 |
11 ms |
16000 KB |
Output is correct |
3 |
Correct |
11 ms |
16128 KB |
Output is correct |
4 |
Correct |
11 ms |
16184 KB |
Output is correct |
5 |
Correct |
43 ms |
25720 KB |
Output is correct |
6 |
Correct |
74 ms |
35552 KB |
Output is correct |
7 |
Correct |
107 ms |
45432 KB |
Output is correct |
8 |
Correct |
149 ms |
55288 KB |
Output is correct |
9 |
Correct |
145 ms |
55164 KB |
Output is correct |
10 |
Correct |
143 ms |
55164 KB |
Output is correct |
11 |
Correct |
11 ms |
16000 KB |
Output is correct |
12 |
Correct |
140 ms |
55184 KB |
Output is correct |
13 |
Correct |
139 ms |
55160 KB |
Output is correct |
14 |
Correct |
137 ms |
55160 KB |
Output is correct |
15 |
Correct |
143 ms |
55316 KB |
Output is correct |
16 |
Correct |
139 ms |
55288 KB |
Output is correct |
17 |
Correct |
139 ms |
55160 KB |
Output is correct |
18 |
Correct |
141 ms |
55288 KB |
Output is correct |
19 |
Correct |
149 ms |
63864 KB |
Output is correct |
20 |
Correct |
134 ms |
55160 KB |
Output is correct |
21 |
Correct |
10 ms |
16000 KB |
Output is correct |
22 |
Correct |
11 ms |
16000 KB |
Output is correct |
23 |
Incorrect |
10 ms |
16000 KB |
Output isn't correct |
24 |
Halted |
0 ms |
0 KB |
- |