#include "race.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
const int N = 200010;
bool bad[N];
long long weight[N];
vector <pair <int, int>> g[N];
int sub[N], ans, ptr, h[N], in[N], out[N], flat[N], target;
// compute subtree sizes
void trav (int u, int p = -1) {
sub[u] = 1;
for (auto [v, w] : g[u]) if (v != p and !bad[v]) {
trav(v, u);
sub[u] += sub[v];
}
}
// find centroid
int centroid (int u, int bound, int p = -1) {
for (auto [v, w] : g[u]) if (v != p and !bad[v] and sub[v] > bound) {
return centroid(v, bound, u);
}
return u;
}
// flatten the tree, compute information from root (centroid) to other nodes
void go (int u, int p = -1, int depth = 0, long long sum = 0) {
flat[++ptr] = u, in[u] = ptr;
h[u] = depth, weight[u] = sum;
for (auto [v, w] : g[u]) if (v != p and !bad[v]) {
go(v, u, depth + 1, sum + w);
}
out[u] = ptr;
}
// centroid decomposition routine
void decompose (int u = 0) {
trav(u);
int root = centroid(u, sub[u] / 2);
ptr = 0; go(root);
// data structure for merging two pieces
gp_hash_table <long long, int> smallest;
smallest[0] = 0;
for (auto [u, w] : g[root]) if (!bad[u]) {
// query
for (int i = in[u]; i <= out[u]; ++i) {
int v = flat[i];
long long required = target - weight[v];
if (smallest.find(required) != smallest.end()) {
ans = min(ans, h[v] + smallest[required]);
}
}
// update
for (int i = in[u]; i <= out[u]; ++i) {
int v = flat[i];
if (smallest.find(weight[v]) == smallest.end()) {
smallest[weight[v]] = h[v];
} else {
smallest[weight[v]] = min(smallest[weight[v]], h[v]);
}
}
}
// remove centroid and proceed recursively
bad[root] = true;
for (auto [v, w] : g[root]) if (!bad[v]) decompose(v);
}
int best_path (int N, int K, int H[][2], int L[]) {
for (int i = 0; i < N - 1; ++i) {
int u = H[i][0], v = H[i][1], w = L[i];
g[u].emplace_back(v, w), g[v].emplace_back(u, w);
}
ans = INT_MAX, target = K;
decompose();
if (ans == INT_MAX) ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12632 KB |
Output is correct |
4 |
Correct |
3 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12632 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12632 KB |
Output is correct |
13 |
Correct |
3 ms |
12636 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
3 ms |
12756 KB |
Output is correct |
16 |
Correct |
2 ms |
12636 KB |
Output is correct |
17 |
Correct |
2 ms |
12632 KB |
Output is correct |
18 |
Correct |
2 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12632 KB |
Output is correct |
4 |
Correct |
3 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12632 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12632 KB |
Output is correct |
13 |
Correct |
3 ms |
12636 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
3 ms |
12756 KB |
Output is correct |
16 |
Correct |
2 ms |
12636 KB |
Output is correct |
17 |
Correct |
2 ms |
12632 KB |
Output is correct |
18 |
Correct |
2 ms |
12636 KB |
Output is correct |
19 |
Correct |
2 ms |
12636 KB |
Output is correct |
20 |
Correct |
2 ms |
12740 KB |
Output is correct |
21 |
Correct |
3 ms |
12636 KB |
Output is correct |
22 |
Correct |
3 ms |
12892 KB |
Output is correct |
23 |
Correct |
3 ms |
12892 KB |
Output is correct |
24 |
Correct |
3 ms |
12892 KB |
Output is correct |
25 |
Correct |
4 ms |
12744 KB |
Output is correct |
26 |
Correct |
3 ms |
12892 KB |
Output is correct |
27 |
Correct |
3 ms |
12636 KB |
Output is correct |
28 |
Correct |
3 ms |
12892 KB |
Output is correct |
29 |
Correct |
3 ms |
12748 KB |
Output is correct |
30 |
Correct |
3 ms |
12892 KB |
Output is correct |
31 |
Correct |
3 ms |
12892 KB |
Output is correct |
32 |
Correct |
4 ms |
12788 KB |
Output is correct |
33 |
Correct |
3 ms |
12892 KB |
Output is correct |
34 |
Correct |
4 ms |
12892 KB |
Output is correct |
35 |
Correct |
3 ms |
12892 KB |
Output is correct |
36 |
Correct |
3 ms |
12752 KB |
Output is correct |
37 |
Correct |
4 ms |
12892 KB |
Output is correct |
38 |
Correct |
3 ms |
12920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12632 KB |
Output is correct |
4 |
Correct |
3 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12632 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12632 KB |
Output is correct |
13 |
Correct |
3 ms |
12636 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
3 ms |
12756 KB |
Output is correct |
16 |
Correct |
2 ms |
12636 KB |
Output is correct |
17 |
Correct |
2 ms |
12632 KB |
Output is correct |
18 |
Correct |
2 ms |
12636 KB |
Output is correct |
19 |
Correct |
115 ms |
19656 KB |
Output is correct |
20 |
Correct |
117 ms |
20372 KB |
Output is correct |
21 |
Correct |
116 ms |
20292 KB |
Output is correct |
22 |
Correct |
100 ms |
20820 KB |
Output is correct |
23 |
Correct |
129 ms |
21460 KB |
Output is correct |
24 |
Correct |
71 ms |
20816 KB |
Output is correct |
25 |
Correct |
127 ms |
24904 KB |
Output is correct |
26 |
Correct |
84 ms |
27472 KB |
Output is correct |
27 |
Correct |
184 ms |
26032 KB |
Output is correct |
28 |
Correct |
564 ms |
64708 KB |
Output is correct |
29 |
Correct |
556 ms |
63920 KB |
Output is correct |
30 |
Correct |
246 ms |
26660 KB |
Output is correct |
31 |
Correct |
207 ms |
26644 KB |
Output is correct |
32 |
Correct |
252 ms |
26560 KB |
Output is correct |
33 |
Correct |
275 ms |
26072 KB |
Output is correct |
34 |
Correct |
475 ms |
53632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12632 KB |
Output is correct |
4 |
Correct |
3 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12632 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12632 KB |
Output is correct |
13 |
Correct |
3 ms |
12636 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
3 ms |
12756 KB |
Output is correct |
16 |
Correct |
2 ms |
12636 KB |
Output is correct |
17 |
Correct |
2 ms |
12632 KB |
Output is correct |
18 |
Correct |
2 ms |
12636 KB |
Output is correct |
19 |
Correct |
2 ms |
12636 KB |
Output is correct |
20 |
Correct |
2 ms |
12740 KB |
Output is correct |
21 |
Correct |
3 ms |
12636 KB |
Output is correct |
22 |
Correct |
3 ms |
12892 KB |
Output is correct |
23 |
Correct |
3 ms |
12892 KB |
Output is correct |
24 |
Correct |
3 ms |
12892 KB |
Output is correct |
25 |
Correct |
4 ms |
12744 KB |
Output is correct |
26 |
Correct |
3 ms |
12892 KB |
Output is correct |
27 |
Correct |
3 ms |
12636 KB |
Output is correct |
28 |
Correct |
3 ms |
12892 KB |
Output is correct |
29 |
Correct |
3 ms |
12748 KB |
Output is correct |
30 |
Correct |
3 ms |
12892 KB |
Output is correct |
31 |
Correct |
3 ms |
12892 KB |
Output is correct |
32 |
Correct |
4 ms |
12788 KB |
Output is correct |
33 |
Correct |
3 ms |
12892 KB |
Output is correct |
34 |
Correct |
4 ms |
12892 KB |
Output is correct |
35 |
Correct |
3 ms |
12892 KB |
Output is correct |
36 |
Correct |
3 ms |
12752 KB |
Output is correct |
37 |
Correct |
4 ms |
12892 KB |
Output is correct |
38 |
Correct |
3 ms |
12920 KB |
Output is correct |
39 |
Correct |
115 ms |
19656 KB |
Output is correct |
40 |
Correct |
117 ms |
20372 KB |
Output is correct |
41 |
Correct |
116 ms |
20292 KB |
Output is correct |
42 |
Correct |
100 ms |
20820 KB |
Output is correct |
43 |
Correct |
129 ms |
21460 KB |
Output is correct |
44 |
Correct |
71 ms |
20816 KB |
Output is correct |
45 |
Correct |
127 ms |
24904 KB |
Output is correct |
46 |
Correct |
84 ms |
27472 KB |
Output is correct |
47 |
Correct |
184 ms |
26032 KB |
Output is correct |
48 |
Correct |
564 ms |
64708 KB |
Output is correct |
49 |
Correct |
556 ms |
63920 KB |
Output is correct |
50 |
Correct |
246 ms |
26660 KB |
Output is correct |
51 |
Correct |
207 ms |
26644 KB |
Output is correct |
52 |
Correct |
252 ms |
26560 KB |
Output is correct |
53 |
Correct |
275 ms |
26072 KB |
Output is correct |
54 |
Correct |
475 ms |
53632 KB |
Output is correct |
55 |
Correct |
13 ms |
13968 KB |
Output is correct |
56 |
Correct |
10 ms |
13148 KB |
Output is correct |
57 |
Correct |
89 ms |
19920 KB |
Output is correct |
58 |
Correct |
53 ms |
20680 KB |
Output is correct |
59 |
Correct |
138 ms |
43208 KB |
Output is correct |
60 |
Correct |
552 ms |
64156 KB |
Output is correct |
61 |
Correct |
223 ms |
27872 KB |
Output is correct |
62 |
Correct |
174 ms |
26656 KB |
Output is correct |
63 |
Correct |
229 ms |
26748 KB |
Output is correct |
64 |
Correct |
436 ms |
40880 KB |
Output is correct |
65 |
Correct |
420 ms |
54988 KB |
Output is correct |
66 |
Correct |
504 ms |
59364 KB |
Output is correct |
67 |
Correct |
97 ms |
27984 KB |
Output is correct |
68 |
Correct |
269 ms |
51232 KB |
Output is correct |
69 |
Correct |
245 ms |
49336 KB |
Output is correct |
70 |
Correct |
236 ms |
51368 KB |
Output is correct |