#include <bits/stdc++.h>
#ifdef LOCAL
#include "debug.h"
#else
#define dbg(...) 42
#include "race.h"
#endif
int best_path(int n, int k, int H[][2], int L[]) {
std::vector<std::vector<std::pair<int, int>>> g(n);
for (int i = 0; i + 1 < n; ++i) {
g[H[i][0]].emplace_back(H[i][1], L[i]);
g[H[i][1]].emplace_back(H[i][0], L[i]);
}
std::vector<int> size(n);
std::vector<bool> died(n, false);
auto dfs_sz = [&](auto& f, int v, int p) -> int {
size[v] = 1;
for (auto [u, _] : g[v]) {
if (u != p && !died[u]) {
size[v] += f(f, u, v);
}
}
return size[v];
};
auto centroid = [&](auto& f, int v, int p, int desired) -> int {
for (auto [u, _] : g[v]) {
if (u != p && !died[u] && size[u] >= desired) {
return f(f, u, v, desired);
}
}
return v;
};
constexpr auto inf = std::numeric_limits<int>::max() / 2;
std::vector<int> min_cnt(k + 1, inf);
auto dfs_calc = [&](auto& f, int v, int p, int tot_w, int cnt, bool do_update, std::set<int>& touched) -> int {
auto loc = inf;
if (tot_w > k) {
return loc;
}
if (do_update) {
min_cnt[tot_w] = std::min(min_cnt[tot_w], cnt);
touched.emplace(tot_w);
} else {
loc = std::min(loc, cnt + min_cnt[k - tot_w]);
}
for (auto [u, w] : g[v]) {
if (u != p && !died[u]) {
loc = std::min(loc, f(f, u, v, tot_w + w, cnt + 1, do_update, touched));
}
}
return loc;
};
auto CD = [&](auto& f, int v, int p) -> int {
auto c = centroid(centroid, v, p, dfs_sz(dfs_sz, v, p) / 2);
died[c] = true;
auto loc = inf;
std::set<int> touched;
for (auto [u, w] : g[c]) {
if (!died[u]) {
loc = std::min(loc, dfs_calc(dfs_calc, u, c, w, 1, false, touched));
assert(dfs_calc(dfs_calc, u, c, w, 1, true, touched) == inf);
}
}
for (auto w : touched) {
min_cnt[w] = inf;
}
for (auto [u, _] : g[c]) {
if (!died[u]) {
loc = std::min(loc, f(f, u, c));
}
}
return loc;
};
min_cnt[0] = 0;
auto res = CD(CD, 0, -1);
return res == inf ? -1 : res;
}
#ifdef LOCAL
void solve() {
int n, k;
std::cin >> n >> k;
int H[n][2], L[n];
for (int i = 0; i + 1 < n; ++i) {
std::cin >> H[i][0] >> H[i][1] >> L[i];
}
std::cout << best_path(n, k, H, L) << '\n';
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
solve();
}
#endif
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2496 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2444 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2496 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2648 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2496 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2444 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2496 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2648 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
2 ms |
5980 KB |
Output is correct |
23 |
Correct |
3 ms |
5320 KB |
Output is correct |
24 |
Correct |
3 ms |
5980 KB |
Output is correct |
25 |
Correct |
3 ms |
5832 KB |
Output is correct |
26 |
Correct |
2 ms |
3676 KB |
Output is correct |
27 |
Correct |
2 ms |
5720 KB |
Output is correct |
28 |
Correct |
2 ms |
3420 KB |
Output is correct |
29 |
Correct |
2 ms |
3676 KB |
Output is correct |
30 |
Correct |
3 ms |
4040 KB |
Output is correct |
31 |
Correct |
4 ms |
5212 KB |
Output is correct |
32 |
Correct |
3 ms |
5212 KB |
Output is correct |
33 |
Correct |
3 ms |
5724 KB |
Output is correct |
34 |
Correct |
2 ms |
4700 KB |
Output is correct |
35 |
Correct |
3 ms |
5724 KB |
Output is correct |
36 |
Correct |
2 ms |
6232 KB |
Output is correct |
37 |
Correct |
2 ms |
5724 KB |
Output is correct |
38 |
Correct |
2 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2496 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2444 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2496 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2648 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
123 ms |
12500 KB |
Output is correct |
20 |
Correct |
140 ms |
12496 KB |
Output is correct |
21 |
Correct |
131 ms |
12524 KB |
Output is correct |
22 |
Correct |
121 ms |
12624 KB |
Output is correct |
23 |
Correct |
55 ms |
12636 KB |
Output is correct |
24 |
Correct |
55 ms |
12636 KB |
Output is correct |
25 |
Correct |
106 ms |
13904 KB |
Output is correct |
26 |
Correct |
91 ms |
15700 KB |
Output is correct |
27 |
Correct |
141 ms |
20640 KB |
Output is correct |
28 |
Correct |
159 ms |
27380 KB |
Output is correct |
29 |
Correct |
161 ms |
26944 KB |
Output is correct |
30 |
Correct |
133 ms |
20564 KB |
Output is correct |
31 |
Correct |
164 ms |
20536 KB |
Output is correct |
32 |
Correct |
132 ms |
20712 KB |
Output is correct |
33 |
Correct |
184 ms |
19540 KB |
Output is correct |
34 |
Correct |
142 ms |
20300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2496 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2444 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2496 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2648 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
2 ms |
5980 KB |
Output is correct |
23 |
Correct |
3 ms |
5320 KB |
Output is correct |
24 |
Correct |
3 ms |
5980 KB |
Output is correct |
25 |
Correct |
3 ms |
5832 KB |
Output is correct |
26 |
Correct |
2 ms |
3676 KB |
Output is correct |
27 |
Correct |
2 ms |
5720 KB |
Output is correct |
28 |
Correct |
2 ms |
3420 KB |
Output is correct |
29 |
Correct |
2 ms |
3676 KB |
Output is correct |
30 |
Correct |
3 ms |
4040 KB |
Output is correct |
31 |
Correct |
4 ms |
5212 KB |
Output is correct |
32 |
Correct |
3 ms |
5212 KB |
Output is correct |
33 |
Correct |
3 ms |
5724 KB |
Output is correct |
34 |
Correct |
2 ms |
4700 KB |
Output is correct |
35 |
Correct |
3 ms |
5724 KB |
Output is correct |
36 |
Correct |
2 ms |
6232 KB |
Output is correct |
37 |
Correct |
2 ms |
5724 KB |
Output is correct |
38 |
Correct |
2 ms |
4444 KB |
Output is correct |
39 |
Correct |
123 ms |
12500 KB |
Output is correct |
40 |
Correct |
140 ms |
12496 KB |
Output is correct |
41 |
Correct |
131 ms |
12524 KB |
Output is correct |
42 |
Correct |
121 ms |
12624 KB |
Output is correct |
43 |
Correct |
55 ms |
12636 KB |
Output is correct |
44 |
Correct |
55 ms |
12636 KB |
Output is correct |
45 |
Correct |
106 ms |
13904 KB |
Output is correct |
46 |
Correct |
91 ms |
15700 KB |
Output is correct |
47 |
Correct |
141 ms |
20640 KB |
Output is correct |
48 |
Correct |
159 ms |
27380 KB |
Output is correct |
49 |
Correct |
161 ms |
26944 KB |
Output is correct |
50 |
Correct |
133 ms |
20564 KB |
Output is correct |
51 |
Correct |
164 ms |
20536 KB |
Output is correct |
52 |
Correct |
132 ms |
20712 KB |
Output is correct |
53 |
Correct |
184 ms |
19540 KB |
Output is correct |
54 |
Correct |
142 ms |
20300 KB |
Output is correct |
55 |
Correct |
11 ms |
3676 KB |
Output is correct |
56 |
Correct |
11 ms |
3164 KB |
Output is correct |
57 |
Correct |
88 ms |
12712 KB |
Output is correct |
58 |
Correct |
33 ms |
12232 KB |
Output is correct |
59 |
Correct |
196 ms |
23880 KB |
Output is correct |
60 |
Correct |
501 ms |
47952 KB |
Output is correct |
61 |
Correct |
200 ms |
20920 KB |
Output is correct |
62 |
Correct |
181 ms |
24804 KB |
Output is correct |
63 |
Correct |
219 ms |
24656 KB |
Output is correct |
64 |
Correct |
555 ms |
34216 KB |
Output is correct |
65 |
Correct |
151 ms |
21328 KB |
Output is correct |
66 |
Correct |
381 ms |
31356 KB |
Output is correct |
67 |
Correct |
81 ms |
25536 KB |
Output is correct |
68 |
Correct |
270 ms |
31992 KB |
Output is correct |
69 |
Correct |
270 ms |
32340 KB |
Output is correct |
70 |
Correct |
235 ms |
31316 KB |
Output is correct |