#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define eb(x...) emplace_back(x)
bool ckmin(auto &a, const auto &b) { return a > b ? a = b, 1 : 0; }
int best_path(int n, int k, int h[][2], int l[]) {
vector<bool> dead(n);
vector<int> mn(k + 1), sz(n);
vector<vector<pair<int, int>>> g(n);
rep(e, 0, n - 1) {
g[h[e][0]].eb(h[e][1], l[e]);
g[h[e][1]].eb(h[e][0], l[e]);
}
int ans = n;
function<int(int)> centroid = [&](int root) {
function<void(int, int)> init = [&](int u, int p) {
sz[u] = 1;
for (auto &[v, d] : g[u])
if (v != p && !dead[v])
init(v, u), sz[u] += sz[v];
};
init(root, -1);
function<int(int, int)> dfs = [&](int u, int p) {
for (auto &[v, d] : g[u])
if (v != p && !dead[v] && 2 * sz[v] > sz[root])
return dfs(v, u);
return u;
};
return dfs(root, -1);
};
function<void(int)> solve = [&](int s) {
int root = centroid(s);
function<void(int, int, int)> clear = [&](int u, int p, int dist) {
if (dist > k) return;
mn[dist] = mn[k - dist] = n;
for (auto &[v, d] : g[u])
if (v != p && !dead[v])
clear(v, u, dist + d);
};
clear(root, -1, 0);
vector<pair<int, int>> lazy; int dep = 0;
function<void(int, int, int)> dfs = [&](int u, int p, int dist) {
if (dist > k) return;
ckmin(ans, dep + mn[k - dist]);
lazy.eb(dist, dep++);
for (auto &[v, d] : g[u]) {
if (v == p || dead[v]) continue;
dfs(v, u, dist + d);
if (u != root) continue;
for (auto &[dist, dep] : lazy) ckmin(mn[dist], dep);
lazy.clear();
}
--dep;
};
mn[0] = 0; dfs(root, -1, 0);
dead[root] = true;
for (auto &[v, d] : g[root]) if (!dead[v]) solve(v);
};
solve(0);
return ans != n ? ans : -1;
}
Compilation message
race.cpp: In lambda function:
race.cpp:24:20: warning: unused variable 'd' [-Wunused-variable]
for (auto &[v, d] : g[u])
^
race.cpp: In lambda function:
race.cpp:31:20: warning: unused variable 'd' [-Wunused-variable]
for (auto &[v, d] : g[u])
^
race.cpp: In lambda function:
race.cpp:68:19: warning: unused variable 'd' [-Wunused-variable]
for (auto &[v, d] : g[root]) if (!dead[v]) solve(v);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
436 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
436 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
3 ms |
3968 KB |
Output is correct |
23 |
Correct |
3 ms |
3328 KB |
Output is correct |
24 |
Correct |
5 ms |
3840 KB |
Output is correct |
25 |
Correct |
4 ms |
3712 KB |
Output is correct |
26 |
Correct |
2 ms |
1664 KB |
Output is correct |
27 |
Correct |
3 ms |
3584 KB |
Output is correct |
28 |
Correct |
2 ms |
1152 KB |
Output is correct |
29 |
Correct |
2 ms |
1664 KB |
Output is correct |
30 |
Correct |
2 ms |
1792 KB |
Output is correct |
31 |
Correct |
3 ms |
2944 KB |
Output is correct |
32 |
Correct |
3 ms |
3200 KB |
Output is correct |
33 |
Correct |
3 ms |
3456 KB |
Output is correct |
34 |
Correct |
3 ms |
2688 KB |
Output is correct |
35 |
Correct |
3 ms |
3584 KB |
Output is correct |
36 |
Correct |
4 ms |
4096 KB |
Output is correct |
37 |
Correct |
3 ms |
3584 KB |
Output is correct |
38 |
Correct |
3 ms |
2432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
436 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
201 ms |
8056 KB |
Output is correct |
20 |
Correct |
230 ms |
8056 KB |
Output is correct |
21 |
Correct |
258 ms |
8372 KB |
Output is correct |
22 |
Correct |
241 ms |
8696 KB |
Output is correct |
23 |
Correct |
138 ms |
8184 KB |
Output is correct |
24 |
Correct |
108 ms |
8132 KB |
Output is correct |
25 |
Correct |
213 ms |
12408 KB |
Output is correct |
26 |
Correct |
183 ms |
17740 KB |
Output is correct |
27 |
Correct |
303 ms |
15736 KB |
Output is correct |
28 |
Correct |
418 ms |
33264 KB |
Output is correct |
29 |
Correct |
476 ms |
31736 KB |
Output is correct |
30 |
Correct |
286 ms |
15864 KB |
Output is correct |
31 |
Correct |
290 ms |
15868 KB |
Output is correct |
32 |
Correct |
348 ms |
15736 KB |
Output is correct |
33 |
Correct |
369 ms |
14968 KB |
Output is correct |
34 |
Correct |
291 ms |
14584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
436 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
3 ms |
3968 KB |
Output is correct |
23 |
Correct |
3 ms |
3328 KB |
Output is correct |
24 |
Correct |
5 ms |
3840 KB |
Output is correct |
25 |
Correct |
4 ms |
3712 KB |
Output is correct |
26 |
Correct |
2 ms |
1664 KB |
Output is correct |
27 |
Correct |
3 ms |
3584 KB |
Output is correct |
28 |
Correct |
2 ms |
1152 KB |
Output is correct |
29 |
Correct |
2 ms |
1664 KB |
Output is correct |
30 |
Correct |
2 ms |
1792 KB |
Output is correct |
31 |
Correct |
3 ms |
2944 KB |
Output is correct |
32 |
Correct |
3 ms |
3200 KB |
Output is correct |
33 |
Correct |
3 ms |
3456 KB |
Output is correct |
34 |
Correct |
3 ms |
2688 KB |
Output is correct |
35 |
Correct |
3 ms |
3584 KB |
Output is correct |
36 |
Correct |
4 ms |
4096 KB |
Output is correct |
37 |
Correct |
3 ms |
3584 KB |
Output is correct |
38 |
Correct |
3 ms |
2432 KB |
Output is correct |
39 |
Correct |
201 ms |
8056 KB |
Output is correct |
40 |
Correct |
230 ms |
8056 KB |
Output is correct |
41 |
Correct |
258 ms |
8372 KB |
Output is correct |
42 |
Correct |
241 ms |
8696 KB |
Output is correct |
43 |
Correct |
138 ms |
8184 KB |
Output is correct |
44 |
Correct |
108 ms |
8132 KB |
Output is correct |
45 |
Correct |
213 ms |
12408 KB |
Output is correct |
46 |
Correct |
183 ms |
17740 KB |
Output is correct |
47 |
Correct |
303 ms |
15736 KB |
Output is correct |
48 |
Correct |
418 ms |
33264 KB |
Output is correct |
49 |
Correct |
476 ms |
31736 KB |
Output is correct |
50 |
Correct |
286 ms |
15864 KB |
Output is correct |
51 |
Correct |
290 ms |
15868 KB |
Output is correct |
52 |
Correct |
348 ms |
15736 KB |
Output is correct |
53 |
Correct |
369 ms |
14968 KB |
Output is correct |
54 |
Correct |
291 ms |
14584 KB |
Output is correct |
55 |
Correct |
14 ms |
1152 KB |
Output is correct |
56 |
Correct |
15 ms |
1152 KB |
Output is correct |
57 |
Correct |
152 ms |
8824 KB |
Output is correct |
58 |
Correct |
67 ms |
8172 KB |
Output is correct |
59 |
Correct |
182 ms |
18516 KB |
Output is correct |
60 |
Correct |
751 ms |
38256 KB |
Output is correct |
61 |
Correct |
296 ms |
15736 KB |
Output is correct |
62 |
Correct |
319 ms |
19584 KB |
Output is correct |
63 |
Correct |
473 ms |
19832 KB |
Output is correct |
64 |
Correct |
720 ms |
19316 KB |
Output is correct |
65 |
Correct |
409 ms |
15736 KB |
Output is correct |
66 |
Correct |
620 ms |
32760 KB |
Output is correct |
67 |
Correct |
285 ms |
19944 KB |
Output is correct |
68 |
Correct |
450 ms |
19708 KB |
Output is correct |
69 |
Correct |
482 ms |
19832 KB |
Output is correct |
70 |
Correct |
401 ms |
19192 KB |
Output is correct |