#include <bits/stdc++.h>
using namespace std;
int best_path(int n, int k, int h[][2], int l[]) {
int ans = n + 1;
vector<int> sz(n); vector<bool> vis(n, false);
vector<vector<pair<int, int>>> g(n);
for(int i = 0; i < n - 1; i++) {
g[h[i][0]].emplace_back(h[i][1], l[i]);
g[h[i][1]].emplace_back(h[i][0], l[i]);
}
map<int, int> m1, m2;
auto proc = [&](auto rec, int u, int p, int num, int sum) -> void {
if(!m2.count(sum)) m2[sum] = num;
else m2[sum] = min(m2[sum], num);
for(auto [v, w]: g[u]) {
if(v == p || vis[v]) continue;
rec(rec, v, u, num + 1, sum + w);
}
};
auto get_size = [&](auto rec, int u, int p = 0) -> int {
sz[u] = 1;
for(auto [v, w]: g[u]) {
if(v != p && !vis[v]) {
sz[u] += rec(rec, v, u);
}
}
return sz[u];
};
auto find_centroid = [&](auto rec, int u, int p, int full_sz) -> int {
for(auto [v, w]: g[u]) {
if(v != p && !vis[v] && 2 * sz[v] > full_sz) {
return rec(rec, v, u, full_sz);
}
}
return u;
};
auto centroid_decomp = [&](auto rec, int u) -> void {
get_size(get_size, u, u);
u = find_centroid(find_centroid, u, u, sz[u]);
get_size(get_size, u, u);
vis[u] = true;
m1[0] = 0;
for(auto [v, w]: g[u]) {
if(!vis[v]) {
proc(proc, v, v, 1, w);
for(auto [sum, num]: m2) {
if(m1.count(k - sum)) {
ans = min(ans, num + m1[k - sum]);
}
}
for(auto [sum, num]: m2) {
if(!m1.count(sum)) m1[sum] = num;
else m1[sum] = min(m1[sum], num);
}
m2.clear();
}
}
m1.clear();
for(auto [v, w]: g[u]) {
if(!vis[v]) rec(rec, v);
}
};
centroid_decomp(centroid_decomp, 0);
return ans == n + 1 ? -1 : ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2492 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 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 |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 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 |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2492 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 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 |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
2 ms |
2396 KB |
Output is correct |
22 |
Correct |
2 ms |
2396 KB |
Output is correct |
23 |
Correct |
2 ms |
2396 KB |
Output is correct |
24 |
Correct |
2 ms |
2396 KB |
Output is correct |
25 |
Correct |
2 ms |
2396 KB |
Output is correct |
26 |
Correct |
2 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
2 ms |
2396 KB |
Output is correct |
29 |
Correct |
2 ms |
2396 KB |
Output is correct |
30 |
Correct |
2 ms |
2396 KB |
Output is correct |
31 |
Correct |
3 ms |
2396 KB |
Output is correct |
32 |
Correct |
2 ms |
2448 KB |
Output is correct |
33 |
Correct |
2 ms |
2396 KB |
Output is correct |
34 |
Correct |
2 ms |
2396 KB |
Output is correct |
35 |
Correct |
2 ms |
2396 KB |
Output is correct |
36 |
Correct |
2 ms |
2396 KB |
Output is correct |
37 |
Correct |
2 ms |
2396 KB |
Output is correct |
38 |
Correct |
2 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2492 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 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 |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
210 ms |
12368 KB |
Output is correct |
20 |
Correct |
214 ms |
12616 KB |
Output is correct |
21 |
Correct |
184 ms |
12380 KB |
Output is correct |
22 |
Correct |
171 ms |
12592 KB |
Output is correct |
23 |
Correct |
307 ms |
12884 KB |
Output is correct |
24 |
Correct |
134 ms |
12728 KB |
Output is correct |
25 |
Correct |
154 ms |
18516 KB |
Output is correct |
26 |
Correct |
86 ms |
18004 KB |
Output is correct |
27 |
Correct |
297 ms |
21084 KB |
Output is correct |
28 |
Correct |
929 ms |
46164 KB |
Output is correct |
29 |
Correct |
973 ms |
46164 KB |
Output is correct |
30 |
Correct |
288 ms |
20876 KB |
Output is correct |
31 |
Correct |
283 ms |
20824 KB |
Output is correct |
32 |
Correct |
327 ms |
20796 KB |
Output is correct |
33 |
Correct |
499 ms |
19792 KB |
Output is correct |
34 |
Correct |
859 ms |
32204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2492 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 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 |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
2 ms |
2396 KB |
Output is correct |
22 |
Correct |
2 ms |
2396 KB |
Output is correct |
23 |
Correct |
2 ms |
2396 KB |
Output is correct |
24 |
Correct |
2 ms |
2396 KB |
Output is correct |
25 |
Correct |
2 ms |
2396 KB |
Output is correct |
26 |
Correct |
2 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
2 ms |
2396 KB |
Output is correct |
29 |
Correct |
2 ms |
2396 KB |
Output is correct |
30 |
Correct |
2 ms |
2396 KB |
Output is correct |
31 |
Correct |
3 ms |
2396 KB |
Output is correct |
32 |
Correct |
2 ms |
2448 KB |
Output is correct |
33 |
Correct |
2 ms |
2396 KB |
Output is correct |
34 |
Correct |
2 ms |
2396 KB |
Output is correct |
35 |
Correct |
2 ms |
2396 KB |
Output is correct |
36 |
Correct |
2 ms |
2396 KB |
Output is correct |
37 |
Correct |
2 ms |
2396 KB |
Output is correct |
38 |
Correct |
2 ms |
2396 KB |
Output is correct |
39 |
Correct |
210 ms |
12368 KB |
Output is correct |
40 |
Correct |
214 ms |
12616 KB |
Output is correct |
41 |
Correct |
184 ms |
12380 KB |
Output is correct |
42 |
Correct |
171 ms |
12592 KB |
Output is correct |
43 |
Correct |
307 ms |
12884 KB |
Output is correct |
44 |
Correct |
134 ms |
12728 KB |
Output is correct |
45 |
Correct |
154 ms |
18516 KB |
Output is correct |
46 |
Correct |
86 ms |
18004 KB |
Output is correct |
47 |
Correct |
297 ms |
21084 KB |
Output is correct |
48 |
Correct |
929 ms |
46164 KB |
Output is correct |
49 |
Correct |
973 ms |
46164 KB |
Output is correct |
50 |
Correct |
288 ms |
20876 KB |
Output is correct |
51 |
Correct |
283 ms |
20824 KB |
Output is correct |
52 |
Correct |
327 ms |
20796 KB |
Output is correct |
53 |
Correct |
499 ms |
19792 KB |
Output is correct |
54 |
Correct |
859 ms |
32204 KB |
Output is correct |
55 |
Correct |
23 ms |
3780 KB |
Output is correct |
56 |
Correct |
14 ms |
3160 KB |
Output is correct |
57 |
Correct |
132 ms |
12704 KB |
Output is correct |
58 |
Correct |
44 ms |
12428 KB |
Output is correct |
59 |
Correct |
357 ms |
23888 KB |
Output is correct |
60 |
Correct |
920 ms |
45144 KB |
Output is correct |
61 |
Correct |
346 ms |
22308 KB |
Output is correct |
62 |
Correct |
327 ms |
20820 KB |
Output is correct |
63 |
Correct |
325 ms |
20936 KB |
Output is correct |
64 |
Correct |
953 ms |
27584 KB |
Output is correct |
65 |
Correct |
800 ms |
31288 KB |
Output is correct |
66 |
Correct |
938 ms |
46152 KB |
Output is correct |
67 |
Correct |
103 ms |
21440 KB |
Output is correct |
68 |
Correct |
462 ms |
29524 KB |
Output is correct |
69 |
Correct |
454 ms |
29812 KB |
Output is correct |
70 |
Correct |
423 ms |
28580 KB |
Output is correct |