#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const int __=3e5+1;
int n, m, par[__];
ll d[__], ans;
priority_queue<ll> a[__];
priority_queue<ll, vector<ll>, greater<ll>> b[__];
vector<pair<int,ll>> g[__];
int find(int u) {
if (par[u]==u) return u;
return par[u]=find(par[u]);
}
void merge(int u, int v) {
u = find(u), v = find(v);
if (u==v) return;
if (a[u].size()>a[v].size()) swap(u, v);
ll o = d[u]-d[v];
while (a[u].size()) {
ll x = a[u].top();
a[u].pop();
a[v].push(x+o);
}
par[u] = v;
}
void add(int u, ll r) {
int uu = u;
u = find(u);
ll x = d[u]+a[u].top();
if (r<x) {
a[u].push(r-d[u]);
a[u].pop();
b[uu].push(x);
ans += x-r;
} else {
b[uu].push(r);
}
}
ll dfs(int i, int p) {
vector<pair<ll, int>> v;
for (auto [j, x]: g[i])
if (j!=p) v.emplace_back(dfs(j, i), j);
sort(v.begin(), v.end(), greater<pair<ll, int>>());
for (auto [r, j]: v) {
if (par[i]==i) {
par[i] = j;
b[i].push(r);
} else {
add(i, r);
merge(i, j);
}
}
int pi = find(i);
ll r = b[i].size()? b[i].top() + d[i] : d[i];
ll l = a[pi].size()? a[pi].top() + d[pi] + d[i] : d[i];
if (a[pi].size()) a[pi].pop();
a[pi].push(l-d[pi]);
return r;
}
void init(int i, int p) {
for (auto [j, c]: g[i])
if (j!=p) {
d[j] = c;
init(j, i);
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i=1; i<=n+m; ++i) par[i] = i;
for (int i=2; i<=n+m; ++i) {
int p; ll c;
scanf("%d%lld", &p, &c);
g[i].emplace_back(p, c);
g[p].emplace_back(i, c);
}
init(1, 0);
dfs(1, 0);
printf("%lld\n", ans);
}
Compilation message
fireworks.cpp: In function 'int main()':
fireworks.cpp:74:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
74 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
fireworks.cpp:78:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | scanf("%d%lld", &p, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
26068 KB |
Output is correct |
2 |
Correct |
14 ms |
26200 KB |
Output is correct |
3 |
Correct |
14 ms |
26096 KB |
Output is correct |
4 |
Correct |
14 ms |
26132 KB |
Output is correct |
5 |
Correct |
14 ms |
26052 KB |
Output is correct |
6 |
Correct |
13 ms |
26164 KB |
Output is correct |
7 |
Correct |
13 ms |
26140 KB |
Output is correct |
8 |
Correct |
14 ms |
26144 KB |
Output is correct |
9 |
Correct |
14 ms |
26068 KB |
Output is correct |
10 |
Correct |
17 ms |
26068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
26140 KB |
Output is correct |
2 |
Correct |
15 ms |
26136 KB |
Output is correct |
3 |
Correct |
14 ms |
26068 KB |
Output is correct |
4 |
Correct |
13 ms |
26168 KB |
Output is correct |
5 |
Correct |
16 ms |
26064 KB |
Output is correct |
6 |
Correct |
15 ms |
26112 KB |
Output is correct |
7 |
Correct |
15 ms |
26068 KB |
Output is correct |
8 |
Correct |
14 ms |
26068 KB |
Output is correct |
9 |
Correct |
16 ms |
26072 KB |
Output is correct |
10 |
Correct |
17 ms |
26136 KB |
Output is correct |
11 |
Correct |
18 ms |
26068 KB |
Output is correct |
12 |
Correct |
17 ms |
26156 KB |
Output is correct |
13 |
Correct |
14 ms |
26128 KB |
Output is correct |
14 |
Correct |
15 ms |
26196 KB |
Output is correct |
15 |
Correct |
14 ms |
26068 KB |
Output is correct |
16 |
Correct |
15 ms |
26184 KB |
Output is correct |
17 |
Correct |
14 ms |
26068 KB |
Output is correct |
18 |
Correct |
14 ms |
26128 KB |
Output is correct |
19 |
Correct |
14 ms |
26176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
26068 KB |
Output is correct |
2 |
Correct |
14 ms |
26200 KB |
Output is correct |
3 |
Correct |
14 ms |
26096 KB |
Output is correct |
4 |
Correct |
14 ms |
26132 KB |
Output is correct |
5 |
Correct |
14 ms |
26052 KB |
Output is correct |
6 |
Correct |
13 ms |
26164 KB |
Output is correct |
7 |
Correct |
13 ms |
26140 KB |
Output is correct |
8 |
Correct |
14 ms |
26144 KB |
Output is correct |
9 |
Correct |
14 ms |
26068 KB |
Output is correct |
10 |
Correct |
17 ms |
26068 KB |
Output is correct |
11 |
Correct |
13 ms |
26140 KB |
Output is correct |
12 |
Correct |
15 ms |
26136 KB |
Output is correct |
13 |
Correct |
14 ms |
26068 KB |
Output is correct |
14 |
Correct |
13 ms |
26168 KB |
Output is correct |
15 |
Correct |
16 ms |
26064 KB |
Output is correct |
16 |
Correct |
15 ms |
26112 KB |
Output is correct |
17 |
Correct |
15 ms |
26068 KB |
Output is correct |
18 |
Correct |
14 ms |
26068 KB |
Output is correct |
19 |
Correct |
16 ms |
26072 KB |
Output is correct |
20 |
Correct |
17 ms |
26136 KB |
Output is correct |
21 |
Correct |
18 ms |
26068 KB |
Output is correct |
22 |
Correct |
17 ms |
26156 KB |
Output is correct |
23 |
Correct |
14 ms |
26128 KB |
Output is correct |
24 |
Correct |
15 ms |
26196 KB |
Output is correct |
25 |
Correct |
14 ms |
26068 KB |
Output is correct |
26 |
Correct |
15 ms |
26184 KB |
Output is correct |
27 |
Correct |
14 ms |
26068 KB |
Output is correct |
28 |
Correct |
14 ms |
26128 KB |
Output is correct |
29 |
Correct |
14 ms |
26176 KB |
Output is correct |
30 |
Correct |
14 ms |
26196 KB |
Output is correct |
31 |
Correct |
15 ms |
26224 KB |
Output is correct |
32 |
Correct |
15 ms |
26324 KB |
Output is correct |
33 |
Correct |
18 ms |
26368 KB |
Output is correct |
34 |
Correct |
19 ms |
26452 KB |
Output is correct |
35 |
Correct |
19 ms |
26532 KB |
Output is correct |
36 |
Correct |
16 ms |
26564 KB |
Output is correct |
37 |
Correct |
23 ms |
26664 KB |
Output is correct |
38 |
Correct |
18 ms |
26708 KB |
Output is correct |
39 |
Correct |
16 ms |
26740 KB |
Output is correct |
40 |
Correct |
16 ms |
27428 KB |
Output is correct |
41 |
Correct |
16 ms |
27348 KB |
Output is correct |
42 |
Correct |
16 ms |
26688 KB |
Output is correct |
43 |
Correct |
18 ms |
27004 KB |
Output is correct |
44 |
Correct |
18 ms |
26972 KB |
Output is correct |
45 |
Correct |
17 ms |
27016 KB |
Output is correct |
46 |
Correct |
16 ms |
26836 KB |
Output is correct |
47 |
Correct |
17 ms |
26836 KB |
Output is correct |
48 |
Correct |
17 ms |
26700 KB |
Output is correct |
49 |
Correct |
16 ms |
26792 KB |
Output is correct |
50 |
Correct |
18 ms |
26660 KB |
Output is correct |
51 |
Correct |
16 ms |
26708 KB |
Output is correct |
52 |
Correct |
16 ms |
26708 KB |
Output is correct |
53 |
Correct |
17 ms |
26836 KB |
Output is correct |
54 |
Correct |
23 ms |
26916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
26068 KB |
Output is correct |
2 |
Correct |
14 ms |
26200 KB |
Output is correct |
3 |
Correct |
14 ms |
26096 KB |
Output is correct |
4 |
Correct |
14 ms |
26132 KB |
Output is correct |
5 |
Correct |
14 ms |
26052 KB |
Output is correct |
6 |
Correct |
13 ms |
26164 KB |
Output is correct |
7 |
Correct |
13 ms |
26140 KB |
Output is correct |
8 |
Correct |
14 ms |
26144 KB |
Output is correct |
9 |
Correct |
14 ms |
26068 KB |
Output is correct |
10 |
Correct |
17 ms |
26068 KB |
Output is correct |
11 |
Correct |
13 ms |
26140 KB |
Output is correct |
12 |
Correct |
15 ms |
26136 KB |
Output is correct |
13 |
Correct |
14 ms |
26068 KB |
Output is correct |
14 |
Correct |
13 ms |
26168 KB |
Output is correct |
15 |
Correct |
16 ms |
26064 KB |
Output is correct |
16 |
Correct |
15 ms |
26112 KB |
Output is correct |
17 |
Correct |
15 ms |
26068 KB |
Output is correct |
18 |
Correct |
14 ms |
26068 KB |
Output is correct |
19 |
Correct |
16 ms |
26072 KB |
Output is correct |
20 |
Correct |
17 ms |
26136 KB |
Output is correct |
21 |
Correct |
18 ms |
26068 KB |
Output is correct |
22 |
Correct |
17 ms |
26156 KB |
Output is correct |
23 |
Correct |
14 ms |
26128 KB |
Output is correct |
24 |
Correct |
15 ms |
26196 KB |
Output is correct |
25 |
Correct |
14 ms |
26068 KB |
Output is correct |
26 |
Correct |
15 ms |
26184 KB |
Output is correct |
27 |
Correct |
14 ms |
26068 KB |
Output is correct |
28 |
Correct |
14 ms |
26128 KB |
Output is correct |
29 |
Correct |
14 ms |
26176 KB |
Output is correct |
30 |
Correct |
14 ms |
26196 KB |
Output is correct |
31 |
Correct |
15 ms |
26224 KB |
Output is correct |
32 |
Correct |
15 ms |
26324 KB |
Output is correct |
33 |
Correct |
18 ms |
26368 KB |
Output is correct |
34 |
Correct |
19 ms |
26452 KB |
Output is correct |
35 |
Correct |
19 ms |
26532 KB |
Output is correct |
36 |
Correct |
16 ms |
26564 KB |
Output is correct |
37 |
Correct |
23 ms |
26664 KB |
Output is correct |
38 |
Correct |
18 ms |
26708 KB |
Output is correct |
39 |
Correct |
16 ms |
26740 KB |
Output is correct |
40 |
Correct |
16 ms |
27428 KB |
Output is correct |
41 |
Correct |
16 ms |
27348 KB |
Output is correct |
42 |
Correct |
16 ms |
26688 KB |
Output is correct |
43 |
Correct |
18 ms |
27004 KB |
Output is correct |
44 |
Correct |
18 ms |
26972 KB |
Output is correct |
45 |
Correct |
17 ms |
27016 KB |
Output is correct |
46 |
Correct |
16 ms |
26836 KB |
Output is correct |
47 |
Correct |
17 ms |
26836 KB |
Output is correct |
48 |
Correct |
17 ms |
26700 KB |
Output is correct |
49 |
Correct |
16 ms |
26792 KB |
Output is correct |
50 |
Correct |
18 ms |
26660 KB |
Output is correct |
51 |
Correct |
16 ms |
26708 KB |
Output is correct |
52 |
Correct |
16 ms |
26708 KB |
Output is correct |
53 |
Correct |
17 ms |
26836 KB |
Output is correct |
54 |
Correct |
23 ms |
26916 KB |
Output is correct |
55 |
Correct |
28 ms |
27664 KB |
Output is correct |
56 |
Correct |
65 ms |
32176 KB |
Output is correct |
57 |
Correct |
89 ms |
36604 KB |
Output is correct |
58 |
Correct |
145 ms |
39512 KB |
Output is correct |
59 |
Correct |
171 ms |
43972 KB |
Output is correct |
60 |
Correct |
282 ms |
48376 KB |
Output is correct |
61 |
Correct |
270 ms |
51588 KB |
Output is correct |
62 |
Correct |
306 ms |
54136 KB |
Output is correct |
63 |
Correct |
381 ms |
59544 KB |
Output is correct |
64 |
Correct |
397 ms |
61232 KB |
Output is correct |
65 |
Correct |
164 ms |
104908 KB |
Output is correct |
66 |
Correct |
164 ms |
104964 KB |
Output is correct |
67 |
Correct |
225 ms |
58104 KB |
Output is correct |
68 |
Correct |
218 ms |
84828 KB |
Output is correct |
69 |
Correct |
262 ms |
81364 KB |
Output is correct |
70 |
Correct |
275 ms |
81312 KB |
Output is correct |
71 |
Correct |
234 ms |
72888 KB |
Output is correct |
72 |
Correct |
242 ms |
72800 KB |
Output is correct |
73 |
Correct |
202 ms |
68868 KB |
Output is correct |
74 |
Correct |
198 ms |
69500 KB |
Output is correct |
75 |
Correct |
200 ms |
67380 KB |
Output is correct |
76 |
Correct |
216 ms |
67484 KB |
Output is correct |
77 |
Correct |
200 ms |
66632 KB |
Output is correct |
78 |
Correct |
195 ms |
65396 KB |
Output is correct |
79 |
Correct |
261 ms |
71920 KB |
Output is correct |