#include "race.h"
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;
const int MAXN = 200005;
struct EDGE { int x, w; };
int N, K, ans = -1;
vector <EDGE> v[MAXN];
void f(int x, int par, unordered_map <int, int> &d, int &L, int &W) {
d[0] = 0;
for (auto &y : v[x]) {
if (y.x == par) continue;
unordered_map <int, int> d2;
int L2 = 0, W2 = 0;
f(y.x, x, d2, L2, W2);
L2++; W2 += y.w;
if (d.size() < d2.size()) {
d.swap(d2);
swap(L, L2);
swap(W, W2);
}
for (auto &t : d2) {
if (d.count(K - (t.first + W2 + W))) {
if (ans == -1 || ans > d[K - (t.first + W2 + W)] + t.second + L + L2)
ans = d[K - (t.first + W2 + W)] + t.second + L + L2;
}
}
for (auto &t : d2) {
if (t.first + W2 > K) continue;
if (t.first + W2 == K) {
if (ans == -1 || ans > t.second + L2)
ans = t.second + L2;
}
if (d.count(t.first + W2 - W)) d[t.first + W2 - W] = min(d[t.first + W2 - W], t.second + L2 - L);
else d[t.first + W2 - W] = t.second + L2 - L;
}
}
}
int best_path(int _N, int _K, int H[][2], int L[]) {
N = _N, K = _K;
for (int i = 0; i < N - 1; i++) {
v[H[i][0]].push_back({ H[i][1], L[i] });
v[H[i][1]].push_back({ H[i][0], L[i] });
}
unordered_map <int, int> d;
int A = 0, B = 0;
f(0, -1, d, A, B);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4984 KB |
Output is correct |
2 |
Correct |
6 ms |
5092 KB |
Output is correct |
3 |
Correct |
5 ms |
5092 KB |
Output is correct |
4 |
Correct |
5 ms |
5144 KB |
Output is correct |
5 |
Correct |
5 ms |
5144 KB |
Output is correct |
6 |
Correct |
6 ms |
5196 KB |
Output is correct |
7 |
Correct |
5 ms |
5244 KB |
Output is correct |
8 |
Correct |
6 ms |
5400 KB |
Output is correct |
9 |
Correct |
5 ms |
5400 KB |
Output is correct |
10 |
Correct |
5 ms |
5400 KB |
Output is correct |
11 |
Correct |
5 ms |
5400 KB |
Output is correct |
12 |
Correct |
5 ms |
5400 KB |
Output is correct |
13 |
Correct |
5 ms |
5400 KB |
Output is correct |
14 |
Correct |
6 ms |
5400 KB |
Output is correct |
15 |
Correct |
5 ms |
5400 KB |
Output is correct |
16 |
Correct |
5 ms |
5400 KB |
Output is correct |
17 |
Correct |
5 ms |
5400 KB |
Output is correct |
18 |
Correct |
5 ms |
5400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4984 KB |
Output is correct |
2 |
Correct |
6 ms |
5092 KB |
Output is correct |
3 |
Correct |
5 ms |
5092 KB |
Output is correct |
4 |
Correct |
5 ms |
5144 KB |
Output is correct |
5 |
Correct |
5 ms |
5144 KB |
Output is correct |
6 |
Correct |
6 ms |
5196 KB |
Output is correct |
7 |
Correct |
5 ms |
5244 KB |
Output is correct |
8 |
Correct |
6 ms |
5400 KB |
Output is correct |
9 |
Correct |
5 ms |
5400 KB |
Output is correct |
10 |
Correct |
5 ms |
5400 KB |
Output is correct |
11 |
Correct |
5 ms |
5400 KB |
Output is correct |
12 |
Correct |
5 ms |
5400 KB |
Output is correct |
13 |
Correct |
5 ms |
5400 KB |
Output is correct |
14 |
Correct |
6 ms |
5400 KB |
Output is correct |
15 |
Correct |
5 ms |
5400 KB |
Output is correct |
16 |
Correct |
5 ms |
5400 KB |
Output is correct |
17 |
Correct |
5 ms |
5400 KB |
Output is correct |
18 |
Correct |
5 ms |
5400 KB |
Output is correct |
19 |
Correct |
5 ms |
5400 KB |
Output is correct |
20 |
Correct |
5 ms |
5400 KB |
Output is correct |
21 |
Correct |
7 ms |
5400 KB |
Output is correct |
22 |
Correct |
6 ms |
5400 KB |
Output is correct |
23 |
Correct |
6 ms |
5400 KB |
Output is correct |
24 |
Correct |
7 ms |
5400 KB |
Output is correct |
25 |
Correct |
6 ms |
5400 KB |
Output is correct |
26 |
Correct |
6 ms |
5400 KB |
Output is correct |
27 |
Correct |
5 ms |
5400 KB |
Output is correct |
28 |
Correct |
6 ms |
5452 KB |
Output is correct |
29 |
Correct |
6 ms |
5452 KB |
Output is correct |
30 |
Correct |
6 ms |
5452 KB |
Output is correct |
31 |
Correct |
6 ms |
5452 KB |
Output is correct |
32 |
Correct |
7 ms |
5452 KB |
Output is correct |
33 |
Correct |
6 ms |
5452 KB |
Output is correct |
34 |
Correct |
6 ms |
5452 KB |
Output is correct |
35 |
Correct |
6 ms |
5452 KB |
Output is correct |
36 |
Correct |
6 ms |
5452 KB |
Output is correct |
37 |
Correct |
6 ms |
5452 KB |
Output is correct |
38 |
Correct |
6 ms |
5452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4984 KB |
Output is correct |
2 |
Correct |
6 ms |
5092 KB |
Output is correct |
3 |
Correct |
5 ms |
5092 KB |
Output is correct |
4 |
Correct |
5 ms |
5144 KB |
Output is correct |
5 |
Correct |
5 ms |
5144 KB |
Output is correct |
6 |
Correct |
6 ms |
5196 KB |
Output is correct |
7 |
Correct |
5 ms |
5244 KB |
Output is correct |
8 |
Correct |
6 ms |
5400 KB |
Output is correct |
9 |
Correct |
5 ms |
5400 KB |
Output is correct |
10 |
Correct |
5 ms |
5400 KB |
Output is correct |
11 |
Correct |
5 ms |
5400 KB |
Output is correct |
12 |
Correct |
5 ms |
5400 KB |
Output is correct |
13 |
Correct |
5 ms |
5400 KB |
Output is correct |
14 |
Correct |
6 ms |
5400 KB |
Output is correct |
15 |
Correct |
5 ms |
5400 KB |
Output is correct |
16 |
Correct |
5 ms |
5400 KB |
Output is correct |
17 |
Correct |
5 ms |
5400 KB |
Output is correct |
18 |
Correct |
5 ms |
5400 KB |
Output is correct |
19 |
Correct |
126 ms |
10092 KB |
Output is correct |
20 |
Correct |
133 ms |
10264 KB |
Output is correct |
21 |
Correct |
132 ms |
10264 KB |
Output is correct |
22 |
Correct |
138 ms |
10352 KB |
Output is correct |
23 |
Correct |
134 ms |
10352 KB |
Output is correct |
24 |
Correct |
118 ms |
10388 KB |
Output is correct |
25 |
Correct |
120 ms |
23192 KB |
Output is correct |
26 |
Correct |
115 ms |
34540 KB |
Output is correct |
27 |
Correct |
231 ms |
34540 KB |
Output is correct |
28 |
Correct |
335 ms |
66484 KB |
Output is correct |
29 |
Correct |
332 ms |
66484 KB |
Output is correct |
30 |
Correct |
236 ms |
66484 KB |
Output is correct |
31 |
Correct |
224 ms |
66484 KB |
Output is correct |
32 |
Correct |
229 ms |
66484 KB |
Output is correct |
33 |
Correct |
213 ms |
66484 KB |
Output is correct |
34 |
Correct |
192 ms |
66484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4984 KB |
Output is correct |
2 |
Correct |
6 ms |
5092 KB |
Output is correct |
3 |
Correct |
5 ms |
5092 KB |
Output is correct |
4 |
Correct |
5 ms |
5144 KB |
Output is correct |
5 |
Correct |
5 ms |
5144 KB |
Output is correct |
6 |
Correct |
6 ms |
5196 KB |
Output is correct |
7 |
Correct |
5 ms |
5244 KB |
Output is correct |
8 |
Correct |
6 ms |
5400 KB |
Output is correct |
9 |
Correct |
5 ms |
5400 KB |
Output is correct |
10 |
Correct |
5 ms |
5400 KB |
Output is correct |
11 |
Correct |
5 ms |
5400 KB |
Output is correct |
12 |
Correct |
5 ms |
5400 KB |
Output is correct |
13 |
Correct |
5 ms |
5400 KB |
Output is correct |
14 |
Correct |
6 ms |
5400 KB |
Output is correct |
15 |
Correct |
5 ms |
5400 KB |
Output is correct |
16 |
Correct |
5 ms |
5400 KB |
Output is correct |
17 |
Correct |
5 ms |
5400 KB |
Output is correct |
18 |
Correct |
5 ms |
5400 KB |
Output is correct |
19 |
Correct |
5 ms |
5400 KB |
Output is correct |
20 |
Correct |
5 ms |
5400 KB |
Output is correct |
21 |
Correct |
7 ms |
5400 KB |
Output is correct |
22 |
Correct |
6 ms |
5400 KB |
Output is correct |
23 |
Correct |
6 ms |
5400 KB |
Output is correct |
24 |
Correct |
7 ms |
5400 KB |
Output is correct |
25 |
Correct |
6 ms |
5400 KB |
Output is correct |
26 |
Correct |
6 ms |
5400 KB |
Output is correct |
27 |
Correct |
5 ms |
5400 KB |
Output is correct |
28 |
Correct |
6 ms |
5452 KB |
Output is correct |
29 |
Correct |
6 ms |
5452 KB |
Output is correct |
30 |
Correct |
6 ms |
5452 KB |
Output is correct |
31 |
Correct |
6 ms |
5452 KB |
Output is correct |
32 |
Correct |
7 ms |
5452 KB |
Output is correct |
33 |
Correct |
6 ms |
5452 KB |
Output is correct |
34 |
Correct |
6 ms |
5452 KB |
Output is correct |
35 |
Correct |
6 ms |
5452 KB |
Output is correct |
36 |
Correct |
6 ms |
5452 KB |
Output is correct |
37 |
Correct |
6 ms |
5452 KB |
Output is correct |
38 |
Correct |
6 ms |
5452 KB |
Output is correct |
39 |
Correct |
126 ms |
10092 KB |
Output is correct |
40 |
Correct |
133 ms |
10264 KB |
Output is correct |
41 |
Correct |
132 ms |
10264 KB |
Output is correct |
42 |
Correct |
138 ms |
10352 KB |
Output is correct |
43 |
Correct |
134 ms |
10352 KB |
Output is correct |
44 |
Correct |
118 ms |
10388 KB |
Output is correct |
45 |
Correct |
120 ms |
23192 KB |
Output is correct |
46 |
Correct |
115 ms |
34540 KB |
Output is correct |
47 |
Correct |
231 ms |
34540 KB |
Output is correct |
48 |
Correct |
335 ms |
66484 KB |
Output is correct |
49 |
Correct |
332 ms |
66484 KB |
Output is correct |
50 |
Correct |
236 ms |
66484 KB |
Output is correct |
51 |
Correct |
224 ms |
66484 KB |
Output is correct |
52 |
Correct |
229 ms |
66484 KB |
Output is correct |
53 |
Correct |
213 ms |
66484 KB |
Output is correct |
54 |
Correct |
192 ms |
66484 KB |
Output is correct |
55 |
Correct |
20 ms |
66484 KB |
Output is correct |
56 |
Correct |
15 ms |
66484 KB |
Output is correct |
57 |
Correct |
94 ms |
66484 KB |
Output is correct |
58 |
Correct |
65 ms |
66484 KB |
Output is correct |
59 |
Correct |
112 ms |
66484 KB |
Output is correct |
60 |
Correct |
350 ms |
66484 KB |
Output is correct |
61 |
Correct |
285 ms |
66484 KB |
Output is correct |
62 |
Correct |
244 ms |
66484 KB |
Output is correct |
63 |
Correct |
274 ms |
66484 KB |
Output is correct |
64 |
Correct |
518 ms |
66484 KB |
Output is correct |
65 |
Correct |
261 ms |
66484 KB |
Output is correct |
66 |
Correct |
417 ms |
66484 KB |
Output is correct |
67 |
Correct |
208 ms |
66484 KB |
Output is correct |
68 |
Correct |
347 ms |
66484 KB |
Output is correct |
69 |
Correct |
434 ms |
66484 KB |
Output is correct |
70 |
Correct |
340 ms |
66484 KB |
Output is correct |