#include "race.h"
#include "bits/stdc++.h"
int best_path(int N, int K, int H[][2], int L[]) {
std::vector<std::vector<std::pair<long long, long long>>> adj(N);
for (long long i = 0; i < N - 1; i++) {
adj[H[i][0]].push_back(std::make_pair(H[i][1], 1ll * L[i]));
adj[H[i][1]].push_back(std::make_pair(H[i][0], 1ll * L[i]));
}
std::vector<long long> sum(N, 0);
std::vector<long long> dep(N);
dep[0] = 1;
auto Dfs = [&](long long u, long long p, auto&& Dfs) -> void {
for (auto& [v, w] : adj[u]) {
if (v == p) continue;
sum[v] = sum[u] + w;
dep[v] = dep[u] + 1;
Dfs(v, u, Dfs);
}
};
Dfs(0, -1, Dfs);
long long mn = N + 1;
auto small_to_large = [&](long long u, long long p, auto&& small_to_large) -> std::map<long long, long long> {
long long need = K + (2 * sum[u]);
std::map<long long, long long> here;
here[sum[u]] = dep[u];
for (auto& [v, w] : adj[u]) {
if (v == p) continue;
std::map<long long, long long> child = small_to_large(v, u, small_to_large);
if (here.size() < child.size()) std::swap(here, child);
for (auto& [s, depth] : child) {
if (depth == 0) continue;
if (here[need - s] != 0) {
mn = std::min(mn, here[need - s] + depth - (2 * dep[u]));
}
}
for (auto& [s, depth] : child) {
if (depth == 0) continue;
if (here[s] == 0) here[s] = depth;
else here[s] = std::min(here[s], depth);
}
}
return here;
};
small_to_large(0, -1, small_to_large);
if (mn == N + 1) return -1;
else return (int) mn;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 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 |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 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 |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 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 |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 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 |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 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 |
2652 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2736 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
2 ms |
2896 KB |
Output is correct |
26 |
Correct |
2 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2392 KB |
Output is correct |
28 |
Correct |
2 ms |
2652 KB |
Output is correct |
29 |
Correct |
2 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2492 KB |
Output is correct |
31 |
Correct |
2 ms |
2652 KB |
Output is correct |
32 |
Correct |
2 ms |
2536 KB |
Output is correct |
33 |
Correct |
3 ms |
2652 KB |
Output is correct |
34 |
Correct |
1 ms |
2652 KB |
Output is correct |
35 |
Correct |
2 ms |
2652 KB |
Output is correct |
36 |
Correct |
1 ms |
2652 KB |
Output is correct |
37 |
Correct |
2 ms |
2652 KB |
Output is correct |
38 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 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 |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 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 |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
117 ms |
14100 KB |
Output is correct |
20 |
Correct |
117 ms |
15140 KB |
Output is correct |
21 |
Correct |
111 ms |
14572 KB |
Output is correct |
22 |
Correct |
96 ms |
15204 KB |
Output is correct |
23 |
Correct |
162 ms |
16252 KB |
Output is correct |
24 |
Correct |
105 ms |
14780 KB |
Output is correct |
25 |
Correct |
74 ms |
31344 KB |
Output is correct |
26 |
Correct |
69 ms |
45140 KB |
Output is correct |
27 |
Correct |
171 ms |
25640 KB |
Output is correct |
28 |
Correct |
228 ms |
98592 KB |
Output is correct |
29 |
Correct |
213 ms |
96812 KB |
Output is correct |
30 |
Correct |
145 ms |
25936 KB |
Output is correct |
31 |
Correct |
141 ms |
25936 KB |
Output is correct |
32 |
Correct |
168 ms |
25960 KB |
Output is correct |
33 |
Correct |
205 ms |
25624 KB |
Output is correct |
34 |
Correct |
375 ms |
51280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 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 |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 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 |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 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 |
2652 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2736 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
2 ms |
2896 KB |
Output is correct |
26 |
Correct |
2 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2392 KB |
Output is correct |
28 |
Correct |
2 ms |
2652 KB |
Output is correct |
29 |
Correct |
2 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2492 KB |
Output is correct |
31 |
Correct |
2 ms |
2652 KB |
Output is correct |
32 |
Correct |
2 ms |
2536 KB |
Output is correct |
33 |
Correct |
3 ms |
2652 KB |
Output is correct |
34 |
Correct |
1 ms |
2652 KB |
Output is correct |
35 |
Correct |
2 ms |
2652 KB |
Output is correct |
36 |
Correct |
1 ms |
2652 KB |
Output is correct |
37 |
Correct |
2 ms |
2652 KB |
Output is correct |
38 |
Correct |
2 ms |
2652 KB |
Output is correct |
39 |
Correct |
117 ms |
14100 KB |
Output is correct |
40 |
Correct |
117 ms |
15140 KB |
Output is correct |
41 |
Correct |
111 ms |
14572 KB |
Output is correct |
42 |
Correct |
96 ms |
15204 KB |
Output is correct |
43 |
Correct |
162 ms |
16252 KB |
Output is correct |
44 |
Correct |
105 ms |
14780 KB |
Output is correct |
45 |
Correct |
74 ms |
31344 KB |
Output is correct |
46 |
Correct |
69 ms |
45140 KB |
Output is correct |
47 |
Correct |
171 ms |
25640 KB |
Output is correct |
48 |
Correct |
228 ms |
98592 KB |
Output is correct |
49 |
Correct |
213 ms |
96812 KB |
Output is correct |
50 |
Correct |
145 ms |
25936 KB |
Output is correct |
51 |
Correct |
141 ms |
25936 KB |
Output is correct |
52 |
Correct |
168 ms |
25960 KB |
Output is correct |
53 |
Correct |
205 ms |
25624 KB |
Output is correct |
54 |
Correct |
375 ms |
51280 KB |
Output is correct |
55 |
Correct |
14 ms |
4092 KB |
Output is correct |
56 |
Correct |
7 ms |
3420 KB |
Output is correct |
57 |
Correct |
65 ms |
14592 KB |
Output is correct |
58 |
Correct |
40 ms |
14280 KB |
Output is correct |
59 |
Correct |
99 ms |
51432 KB |
Output is correct |
60 |
Correct |
262 ms |
97108 KB |
Output is correct |
61 |
Correct |
193 ms |
28280 KB |
Output is correct |
62 |
Correct |
164 ms |
25952 KB |
Output is correct |
63 |
Correct |
187 ms |
25936 KB |
Output is correct |
64 |
Correct |
510 ms |
41880 KB |
Output is correct |
65 |
Correct |
535 ms |
51180 KB |
Output is correct |
66 |
Correct |
243 ms |
91084 KB |
Output is correct |
67 |
Correct |
104 ms |
25540 KB |
Output is correct |
68 |
Correct |
298 ms |
45916 KB |
Output is correct |
69 |
Correct |
382 ms |
52280 KB |
Output is correct |
70 |
Correct |
283 ms |
44368 KB |
Output is correct |