#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 {
ll av = a.fi.fi * b.fi.se;
ll 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];
int en = s.erase((entry){ { 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:62:7: warning: unused variable 'en' [-Wunused-variable]
int en = s.erase((entry){ { uu[f(par)], dd[f(par)] }, f(par) });
^~
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 |
Correct |
8 ms |
5112 KB |
Output is correct |
5 |
Correct |
99 ms |
11640 KB |
Output is correct |
6 |
Correct |
233 ms |
17780 KB |
Output is correct |
7 |
Correct |
422 ms |
24564 KB |
Output is correct |
8 |
Correct |
605 ms |
30576 KB |
Output is correct |
9 |
Correct |
601 ms |
30720 KB |
Output is correct |
10 |
Correct |
654 ms |
30704 KB |
Output is correct |
11 |
Correct |
8 ms |
4984 KB |
Output is correct |
12 |
Correct |
661 ms |
30832 KB |
Output is correct |
13 |
Correct |
344 ms |
35820 KB |
Output is correct |
14 |
Correct |
364 ms |
30200 KB |
Output is correct |
15 |
Correct |
225 ms |
35832 KB |
Output is correct |
16 |
Correct |
276 ms |
29608 KB |
Output is correct |
17 |
Correct |
656 ms |
30668 KB |
Output is correct |
18 |
Correct |
678 ms |
30704 KB |
Output is correct |
19 |
Correct |
208 ms |
35704 KB |
Output is correct |
20 |
Correct |
190 ms |
29304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5112 KB |
Output is correct |
2 |
Correct |
8 ms |
4984 KB |
Output is correct |
3 |
Correct |
8 ms |
4984 KB |
Output is correct |
4 |
Correct |
299 ms |
29044 KB |
Output is correct |
5 |
Correct |
295 ms |
29188 KB |
Output is correct |
6 |
Correct |
290 ms |
29044 KB |
Output is correct |
7 |
Correct |
314 ms |
29044 KB |
Output is correct |
8 |
Correct |
308 ms |
29008 KB |
Output is correct |
9 |
Correct |
318 ms |
29044 KB |
Output is correct |
10 |
Correct |
293 ms |
29148 KB |
Output is correct |
11 |
Correct |
305 ms |
29044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 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 |
Correct |
8 ms |
5112 KB |
Output is correct |
5 |
Correct |
16 ms |
6264 KB |
Output is correct |
6 |
Correct |
301 ms |
29684 KB |
Output is correct |
7 |
Correct |
272 ms |
29684 KB |
Output is correct |
8 |
Correct |
284 ms |
29728 KB |
Output is correct |
9 |
Correct |
272 ms |
29684 KB |
Output is correct |
10 |
Correct |
8 ms |
4984 KB |
Output is correct |
11 |
Correct |
9 ms |
5240 KB |
Output is correct |
12 |
Correct |
14 ms |
6136 KB |
Output is correct |
13 |
Correct |
16 ms |
6264 KB |
Output is correct |
14 |
Correct |
279 ms |
29684 KB |
Output is correct |
15 |
Correct |
280 ms |
29556 KB |
Output is correct |
16 |
Correct |
284 ms |
29684 KB |
Output is correct |
17 |
Correct |
297 ms |
29556 KB |
Output is correct |
18 |
Correct |
292 ms |
29684 KB |
Output is correct |
19 |
Correct |
280 ms |
29556 KB |
Output is correct |
20 |
Correct |
295 ms |
29828 KB |
Output is correct |
21 |
Correct |
274 ms |
29684 KB |
Output is correct |
22 |
Correct |
280 ms |
29556 KB |
Output is correct |
23 |
Correct |
294 ms |
29684 KB |
Output is correct |
24 |
Correct |
277 ms |
29640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
4984 KB |
Output is correct |
2 |
Correct |
657 ms |
30576 KB |
Output is correct |
3 |
Correct |
628 ms |
30704 KB |
Output is correct |
4 |
Correct |
608 ms |
30704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
4988 KB |
Output is correct |
2 |
Correct |
7 ms |
4984 KB |
Output is correct |
3 |
Correct |
8 ms |
4984 KB |
Output is correct |
4 |
Correct |
8 ms |
4984 KB |
Output is correct |
5 |
Correct |
8 ms |
4984 KB |
Output is correct |
6 |
Correct |
7 ms |
5112 KB |
Output is correct |
7 |
Correct |
8 ms |
4984 KB |
Output is correct |
8 |
Correct |
8 ms |
4984 KB |
Output is correct |
9 |
Correct |
9 ms |
4984 KB |
Output is correct |
10 |
Correct |
8 ms |
4984 KB |
Output is correct |
11 |
Correct |
8 ms |
5112 KB |
Output is correct |
12 |
Correct |
8 ms |
4984 KB |
Output is correct |
13 |
Correct |
7 ms |
4984 KB |
Output is correct |
14 |
Correct |
7 ms |
4984 KB |
Output is correct |
15 |
Correct |
8 ms |
4984 KB |
Output is correct |
16 |
Correct |
8 ms |
5112 KB |
Output is correct |
17 |
Correct |
8 ms |
4984 KB |
Output is correct |
# |
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 |
Correct |
8 ms |
5112 KB |
Output is correct |
5 |
Correct |
99 ms |
11640 KB |
Output is correct |
6 |
Correct |
233 ms |
17780 KB |
Output is correct |
7 |
Correct |
422 ms |
24564 KB |
Output is correct |
8 |
Correct |
605 ms |
30576 KB |
Output is correct |
9 |
Correct |
601 ms |
30720 KB |
Output is correct |
10 |
Correct |
654 ms |
30704 KB |
Output is correct |
11 |
Correct |
8 ms |
4984 KB |
Output is correct |
12 |
Correct |
661 ms |
30832 KB |
Output is correct |
13 |
Correct |
344 ms |
35820 KB |
Output is correct |
14 |
Correct |
364 ms |
30200 KB |
Output is correct |
15 |
Correct |
225 ms |
35832 KB |
Output is correct |
16 |
Correct |
276 ms |
29608 KB |
Output is correct |
17 |
Correct |
656 ms |
30668 KB |
Output is correct |
18 |
Correct |
678 ms |
30704 KB |
Output is correct |
19 |
Correct |
208 ms |
35704 KB |
Output is correct |
20 |
Correct |
190 ms |
29304 KB |
Output is correct |
21 |
Correct |
8 ms |
5112 KB |
Output is correct |
22 |
Correct |
8 ms |
4984 KB |
Output is correct |
23 |
Correct |
8 ms |
4984 KB |
Output is correct |
24 |
Correct |
299 ms |
29044 KB |
Output is correct |
25 |
Correct |
295 ms |
29188 KB |
Output is correct |
26 |
Correct |
290 ms |
29044 KB |
Output is correct |
27 |
Correct |
314 ms |
29044 KB |
Output is correct |
28 |
Correct |
308 ms |
29008 KB |
Output is correct |
29 |
Correct |
318 ms |
29044 KB |
Output is correct |
30 |
Correct |
293 ms |
29148 KB |
Output is correct |
31 |
Correct |
305 ms |
29044 KB |
Output is correct |
32 |
Correct |
7 ms |
4984 KB |
Output is correct |
33 |
Correct |
7 ms |
4984 KB |
Output is correct |
34 |
Correct |
8 ms |
4984 KB |
Output is correct |
35 |
Correct |
8 ms |
5112 KB |
Output is correct |
36 |
Correct |
16 ms |
6264 KB |
Output is correct |
37 |
Correct |
301 ms |
29684 KB |
Output is correct |
38 |
Correct |
272 ms |
29684 KB |
Output is correct |
39 |
Correct |
284 ms |
29728 KB |
Output is correct |
40 |
Correct |
272 ms |
29684 KB |
Output is correct |
41 |
Correct |
8 ms |
4984 KB |
Output is correct |
42 |
Correct |
9 ms |
5240 KB |
Output is correct |
43 |
Correct |
14 ms |
6136 KB |
Output is correct |
44 |
Correct |
16 ms |
6264 KB |
Output is correct |
45 |
Correct |
279 ms |
29684 KB |
Output is correct |
46 |
Correct |
280 ms |
29556 KB |
Output is correct |
47 |
Correct |
284 ms |
29684 KB |
Output is correct |
48 |
Correct |
297 ms |
29556 KB |
Output is correct |
49 |
Correct |
292 ms |
29684 KB |
Output is correct |
50 |
Correct |
280 ms |
29556 KB |
Output is correct |
51 |
Correct |
295 ms |
29828 KB |
Output is correct |
52 |
Correct |
274 ms |
29684 KB |
Output is correct |
53 |
Correct |
280 ms |
29556 KB |
Output is correct |
54 |
Correct |
294 ms |
29684 KB |
Output is correct |
55 |
Correct |
277 ms |
29640 KB |
Output is correct |
56 |
Correct |
8 ms |
4984 KB |
Output is correct |
57 |
Correct |
657 ms |
30576 KB |
Output is correct |
58 |
Correct |
628 ms |
30704 KB |
Output is correct |
59 |
Correct |
608 ms |
30704 KB |
Output is correct |
60 |
Correct |
7 ms |
4988 KB |
Output is correct |
61 |
Correct |
7 ms |
4984 KB |
Output is correct |
62 |
Correct |
8 ms |
4984 KB |
Output is correct |
63 |
Correct |
8 ms |
4984 KB |
Output is correct |
64 |
Correct |
8 ms |
4984 KB |
Output is correct |
65 |
Correct |
7 ms |
5112 KB |
Output is correct |
66 |
Correct |
8 ms |
4984 KB |
Output is correct |
67 |
Correct |
8 ms |
4984 KB |
Output is correct |
68 |
Correct |
9 ms |
4984 KB |
Output is correct |
69 |
Correct |
8 ms |
4984 KB |
Output is correct |
70 |
Correct |
8 ms |
5112 KB |
Output is correct |
71 |
Correct |
8 ms |
4984 KB |
Output is correct |
72 |
Correct |
7 ms |
4984 KB |
Output is correct |
73 |
Correct |
7 ms |
4984 KB |
Output is correct |
74 |
Correct |
8 ms |
4984 KB |
Output is correct |
75 |
Correct |
8 ms |
5112 KB |
Output is correct |
76 |
Correct |
8 ms |
4984 KB |
Output is correct |
77 |
Correct |
8 ms |
4984 KB |
Output is correct |
78 |
Correct |
8 ms |
4984 KB |
Output is correct |
79 |
Correct |
8 ms |
4984 KB |
Output is correct |
80 |
Correct |
7 ms |
4984 KB |
Output is correct |
81 |
Correct |
7 ms |
4984 KB |
Output is correct |
82 |
Correct |
7 ms |
4984 KB |
Output is correct |
83 |
Correct |
8 ms |
4984 KB |
Output is correct |
84 |
Correct |
8 ms |
4984 KB |
Output is correct |
85 |
Correct |
8 ms |
4988 KB |
Output is correct |
86 |
Correct |
7 ms |
4984 KB |
Output is correct |
87 |
Correct |
7 ms |
4984 KB |
Output is correct |
88 |
Correct |
12 ms |
5752 KB |
Output is correct |
89 |
Correct |
16 ms |
6008 KB |
Output is correct |
90 |
Correct |
19 ms |
6392 KB |
Output is correct |
91 |
Correct |
82 ms |
11640 KB |
Output is correct |
92 |
Correct |
187 ms |
17912 KB |
Output is correct |
93 |
Correct |
503 ms |
30840 KB |
Output is correct |
94 |
Correct |
496 ms |
30952 KB |
Output is correct |
95 |
Correct |
494 ms |
31128 KB |
Output is correct |
96 |
Correct |
494 ms |
30968 KB |
Output is correct |
97 |
Correct |
450 ms |
30928 KB |
Output is correct |
98 |
Correct |
434 ms |
31144 KB |
Output is correct |
99 |
Correct |
379 ms |
31468 KB |
Output is correct |
100 |
Correct |
527 ms |
31116 KB |
Output is correct |
101 |
Correct |
550 ms |
31108 KB |
Output is correct |
102 |
Correct |
293 ms |
30452 KB |
Output is correct |
103 |
Correct |
547 ms |
30832 KB |
Output is correct |
104 |
Correct |
508 ms |
30704 KB |
Output is correct |
105 |
Correct |
262 ms |
30896 KB |
Output is correct |
106 |
Correct |
272 ms |
29812 KB |
Output is correct |