#include "race.h"
#include <bits/stdc++.h>
using LL = long long;
const LL INF = 1e9;
using namespace std;
using pii = pair<LL, LL>;
#define pb push_back
#ifdef LOCAL
#define debug(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define debug(...) "zzz"
#endif
int best_path(int N, int K, int H[][2], int L[]) {
LL ans = INF;
vector e(N, vector<pii>());
for (LL i = 0; i < N - 1; i++) {
LL x = H[i][0], y = H[i][1], d = L[i];
e[x].pb({y, d});
e[y].pb({x, d});
}
//debug(e);
vector<bool> v(N);
vector<LL> sub(N);
auto cen_decomp = [&](auto cen_decomp, LL g, LL SZ) -> void {
auto dfs_sub = [&](auto dfs_sub, LL g, LL p) -> void {
sub[g] = 1;
for (auto [u, d] : e[g]) if (u != p && !v[u]) {
dfs_sub(dfs_sub, u, g);
sub[g] += sub[u];
}
};
auto find_cen = [&](auto find_cen, LL g, LL p) -> LL {
for (auto [u, d] : e[g]) if (u != p && !v[u]) {
if (sub[u] * 2 >= SZ)
return find_cen(find_cen, u, g);
}
return g;
};
dfs_sub(dfs_sub, g, -1);
g = find_cen(find_cen, g, -1);
dfs_sub(dfs_sub, g, -1);
//debug(g);
v[g] = 1;
map<LL, LL> dp;
auto fill_dp = [&](auto fill_dp, LL g, LL p, LL L, LL d) -> void {
if (!dp.count(L)) {
dp[L] = d;
} else {
dp[L] = min(dp[L], (LL)d);
}
for (auto [u, nl] : e[g]) if (!v[u] && u != p) {
fill_dp(fill_dp, u, g, L + nl, d + 1);
}
};
auto compare_dp = [&](auto compare_dp, LL g, LL p, LL L, LL d) -> void {
if (dp.count(K - L)) {
ans = min(ans, d + dp[K - L]);
}
for (auto [u, nl] : e[g]) if (!v[u] && u != p) {
compare_dp(compare_dp, u, g, L + nl, d + 1);
}
};
dp[0] = 0;
for (auto [u, d] : e[g]) if (!v[u]) {
compare_dp(compare_dp, u, g, d, 1);
fill_dp(fill_dp, u, g, d, 1);
}
for (auto [u, d] : e[g]) if (!v[u]) {
cen_decomp(cen_decomp, u, sub[u]);
}
};
cen_decomp(cen_decomp, 0, N);
return (ans >= INF ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
304 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
312 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
308 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
304 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
312 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
308 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
1 ms |
284 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
2 ms |
444 KB |
Output is correct |
22 |
Correct |
3 ms |
468 KB |
Output is correct |
23 |
Correct |
3 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
4 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
316 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
29 |
Correct |
3 ms |
468 KB |
Output is correct |
30 |
Correct |
3 ms |
448 KB |
Output is correct |
31 |
Correct |
3 ms |
468 KB |
Output is correct |
32 |
Correct |
3 ms |
468 KB |
Output is correct |
33 |
Correct |
3 ms |
440 KB |
Output is correct |
34 |
Correct |
2 ms |
468 KB |
Output is correct |
35 |
Correct |
3 ms |
468 KB |
Output is correct |
36 |
Correct |
2 ms |
460 KB |
Output is correct |
37 |
Correct |
2 ms |
444 KB |
Output is correct |
38 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
304 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
312 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
308 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
381 ms |
11200 KB |
Output is correct |
20 |
Correct |
315 ms |
11088 KB |
Output is correct |
21 |
Correct |
314 ms |
11260 KB |
Output is correct |
22 |
Correct |
243 ms |
11212 KB |
Output is correct |
23 |
Correct |
346 ms |
11900 KB |
Output is correct |
24 |
Correct |
174 ms |
10792 KB |
Output is correct |
25 |
Correct |
240 ms |
19148 KB |
Output is correct |
26 |
Correct |
159 ms |
18508 KB |
Output is correct |
27 |
Correct |
325 ms |
22900 KB |
Output is correct |
28 |
Correct |
1142 ms |
61880 KB |
Output is correct |
29 |
Correct |
1138 ms |
61932 KB |
Output is correct |
30 |
Correct |
363 ms |
22908 KB |
Output is correct |
31 |
Correct |
385 ms |
22912 KB |
Output is correct |
32 |
Correct |
465 ms |
22964 KB |
Output is correct |
33 |
Correct |
640 ms |
22128 KB |
Output is correct |
34 |
Correct |
969 ms |
44256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
304 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
312 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
308 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
1 ms |
284 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
2 ms |
444 KB |
Output is correct |
22 |
Correct |
3 ms |
468 KB |
Output is correct |
23 |
Correct |
3 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
4 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
316 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
29 |
Correct |
3 ms |
468 KB |
Output is correct |
30 |
Correct |
3 ms |
448 KB |
Output is correct |
31 |
Correct |
3 ms |
468 KB |
Output is correct |
32 |
Correct |
3 ms |
468 KB |
Output is correct |
33 |
Correct |
3 ms |
440 KB |
Output is correct |
34 |
Correct |
2 ms |
468 KB |
Output is correct |
35 |
Correct |
3 ms |
468 KB |
Output is correct |
36 |
Correct |
2 ms |
460 KB |
Output is correct |
37 |
Correct |
2 ms |
444 KB |
Output is correct |
38 |
Correct |
2 ms |
468 KB |
Output is correct |
39 |
Correct |
381 ms |
11200 KB |
Output is correct |
40 |
Correct |
315 ms |
11088 KB |
Output is correct |
41 |
Correct |
314 ms |
11260 KB |
Output is correct |
42 |
Correct |
243 ms |
11212 KB |
Output is correct |
43 |
Correct |
346 ms |
11900 KB |
Output is correct |
44 |
Correct |
174 ms |
10792 KB |
Output is correct |
45 |
Correct |
240 ms |
19148 KB |
Output is correct |
46 |
Correct |
159 ms |
18508 KB |
Output is correct |
47 |
Correct |
325 ms |
22900 KB |
Output is correct |
48 |
Correct |
1142 ms |
61880 KB |
Output is correct |
49 |
Correct |
1138 ms |
61932 KB |
Output is correct |
50 |
Correct |
363 ms |
22908 KB |
Output is correct |
51 |
Correct |
385 ms |
22912 KB |
Output is correct |
52 |
Correct |
465 ms |
22964 KB |
Output is correct |
53 |
Correct |
640 ms |
22128 KB |
Output is correct |
54 |
Correct |
969 ms |
44256 KB |
Output is correct |
55 |
Correct |
31 ms |
2132 KB |
Output is correct |
56 |
Correct |
18 ms |
1340 KB |
Output is correct |
57 |
Correct |
178 ms |
11460 KB |
Output is correct |
58 |
Correct |
65 ms |
10360 KB |
Output is correct |
59 |
Correct |
354 ms |
27908 KB |
Output is correct |
60 |
Correct |
1230 ms |
59852 KB |
Output is correct |
61 |
Correct |
423 ms |
24752 KB |
Output is correct |
62 |
Correct |
423 ms |
22852 KB |
Output is correct |
63 |
Correct |
465 ms |
22972 KB |
Output is correct |
64 |
Correct |
1295 ms |
37440 KB |
Output is correct |
65 |
Correct |
1103 ms |
44908 KB |
Output is correct |
66 |
Correct |
1176 ms |
62136 KB |
Output is correct |
67 |
Correct |
181 ms |
21808 KB |
Output is correct |
68 |
Correct |
520 ms |
38516 KB |
Output is correct |
69 |
Correct |
530 ms |
38792 KB |
Output is correct |
70 |
Correct |
549 ms |
36768 KB |
Output is correct |