#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 3;
int n, k;
vector<pair<int, int>> g[N];
bool vst[N];
int sz[N], min_edge[5 * N], ans = N;
vector<int> tmp;
int get_sz(int u, int p) {
sz[u] = 1;
for (auto [v, w]: g[u]) if (v != p && !vst[v])
sz[u] += get_sz(v, u);
return sz[u];
}
int centroid(int u, int p, int csz) {
for (auto [v, w]: g[u]) if (v != p && !vst[v]) {
if (sz[v] > csz / 2) return centroid(v, u, csz);
}
return u;
}
void dfs(int u, int p, int cur_len, int cnt, bool get_ans) {
if (cur_len > k) return;
if (get_ans) ans = min(ans, cnt + min_edge[k - cur_len]);
else {
tmp.push_back(cur_len);
min_edge[cur_len] = min(min_edge[cur_len], cnt);
}
for (auto [v, w]: g[u]) if (v != p && !vst[v])
dfs(v, u, cur_len + w, cnt + 1, get_ans);
}
void build_cd(int u) {
u = centroid(u, 0, get_sz(u, 0));
vst[u] = true;
for (auto [v, w]: g[u]) if (!vst[v]) {
dfs(v, u, w, 1, 1);
dfs(v, u, w, 1, 0);
}
ans = min(ans, min_edge[k]);
while (!tmp.empty()) {
min_edge[tmp.back()] = N;
tmp.pop_back();
}
for (auto [v, w]: g[u]) if (!vst[v])
build_cd(v);
}
int solve() {
for (int i = 0; i < 5 * N; ++i)
min_edge[i] = N;
build_cd(1);
return (ans != N ? ans : -1);
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N, k = K;
for (int i = 0; i < N - 1; ++i) {
g[H[i][0] + 1].emplace_back(H[i][1] + 1, L[i]);
g[H[i][1] + 1].emplace_back(H[i][0] + 1, L[i]);
}
return solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8908 KB |
Output is correct |
2 |
Correct |
4 ms |
8908 KB |
Output is correct |
3 |
Correct |
5 ms |
8908 KB |
Output is correct |
4 |
Correct |
4 ms |
8868 KB |
Output is correct |
5 |
Correct |
5 ms |
8908 KB |
Output is correct |
6 |
Correct |
4 ms |
8908 KB |
Output is correct |
7 |
Correct |
4 ms |
8908 KB |
Output is correct |
8 |
Correct |
5 ms |
8908 KB |
Output is correct |
9 |
Correct |
4 ms |
8908 KB |
Output is correct |
10 |
Correct |
5 ms |
8940 KB |
Output is correct |
11 |
Correct |
4 ms |
8908 KB |
Output is correct |
12 |
Correct |
5 ms |
8908 KB |
Output is correct |
13 |
Correct |
4 ms |
8908 KB |
Output is correct |
14 |
Correct |
5 ms |
8908 KB |
Output is correct |
15 |
Correct |
4 ms |
8908 KB |
Output is correct |
16 |
Correct |
4 ms |
8840 KB |
Output is correct |
17 |
Correct |
4 ms |
8908 KB |
Output is correct |
18 |
Correct |
4 ms |
8908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8908 KB |
Output is correct |
2 |
Correct |
4 ms |
8908 KB |
Output is correct |
3 |
Correct |
5 ms |
8908 KB |
Output is correct |
4 |
Correct |
4 ms |
8868 KB |
Output is correct |
5 |
Correct |
5 ms |
8908 KB |
Output is correct |
6 |
Correct |
4 ms |
8908 KB |
Output is correct |
7 |
Correct |
4 ms |
8908 KB |
Output is correct |
8 |
Correct |
5 ms |
8908 KB |
Output is correct |
9 |
Correct |
4 ms |
8908 KB |
Output is correct |
10 |
Correct |
5 ms |
8940 KB |
Output is correct |
11 |
Correct |
4 ms |
8908 KB |
Output is correct |
12 |
Correct |
5 ms |
8908 KB |
Output is correct |
13 |
Correct |
4 ms |
8908 KB |
Output is correct |
14 |
Correct |
5 ms |
8908 KB |
Output is correct |
15 |
Correct |
4 ms |
8908 KB |
Output is correct |
16 |
Correct |
4 ms |
8840 KB |
Output is correct |
17 |
Correct |
4 ms |
8908 KB |
Output is correct |
18 |
Correct |
4 ms |
8908 KB |
Output is correct |
19 |
Correct |
6 ms |
8908 KB |
Output is correct |
20 |
Correct |
5 ms |
8908 KB |
Output is correct |
21 |
Correct |
5 ms |
8908 KB |
Output is correct |
22 |
Correct |
5 ms |
8908 KB |
Output is correct |
23 |
Correct |
5 ms |
8908 KB |
Output is correct |
24 |
Correct |
5 ms |
8908 KB |
Output is correct |
25 |
Correct |
5 ms |
8908 KB |
Output is correct |
26 |
Correct |
6 ms |
8908 KB |
Output is correct |
27 |
Correct |
5 ms |
8908 KB |
Output is correct |
28 |
Correct |
5 ms |
8908 KB |
Output is correct |
29 |
Correct |
6 ms |
8908 KB |
Output is correct |
30 |
Correct |
5 ms |
8908 KB |
Output is correct |
31 |
Correct |
5 ms |
8952 KB |
Output is correct |
32 |
Correct |
5 ms |
8908 KB |
Output is correct |
33 |
Correct |
5 ms |
8908 KB |
Output is correct |
34 |
Correct |
5 ms |
8908 KB |
Output is correct |
35 |
Correct |
6 ms |
9036 KB |
Output is correct |
36 |
Correct |
5 ms |
8992 KB |
Output is correct |
37 |
Correct |
5 ms |
8908 KB |
Output is correct |
38 |
Correct |
5 ms |
8908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8908 KB |
Output is correct |
2 |
Correct |
4 ms |
8908 KB |
Output is correct |
3 |
Correct |
5 ms |
8908 KB |
Output is correct |
4 |
Correct |
4 ms |
8868 KB |
Output is correct |
5 |
Correct |
5 ms |
8908 KB |
Output is correct |
6 |
Correct |
4 ms |
8908 KB |
Output is correct |
7 |
Correct |
4 ms |
8908 KB |
Output is correct |
8 |
Correct |
5 ms |
8908 KB |
Output is correct |
9 |
Correct |
4 ms |
8908 KB |
Output is correct |
10 |
Correct |
5 ms |
8940 KB |
Output is correct |
11 |
Correct |
4 ms |
8908 KB |
Output is correct |
12 |
Correct |
5 ms |
8908 KB |
Output is correct |
13 |
Correct |
4 ms |
8908 KB |
Output is correct |
14 |
Correct |
5 ms |
8908 KB |
Output is correct |
15 |
Correct |
4 ms |
8908 KB |
Output is correct |
16 |
Correct |
4 ms |
8840 KB |
Output is correct |
17 |
Correct |
4 ms |
8908 KB |
Output is correct |
18 |
Correct |
4 ms |
8908 KB |
Output is correct |
19 |
Correct |
136 ms |
14260 KB |
Output is correct |
20 |
Correct |
126 ms |
14276 KB |
Output is correct |
21 |
Correct |
117 ms |
14532 KB |
Output is correct |
22 |
Correct |
108 ms |
14848 KB |
Output is correct |
23 |
Correct |
77 ms |
14400 KB |
Output is correct |
24 |
Correct |
55 ms |
14364 KB |
Output is correct |
25 |
Correct |
108 ms |
17028 KB |
Output is correct |
26 |
Correct |
97 ms |
20520 KB |
Output is correct |
27 |
Correct |
155 ms |
19780 KB |
Output is correct |
28 |
Correct |
234 ms |
30892 KB |
Output is correct |
29 |
Correct |
241 ms |
30048 KB |
Output is correct |
30 |
Correct |
146 ms |
19832 KB |
Output is correct |
31 |
Correct |
155 ms |
19752 KB |
Output is correct |
32 |
Correct |
192 ms |
19764 KB |
Output is correct |
33 |
Correct |
192 ms |
18712 KB |
Output is correct |
34 |
Correct |
172 ms |
18556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8908 KB |
Output is correct |
2 |
Correct |
4 ms |
8908 KB |
Output is correct |
3 |
Correct |
5 ms |
8908 KB |
Output is correct |
4 |
Correct |
4 ms |
8868 KB |
Output is correct |
5 |
Correct |
5 ms |
8908 KB |
Output is correct |
6 |
Correct |
4 ms |
8908 KB |
Output is correct |
7 |
Correct |
4 ms |
8908 KB |
Output is correct |
8 |
Correct |
5 ms |
8908 KB |
Output is correct |
9 |
Correct |
4 ms |
8908 KB |
Output is correct |
10 |
Correct |
5 ms |
8940 KB |
Output is correct |
11 |
Correct |
4 ms |
8908 KB |
Output is correct |
12 |
Correct |
5 ms |
8908 KB |
Output is correct |
13 |
Correct |
4 ms |
8908 KB |
Output is correct |
14 |
Correct |
5 ms |
8908 KB |
Output is correct |
15 |
Correct |
4 ms |
8908 KB |
Output is correct |
16 |
Correct |
4 ms |
8840 KB |
Output is correct |
17 |
Correct |
4 ms |
8908 KB |
Output is correct |
18 |
Correct |
4 ms |
8908 KB |
Output is correct |
19 |
Correct |
6 ms |
8908 KB |
Output is correct |
20 |
Correct |
5 ms |
8908 KB |
Output is correct |
21 |
Correct |
5 ms |
8908 KB |
Output is correct |
22 |
Correct |
5 ms |
8908 KB |
Output is correct |
23 |
Correct |
5 ms |
8908 KB |
Output is correct |
24 |
Correct |
5 ms |
8908 KB |
Output is correct |
25 |
Correct |
5 ms |
8908 KB |
Output is correct |
26 |
Correct |
6 ms |
8908 KB |
Output is correct |
27 |
Correct |
5 ms |
8908 KB |
Output is correct |
28 |
Correct |
5 ms |
8908 KB |
Output is correct |
29 |
Correct |
6 ms |
8908 KB |
Output is correct |
30 |
Correct |
5 ms |
8908 KB |
Output is correct |
31 |
Correct |
5 ms |
8952 KB |
Output is correct |
32 |
Correct |
5 ms |
8908 KB |
Output is correct |
33 |
Correct |
5 ms |
8908 KB |
Output is correct |
34 |
Correct |
5 ms |
8908 KB |
Output is correct |
35 |
Correct |
6 ms |
9036 KB |
Output is correct |
36 |
Correct |
5 ms |
8992 KB |
Output is correct |
37 |
Correct |
5 ms |
8908 KB |
Output is correct |
38 |
Correct |
5 ms |
8908 KB |
Output is correct |
39 |
Correct |
136 ms |
14260 KB |
Output is correct |
40 |
Correct |
126 ms |
14276 KB |
Output is correct |
41 |
Correct |
117 ms |
14532 KB |
Output is correct |
42 |
Correct |
108 ms |
14848 KB |
Output is correct |
43 |
Correct |
77 ms |
14400 KB |
Output is correct |
44 |
Correct |
55 ms |
14364 KB |
Output is correct |
45 |
Correct |
108 ms |
17028 KB |
Output is correct |
46 |
Correct |
97 ms |
20520 KB |
Output is correct |
47 |
Correct |
155 ms |
19780 KB |
Output is correct |
48 |
Correct |
234 ms |
30892 KB |
Output is correct |
49 |
Correct |
241 ms |
30048 KB |
Output is correct |
50 |
Correct |
146 ms |
19832 KB |
Output is correct |
51 |
Correct |
155 ms |
19752 KB |
Output is correct |
52 |
Correct |
192 ms |
19764 KB |
Output is correct |
53 |
Correct |
192 ms |
18712 KB |
Output is correct |
54 |
Correct |
172 ms |
18556 KB |
Output is correct |
55 |
Correct |
11 ms |
9420 KB |
Output is correct |
56 |
Correct |
13 ms |
9548 KB |
Output is correct |
57 |
Correct |
82 ms |
15044 KB |
Output is correct |
58 |
Correct |
34 ms |
15288 KB |
Output is correct |
59 |
Correct |
92 ms |
20484 KB |
Output is correct |
60 |
Correct |
449 ms |
31320 KB |
Output is correct |
61 |
Correct |
164 ms |
20040 KB |
Output is correct |
62 |
Correct |
175 ms |
20680 KB |
Output is correct |
63 |
Correct |
244 ms |
20680 KB |
Output is correct |
64 |
Correct |
427 ms |
20456 KB |
Output is correct |
65 |
Correct |
199 ms |
19780 KB |
Output is correct |
66 |
Correct |
323 ms |
27976 KB |
Output is correct |
67 |
Correct |
119 ms |
21808 KB |
Output is correct |
68 |
Correct |
238 ms |
20412 KB |
Output is correct |
69 |
Correct |
225 ms |
20420 KB |
Output is correct |
70 |
Correct |
207 ms |
19904 KB |
Output is correct |