#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;
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];
}
}
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;
}
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;
}
void decompose (int u = 0) {
trav(u);
int root = centroid(u, sub[u] / 2);
ptr = 0; go(root);
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]);
}
}
}
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 |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5012 KB |
Output is correct |
3 |
Correct |
3 ms |
5060 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5008 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5012 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5008 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
4 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5016 KB |
Output is correct |
18 |
Correct |
3 ms |
5012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5012 KB |
Output is correct |
3 |
Correct |
3 ms |
5060 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5008 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5012 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5008 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
4 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5016 KB |
Output is correct |
18 |
Correct |
3 ms |
5012 KB |
Output is correct |
19 |
Correct |
3 ms |
5044 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5204 KB |
Output is correct |
22 |
Correct |
4 ms |
5204 KB |
Output is correct |
23 |
Correct |
5 ms |
5152 KB |
Output is correct |
24 |
Correct |
5 ms |
5204 KB |
Output is correct |
25 |
Correct |
6 ms |
5204 KB |
Output is correct |
26 |
Correct |
4 ms |
5204 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5204 KB |
Output is correct |
29 |
Correct |
4 ms |
5204 KB |
Output is correct |
30 |
Correct |
4 ms |
5204 KB |
Output is correct |
31 |
Correct |
4 ms |
5204 KB |
Output is correct |
32 |
Correct |
4 ms |
5148 KB |
Output is correct |
33 |
Correct |
4 ms |
5204 KB |
Output is correct |
34 |
Correct |
4 ms |
5204 KB |
Output is correct |
35 |
Correct |
5 ms |
5204 KB |
Output is correct |
36 |
Correct |
4 ms |
5204 KB |
Output is correct |
37 |
Correct |
4 ms |
5212 KB |
Output is correct |
38 |
Correct |
5 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5012 KB |
Output is correct |
3 |
Correct |
3 ms |
5060 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5008 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5012 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5008 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
4 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5016 KB |
Output is correct |
18 |
Correct |
3 ms |
5012 KB |
Output is correct |
19 |
Correct |
195 ms |
14172 KB |
Output is correct |
20 |
Correct |
178 ms |
14176 KB |
Output is correct |
21 |
Correct |
224 ms |
14160 KB |
Output is correct |
22 |
Correct |
159 ms |
14180 KB |
Output is correct |
23 |
Correct |
178 ms |
14764 KB |
Output is correct |
24 |
Correct |
123 ms |
14376 KB |
Output is correct |
25 |
Correct |
183 ms |
18304 KB |
Output is correct |
26 |
Correct |
106 ms |
21424 KB |
Output is correct |
27 |
Correct |
276 ms |
23820 KB |
Output is correct |
28 |
Correct |
720 ms |
62572 KB |
Output is correct |
29 |
Correct |
684 ms |
61492 KB |
Output is correct |
30 |
Correct |
271 ms |
23692 KB |
Output is correct |
31 |
Correct |
237 ms |
23800 KB |
Output is correct |
32 |
Correct |
314 ms |
23796 KB |
Output is correct |
33 |
Correct |
351 ms |
23176 KB |
Output is correct |
34 |
Correct |
569 ms |
50964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5012 KB |
Output is correct |
3 |
Correct |
3 ms |
5060 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5008 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5012 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5008 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
4 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5016 KB |
Output is correct |
18 |
Correct |
3 ms |
5012 KB |
Output is correct |
19 |
Correct |
3 ms |
5044 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5204 KB |
Output is correct |
22 |
Correct |
4 ms |
5204 KB |
Output is correct |
23 |
Correct |
5 ms |
5152 KB |
Output is correct |
24 |
Correct |
5 ms |
5204 KB |
Output is correct |
25 |
Correct |
6 ms |
5204 KB |
Output is correct |
26 |
Correct |
4 ms |
5204 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5204 KB |
Output is correct |
29 |
Correct |
4 ms |
5204 KB |
Output is correct |
30 |
Correct |
4 ms |
5204 KB |
Output is correct |
31 |
Correct |
4 ms |
5204 KB |
Output is correct |
32 |
Correct |
4 ms |
5148 KB |
Output is correct |
33 |
Correct |
4 ms |
5204 KB |
Output is correct |
34 |
Correct |
4 ms |
5204 KB |
Output is correct |
35 |
Correct |
5 ms |
5204 KB |
Output is correct |
36 |
Correct |
4 ms |
5204 KB |
Output is correct |
37 |
Correct |
4 ms |
5212 KB |
Output is correct |
38 |
Correct |
5 ms |
5204 KB |
Output is correct |
39 |
Correct |
195 ms |
14172 KB |
Output is correct |
40 |
Correct |
178 ms |
14176 KB |
Output is correct |
41 |
Correct |
224 ms |
14160 KB |
Output is correct |
42 |
Correct |
159 ms |
14180 KB |
Output is correct |
43 |
Correct |
178 ms |
14764 KB |
Output is correct |
44 |
Correct |
123 ms |
14376 KB |
Output is correct |
45 |
Correct |
183 ms |
18304 KB |
Output is correct |
46 |
Correct |
106 ms |
21424 KB |
Output is correct |
47 |
Correct |
276 ms |
23820 KB |
Output is correct |
48 |
Correct |
720 ms |
62572 KB |
Output is correct |
49 |
Correct |
684 ms |
61492 KB |
Output is correct |
50 |
Correct |
271 ms |
23692 KB |
Output is correct |
51 |
Correct |
237 ms |
23800 KB |
Output is correct |
52 |
Correct |
314 ms |
23796 KB |
Output is correct |
53 |
Correct |
351 ms |
23176 KB |
Output is correct |
54 |
Correct |
569 ms |
50964 KB |
Output is correct |
55 |
Correct |
16 ms |
6740 KB |
Output is correct |
56 |
Correct |
14 ms |
5956 KB |
Output is correct |
57 |
Correct |
124 ms |
14400 KB |
Output is correct |
58 |
Correct |
48 ms |
13980 KB |
Output is correct |
59 |
Correct |
161 ms |
36700 KB |
Output is correct |
60 |
Correct |
659 ms |
61636 KB |
Output is correct |
61 |
Correct |
277 ms |
25924 KB |
Output is correct |
62 |
Correct |
239 ms |
24004 KB |
Output is correct |
63 |
Correct |
328 ms |
23808 KB |
Output is correct |
64 |
Correct |
587 ms |
40036 KB |
Output is correct |
65 |
Correct |
503 ms |
52212 KB |
Output is correct |
66 |
Correct |
709 ms |
58912 KB |
Output is correct |
67 |
Correct |
162 ms |
24376 KB |
Output is correct |
68 |
Correct |
373 ms |
48784 KB |
Output is correct |
69 |
Correct |
380 ms |
49064 KB |
Output is correct |
70 |
Correct |
308 ms |
48012 KB |
Output is correct |