#include "job.h"
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <utility>
#include <cstring>
#include <vector>
#include <cassert>
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<pll, int> entry;
struct comp {
bool operator()(entry a, entry b) const {
int av = a.fi.fi * b.fi.se;
int bv = a.fi.se * b.fi.fi;
if (av != bv) {
return av < bv;
}
return a.se < b.se;
}
};
set<entry, comp> s;
vector<int> v[200005];
vector<int> ord;
int N, uf[200005];
ll uu[200005], dd[200005];
int f(int x) { if (uf[x] == x) return x; return uf[x] = f(uf[x]); }
void un(int x, int y) { uf[f(x)] = f(y); }
void dfs(int x) {
ord.push_back(x);
for (int i = 0; i < v[x].size(); i++) {
dfs(v[x][i]);
}
}
ll scheduling_cost(vector<int> p, vector<int> u, vector<int> d) {
int N = p.size();
for (int i = 0; i < N; i++) uf[i] = i;
for (int i = 1; i < N; i++) {
uu[i] = u[i];
dd[i] = d[i];
v[i].clear();
}
for (int i = 1; i < N; i++) s.insert({ { u[i], d[i] }, i });
while (s.size() > 0) {
auto it = --s.end();
entry e = *it;
s.erase(it);
assert(f(e.se) == e.se);
int par = p[e.se];
// Merge with parent
ll nu = uu[f(par)] + uu[e.se];
ll nd = dd[f(par)] + dd[e.se];
s.erase({ { uu[f(par)], dd[f(par)] }, f(par) });
v[f(par)].push_back(e.se);
un(e.se, par);
uu[f(par)] = nu;
dd[f(par)] = nd;
if (f(par) != 0) s.insert({ { uu[f(par)], dd[f(par)] }, f(par) });
}
dfs(0);
ll tm = 0, ans = 0;
for (int i = 0; i < ord.size(); i++) {
tm += d[ord[i]];
ans += tm * u[ord[i]];
}
return ans;
}
Compilation message
job.cpp: In function 'void dfs(int)':
job.cpp:39:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < v[x].size(); i++) {
~~^~~~~~~~~~~~~
job.cpp: In function 'll scheduling_cost(std::vector<int>, std::vector<int>, std::vector<int>)':
job.cpp:71:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < ord.size(); i++) {
~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
4984 KB |
Output is correct |
2 |
Correct |
7 ms |
4984 KB |
Output is correct |
3 |
Correct |
8 ms |
4984 KB |
Output is correct |
4 |
Runtime error |
18 ms |
10232 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
4984 KB |
Output is correct |
2 |
Correct |
8 ms |
4984 KB |
Output is correct |
3 |
Correct |
9 ms |
4984 KB |
Output is correct |
4 |
Correct |
291 ms |
28280 KB |
Output is correct |
5 |
Correct |
269 ms |
28148 KB |
Output is correct |
6 |
Correct |
289 ms |
28512 KB |
Output is correct |
7 |
Correct |
266 ms |
28148 KB |
Output is correct |
8 |
Correct |
277 ms |
28148 KB |
Output is correct |
9 |
Correct |
267 ms |
28276 KB |
Output is correct |
10 |
Correct |
271 ms |
28148 KB |
Output is correct |
11 |
Correct |
285 ms |
28404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
4984 KB |
Output is correct |
2 |
Correct |
8 ms |
4984 KB |
Output is correct |
3 |
Correct |
8 ms |
4984 KB |
Output is correct |
4 |
Correct |
8 ms |
5112 KB |
Output is correct |
5 |
Correct |
16 ms |
6264 KB |
Output is correct |
6 |
Correct |
320 ms |
28148 KB |
Output is correct |
7 |
Correct |
307 ms |
28276 KB |
Output is correct |
8 |
Correct |
311 ms |
28276 KB |
Output is correct |
9 |
Correct |
301 ms |
28148 KB |
Output is correct |
10 |
Correct |
8 ms |
4984 KB |
Output is correct |
11 |
Correct |
8 ms |
5240 KB |
Output is correct |
12 |
Correct |
15 ms |
6136 KB |
Output is correct |
13 |
Correct |
16 ms |
6264 KB |
Output is correct |
14 |
Correct |
325 ms |
28340 KB |
Output is correct |
15 |
Correct |
306 ms |
28148 KB |
Output is correct |
16 |
Correct |
306 ms |
28380 KB |
Output is correct |
17 |
Correct |
313 ms |
28276 KB |
Output is correct |
18 |
Correct |
330 ms |
28276 KB |
Output is correct |
19 |
Correct |
310 ms |
28276 KB |
Output is correct |
20 |
Correct |
318 ms |
28148 KB |
Output is correct |
21 |
Correct |
305 ms |
28144 KB |
Output is correct |
22 |
Correct |
306 ms |
28144 KB |
Output is correct |
23 |
Correct |
314 ms |
28296 KB |
Output is correct |
24 |
Correct |
306 ms |
28148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
4984 KB |
Output is correct |
2 |
Runtime error |
550 ms |
56332 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
4984 KB |
Output is correct |
2 |
Correct |
8 ms |
4984 KB |
Output is correct |
3 |
Correct |
8 ms |
5112 KB |
Output is correct |
4 |
Correct |
8 ms |
4984 KB |
Output is correct |
5 |
Correct |
8 ms |
4984 KB |
Output is correct |
6 |
Correct |
8 ms |
4984 KB |
Output is correct |
7 |
Correct |
9 ms |
4984 KB |
Output is correct |
8 |
Correct |
8 ms |
4984 KB |
Output is correct |
9 |
Correct |
8 ms |
4984 KB |
Output is correct |
10 |
Runtime error |
14 ms |
9976 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
4984 KB |
Output is correct |
2 |
Correct |
7 ms |
4984 KB |
Output is correct |
3 |
Correct |
8 ms |
4984 KB |
Output is correct |
4 |
Runtime error |
18 ms |
10232 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
5 |
Halted |
0 ms |
0 KB |
- |