#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif
int best_path(int N, int K, int H[][2], int L[]) {
int n = N;
int k = K;
vector<vector<pair<int, int>>> g(n);
for (int i = 0; i < n - 1; ++i) {
int x = H[i][0];
int y = H[i][1];
int z = L[i];
g[x].emplace_back(y, z);
g[y].emplace_back(x, z);
}
vector<bool> removed(n, false);
vector<int> sz(n);
vector<int> pv(n);
function<void(int)> DFS = [&](int v) -> void {
sz[v] = 1;
for (auto [u, _] : g[v]) {
if (u != pv[v] && !removed[u]) {
pv[u] = v;
DFS(u);
sz[v] += sz[u];
}
}
};
auto GetCenter = [&](int r) {
pv[r] = -1;
DFS(r);
int tot = sz[r];
while (true) {
int nr = -1;
for (auto [u, _] : g[r]) {
if (u != pv[r] && !removed[u] && sz[u] * 2 > tot) {
nr = u;
break;
}
}
if (nr == -1) {
return r;
}
r = nr;
}
};
int ans = n;
int iter = 0;
vector<int> aux(k + 1, 0);
vector<int> dp(k + 1, 0);
function<void(int, int, int, int)> Find = [&](int v, int pv, int dist, int depth) -> void {
if (dist > k) {
return;
}
if (aux[k - dist] == iter) {
ans = min(ans, dp[k - dist] + depth);
}
if (dist == k) {
ans = min(ans, depth);
}
for (auto [u, w] : g[v]) {
if (!removed[u] && u != pv) {
Find(u, v, dist + w, depth + 1);
}
}
};
function<void(int, int, int, int)> Calc = [&](int v, int pv, int dist, int depth) -> void {
if (dist > k) {
return;
}
if (aux[dist] < iter) {
aux[dist] = iter;
dp[dist] = depth;
} else
if (dp[dist] > depth) {
dp[dist] = depth;
}
for (auto [u, w] : g[v]) {
if (!removed[u] && u != pv) {
Calc(u, v, dist + w, depth + 1);
}
}
};
function<void(int)> Solve = [&](int v) -> void {
v = GetCenter(v);
iter += 1;
for (auto [u, w] : g[v]) {
if (!removed[u]) {
Find(u, v, w, 1);
Calc(u, v, w, 1);
}
}
removed[v] = true;
for (auto [u, _] : g[v]) {
if (!removed[u]) {
Solve(u);
}
}
};
Solve(0);
return (ans == n ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2384 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2384 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
2 ms |
2384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2384 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2384 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
2 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
1 ms |
2384 KB |
Output is correct |
21 |
Correct |
1 ms |
2384 KB |
Output is correct |
22 |
Correct |
4 ms |
9552 KB |
Output is correct |
23 |
Correct |
4 ms |
8272 KB |
Output is correct |
24 |
Correct |
4 ms |
9296 KB |
Output is correct |
25 |
Correct |
4 ms |
9040 KB |
Output is correct |
26 |
Correct |
3 ms |
5200 KB |
Output is correct |
27 |
Correct |
4 ms |
8796 KB |
Output is correct |
28 |
Correct |
2 ms |
3920 KB |
Output is correct |
29 |
Correct |
3 ms |
4944 KB |
Output is correct |
30 |
Correct |
3 ms |
5440 KB |
Output is correct |
31 |
Correct |
3 ms |
7504 KB |
Output is correct |
32 |
Correct |
3 ms |
8016 KB |
Output is correct |
33 |
Correct |
4 ms |
8528 KB |
Output is correct |
34 |
Correct |
2 ms |
7160 KB |
Output is correct |
35 |
Correct |
3 ms |
8784 KB |
Output is correct |
36 |
Correct |
4 ms |
9808 KB |
Output is correct |
37 |
Correct |
3 ms |
8784 KB |
Output is correct |
38 |
Correct |
4 ms |
6480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2384 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2384 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
2 ms |
2384 KB |
Output is correct |
19 |
Correct |
90 ms |
11508 KB |
Output is correct |
20 |
Correct |
88 ms |
12292 KB |
Output is correct |
21 |
Correct |
102 ms |
11372 KB |
Output is correct |
22 |
Correct |
86 ms |
11600 KB |
Output is correct |
23 |
Correct |
57 ms |
11592 KB |
Output is correct |
24 |
Correct |
42 ms |
12428 KB |
Output is correct |
25 |
Correct |
100 ms |
15896 KB |
Output is correct |
26 |
Correct |
84 ms |
20296 KB |
Output is correct |
27 |
Correct |
115 ms |
18504 KB |
Output is correct |
28 |
Correct |
187 ms |
38728 KB |
Output is correct |
29 |
Correct |
175 ms |
37464 KB |
Output is correct |
30 |
Correct |
104 ms |
20180 KB |
Output is correct |
31 |
Correct |
99 ms |
20296 KB |
Output is correct |
32 |
Correct |
131 ms |
20996 KB |
Output is correct |
33 |
Correct |
152 ms |
19784 KB |
Output is correct |
34 |
Correct |
129 ms |
20808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2384 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2384 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
2 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
1 ms |
2384 KB |
Output is correct |
21 |
Correct |
1 ms |
2384 KB |
Output is correct |
22 |
Correct |
4 ms |
9552 KB |
Output is correct |
23 |
Correct |
4 ms |
8272 KB |
Output is correct |
24 |
Correct |
4 ms |
9296 KB |
Output is correct |
25 |
Correct |
4 ms |
9040 KB |
Output is correct |
26 |
Correct |
3 ms |
5200 KB |
Output is correct |
27 |
Correct |
4 ms |
8796 KB |
Output is correct |
28 |
Correct |
2 ms |
3920 KB |
Output is correct |
29 |
Correct |
3 ms |
4944 KB |
Output is correct |
30 |
Correct |
3 ms |
5440 KB |
Output is correct |
31 |
Correct |
3 ms |
7504 KB |
Output is correct |
32 |
Correct |
3 ms |
8016 KB |
Output is correct |
33 |
Correct |
4 ms |
8528 KB |
Output is correct |
34 |
Correct |
2 ms |
7160 KB |
Output is correct |
35 |
Correct |
3 ms |
8784 KB |
Output is correct |
36 |
Correct |
4 ms |
9808 KB |
Output is correct |
37 |
Correct |
3 ms |
8784 KB |
Output is correct |
38 |
Correct |
4 ms |
6480 KB |
Output is correct |
39 |
Correct |
90 ms |
11508 KB |
Output is correct |
40 |
Correct |
88 ms |
12292 KB |
Output is correct |
41 |
Correct |
102 ms |
11372 KB |
Output is correct |
42 |
Correct |
86 ms |
11600 KB |
Output is correct |
43 |
Correct |
57 ms |
11592 KB |
Output is correct |
44 |
Correct |
42 ms |
12428 KB |
Output is correct |
45 |
Correct |
100 ms |
15896 KB |
Output is correct |
46 |
Correct |
84 ms |
20296 KB |
Output is correct |
47 |
Correct |
115 ms |
18504 KB |
Output is correct |
48 |
Correct |
187 ms |
38728 KB |
Output is correct |
49 |
Correct |
175 ms |
37464 KB |
Output is correct |
50 |
Correct |
104 ms |
20180 KB |
Output is correct |
51 |
Correct |
99 ms |
20296 KB |
Output is correct |
52 |
Correct |
131 ms |
20996 KB |
Output is correct |
53 |
Correct |
152 ms |
19784 KB |
Output is correct |
54 |
Correct |
129 ms |
20808 KB |
Output is correct |
55 |
Correct |
7 ms |
3380 KB |
Output is correct |
56 |
Correct |
8 ms |
3124 KB |
Output is correct |
57 |
Correct |
65 ms |
13060 KB |
Output is correct |
58 |
Correct |
25 ms |
11716 KB |
Output is correct |
59 |
Correct |
87 ms |
22868 KB |
Output is correct |
60 |
Correct |
282 ms |
45384 KB |
Output is correct |
61 |
Correct |
134 ms |
21576 KB |
Output is correct |
62 |
Correct |
129 ms |
27976 KB |
Output is correct |
63 |
Correct |
149 ms |
28156 KB |
Output is correct |
64 |
Correct |
277 ms |
24348 KB |
Output is correct |
65 |
Correct |
131 ms |
22072 KB |
Output is correct |
66 |
Correct |
270 ms |
41916 KB |
Output is correct |
67 |
Correct |
76 ms |
30136 KB |
Output is correct |
68 |
Correct |
188 ms |
28320 KB |
Output is correct |
69 |
Correct |
163 ms |
30024 KB |
Output is correct |
70 |
Correct |
137 ms |
28744 KB |
Output is correct |