#include "race.h"
#include <iostream>
#include <vector>
#include <map>
using namespace std;
int n, k, ans = 1e9;
vector<vector<pair<int, int>>> g;
map<int, int> dfs(int u, int par, int s, int d) {
map<int, int> ret;
ret[s] = d;
for (auto [v, w] : g[u]) {
if (v == par) continue;
map<int, int> res = dfs(v, u, s + w, d + 1);
if (ret.size() < res.size()) swap(ret, res);
for (auto [sv, dv] : res) {
int su = k - (sv - 2 * s);
if (ret.find(su) != ret.end()) ans = min(ans, dv + ret[su] - 2 * d);
}
for (auto [sv, dv] : res) {
if (ret.find(sv) != ret.end()) ret[sv] = min(ret[sv], dv);
else ret[sv] = dv;
}
}
return ret;
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N;
k = K;
g.resize(n);
for (int i = 0; i < n - 1; i++) {
g[H[i][0]].push_back({H[i][1], L[i]});
g[H[i][1]].push_back({H[i][0], L[i]});
}
dfs(0, 0, 0, 0);
return (ans > n ? -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 |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
308 KB |
Output is correct |
15 |
Correct |
1 ms |
308 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 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 |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
308 KB |
Output is correct |
15 |
Correct |
1 ms |
308 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
448 KB |
Output is correct |
25 |
Correct |
1 ms |
448 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
2 ms |
340 KB |
Output is correct |
33 |
Correct |
2 ms |
448 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
2 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
2 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
340 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 |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
308 KB |
Output is correct |
15 |
Correct |
1 ms |
308 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
99 ms |
8780 KB |
Output is correct |
20 |
Correct |
84 ms |
8888 KB |
Output is correct |
21 |
Correct |
86 ms |
8816 KB |
Output is correct |
22 |
Correct |
94 ms |
8892 KB |
Output is correct |
23 |
Correct |
108 ms |
9348 KB |
Output is correct |
24 |
Correct |
86 ms |
9108 KB |
Output is correct |
25 |
Correct |
76 ms |
21688 KB |
Output is correct |
26 |
Correct |
60 ms |
33232 KB |
Output is correct |
27 |
Correct |
166 ms |
18084 KB |
Output is correct |
28 |
Correct |
211 ms |
66556 KB |
Output is correct |
29 |
Correct |
229 ms |
64312 KB |
Output is correct |
30 |
Correct |
168 ms |
18028 KB |
Output is correct |
31 |
Correct |
178 ms |
18052 KB |
Output is correct |
32 |
Correct |
178 ms |
18248 KB |
Output is correct |
33 |
Correct |
171 ms |
17456 KB |
Output is correct |
34 |
Correct |
297 ms |
27724 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 |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
308 KB |
Output is correct |
15 |
Correct |
1 ms |
308 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
448 KB |
Output is correct |
25 |
Correct |
1 ms |
448 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
2 ms |
340 KB |
Output is correct |
33 |
Correct |
2 ms |
448 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
2 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
2 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
99 ms |
8780 KB |
Output is correct |
40 |
Correct |
84 ms |
8888 KB |
Output is correct |
41 |
Correct |
86 ms |
8816 KB |
Output is correct |
42 |
Correct |
94 ms |
8892 KB |
Output is correct |
43 |
Correct |
108 ms |
9348 KB |
Output is correct |
44 |
Correct |
86 ms |
9108 KB |
Output is correct |
45 |
Correct |
76 ms |
21688 KB |
Output is correct |
46 |
Correct |
60 ms |
33232 KB |
Output is correct |
47 |
Correct |
166 ms |
18084 KB |
Output is correct |
48 |
Correct |
211 ms |
66556 KB |
Output is correct |
49 |
Correct |
229 ms |
64312 KB |
Output is correct |
50 |
Correct |
168 ms |
18028 KB |
Output is correct |
51 |
Correct |
178 ms |
18052 KB |
Output is correct |
52 |
Correct |
178 ms |
18248 KB |
Output is correct |
53 |
Correct |
171 ms |
17456 KB |
Output is correct |
54 |
Correct |
297 ms |
27724 KB |
Output is correct |
55 |
Correct |
16 ms |
1432 KB |
Output is correct |
56 |
Correct |
6 ms |
1108 KB |
Output is correct |
57 |
Correct |
52 ms |
9016 KB |
Output is correct |
58 |
Correct |
45 ms |
8812 KB |
Output is correct |
59 |
Correct |
85 ms |
33308 KB |
Output is correct |
60 |
Correct |
214 ms |
64904 KB |
Output is correct |
61 |
Correct |
215 ms |
19568 KB |
Output is correct |
62 |
Correct |
203 ms |
17996 KB |
Output is correct |
63 |
Correct |
187 ms |
18112 KB |
Output is correct |
64 |
Correct |
396 ms |
24608 KB |
Output is correct |
65 |
Correct |
370 ms |
28076 KB |
Output is correct |
66 |
Correct |
234 ms |
59220 KB |
Output is correct |
67 |
Correct |
126 ms |
18604 KB |
Output is correct |
68 |
Correct |
287 ms |
26616 KB |
Output is correct |
69 |
Correct |
308 ms |
30292 KB |
Output is correct |
70 |
Correct |
252 ms |
25560 KB |
Output is correct |