#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> ii;
vector<ii> adj[200001];
int h[200001], d[200001];
map<int, int> mp[200001];
int dfs(int u, int p, int k) {
int ans = 1e9;
mp[u][d[u]] = h[u];
for (auto& [v, w] : adj[u]) {
if (v == p) continue;
h[v] = h[u] + 1;
d[v] = d[u] + w;
ans = min(ans, dfs(v, u, k));
if (mp[v].size() > mp[u].size()) swap(mp[v], mp[u]);
for (auto& [dis, height] : mp[v])
if (mp[u].count(k - (dis - d[u]) + d[u]))
ans = min(ans, mp[u][k - (dis - d[u]) + d[u]] + height - 2 * h[u]);
for (auto& [dis, height] : mp[v]) {
if (!mp[u].count(dis)) mp[u][dis] = height;
else mp[u][dis] = min(mp[u][dis], height);
}
mp[v].clear();
}
return ans;
}
int best_path(int n, int k, int h[][2], int l[]) {
for (int i = 0; i < n - 1; i++) {
int u = h[i][0], v = h[i][1], w = l[i];
adj[u].push_back(ii(v, w));
adj[v].push_back(ii(u, w));
}
int ans = dfs(0, -1, k);
if (ans == 1e9) ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14412 KB |
Output is correct |
2 |
Correct |
7 ms |
14396 KB |
Output is correct |
3 |
Correct |
8 ms |
14408 KB |
Output is correct |
4 |
Correct |
8 ms |
14348 KB |
Output is correct |
5 |
Correct |
7 ms |
14412 KB |
Output is correct |
6 |
Correct |
7 ms |
14412 KB |
Output is correct |
7 |
Correct |
7 ms |
14412 KB |
Output is correct |
8 |
Correct |
7 ms |
14412 KB |
Output is correct |
9 |
Correct |
7 ms |
14400 KB |
Output is correct |
10 |
Correct |
8 ms |
14412 KB |
Output is correct |
11 |
Correct |
8 ms |
14412 KB |
Output is correct |
12 |
Correct |
7 ms |
14412 KB |
Output is correct |
13 |
Correct |
8 ms |
14392 KB |
Output is correct |
14 |
Correct |
8 ms |
14388 KB |
Output is correct |
15 |
Correct |
7 ms |
14396 KB |
Output is correct |
16 |
Correct |
7 ms |
14396 KB |
Output is correct |
17 |
Correct |
7 ms |
14412 KB |
Output is correct |
18 |
Correct |
7 ms |
14412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14412 KB |
Output is correct |
2 |
Correct |
7 ms |
14396 KB |
Output is correct |
3 |
Correct |
8 ms |
14408 KB |
Output is correct |
4 |
Correct |
8 ms |
14348 KB |
Output is correct |
5 |
Correct |
7 ms |
14412 KB |
Output is correct |
6 |
Correct |
7 ms |
14412 KB |
Output is correct |
7 |
Correct |
7 ms |
14412 KB |
Output is correct |
8 |
Correct |
7 ms |
14412 KB |
Output is correct |
9 |
Correct |
7 ms |
14400 KB |
Output is correct |
10 |
Correct |
8 ms |
14412 KB |
Output is correct |
11 |
Correct |
8 ms |
14412 KB |
Output is correct |
12 |
Correct |
7 ms |
14412 KB |
Output is correct |
13 |
Correct |
8 ms |
14392 KB |
Output is correct |
14 |
Correct |
8 ms |
14388 KB |
Output is correct |
15 |
Correct |
7 ms |
14396 KB |
Output is correct |
16 |
Correct |
7 ms |
14396 KB |
Output is correct |
17 |
Correct |
7 ms |
14412 KB |
Output is correct |
18 |
Correct |
7 ms |
14412 KB |
Output is correct |
19 |
Correct |
8 ms |
14388 KB |
Output is correct |
20 |
Correct |
7 ms |
14396 KB |
Output is correct |
21 |
Correct |
8 ms |
14404 KB |
Output is correct |
22 |
Correct |
8 ms |
14420 KB |
Output is correct |
23 |
Correct |
9 ms |
14388 KB |
Output is correct |
24 |
Correct |
8 ms |
14412 KB |
Output is correct |
25 |
Correct |
9 ms |
14544 KB |
Output is correct |
26 |
Correct |
9 ms |
14540 KB |
Output is correct |
27 |
Correct |
7 ms |
14404 KB |
Output is correct |
28 |
Correct |
9 ms |
14404 KB |
Output is correct |
29 |
Correct |
8 ms |
14412 KB |
Output is correct |
30 |
Correct |
9 ms |
14408 KB |
Output is correct |
31 |
Correct |
10 ms |
14428 KB |
Output is correct |
32 |
Correct |
8 ms |
14512 KB |
Output is correct |
33 |
Correct |
9 ms |
14540 KB |
Output is correct |
34 |
Correct |
8 ms |
14516 KB |
Output is correct |
35 |
Correct |
9 ms |
14524 KB |
Output is correct |
36 |
Correct |
10 ms |
14532 KB |
Output is correct |
37 |
Correct |
8 ms |
14528 KB |
Output is correct |
38 |
Correct |
8 ms |
14404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14412 KB |
Output is correct |
2 |
Correct |
7 ms |
14396 KB |
Output is correct |
3 |
Correct |
8 ms |
14408 KB |
Output is correct |
4 |
Correct |
8 ms |
14348 KB |
Output is correct |
5 |
Correct |
7 ms |
14412 KB |
Output is correct |
6 |
Correct |
7 ms |
14412 KB |
Output is correct |
7 |
Correct |
7 ms |
14412 KB |
Output is correct |
8 |
Correct |
7 ms |
14412 KB |
Output is correct |
9 |
Correct |
7 ms |
14400 KB |
Output is correct |
10 |
Correct |
8 ms |
14412 KB |
Output is correct |
11 |
Correct |
8 ms |
14412 KB |
Output is correct |
12 |
Correct |
7 ms |
14412 KB |
Output is correct |
13 |
Correct |
8 ms |
14392 KB |
Output is correct |
14 |
Correct |
8 ms |
14388 KB |
Output is correct |
15 |
Correct |
7 ms |
14396 KB |
Output is correct |
16 |
Correct |
7 ms |
14396 KB |
Output is correct |
17 |
Correct |
7 ms |
14412 KB |
Output is correct |
18 |
Correct |
7 ms |
14412 KB |
Output is correct |
19 |
Correct |
101 ms |
21440 KB |
Output is correct |
20 |
Correct |
105 ms |
21408 KB |
Output is correct |
21 |
Correct |
96 ms |
21320 KB |
Output is correct |
22 |
Correct |
95 ms |
21436 KB |
Output is correct |
23 |
Correct |
131 ms |
21972 KB |
Output is correct |
24 |
Correct |
98 ms |
21572 KB |
Output is correct |
25 |
Correct |
87 ms |
30912 KB |
Output is correct |
26 |
Correct |
59 ms |
39528 KB |
Output is correct |
27 |
Correct |
170 ms |
29016 KB |
Output is correct |
28 |
Correct |
273 ms |
65092 KB |
Output is correct |
29 |
Correct |
253 ms |
63664 KB |
Output is correct |
30 |
Correct |
172 ms |
28868 KB |
Output is correct |
31 |
Correct |
164 ms |
28868 KB |
Output is correct |
32 |
Correct |
208 ms |
29116 KB |
Output is correct |
33 |
Correct |
186 ms |
28284 KB |
Output is correct |
34 |
Correct |
274 ms |
38584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14412 KB |
Output is correct |
2 |
Correct |
7 ms |
14396 KB |
Output is correct |
3 |
Correct |
8 ms |
14408 KB |
Output is correct |
4 |
Correct |
8 ms |
14348 KB |
Output is correct |
5 |
Correct |
7 ms |
14412 KB |
Output is correct |
6 |
Correct |
7 ms |
14412 KB |
Output is correct |
7 |
Correct |
7 ms |
14412 KB |
Output is correct |
8 |
Correct |
7 ms |
14412 KB |
Output is correct |
9 |
Correct |
7 ms |
14400 KB |
Output is correct |
10 |
Correct |
8 ms |
14412 KB |
Output is correct |
11 |
Correct |
8 ms |
14412 KB |
Output is correct |
12 |
Correct |
7 ms |
14412 KB |
Output is correct |
13 |
Correct |
8 ms |
14392 KB |
Output is correct |
14 |
Correct |
8 ms |
14388 KB |
Output is correct |
15 |
Correct |
7 ms |
14396 KB |
Output is correct |
16 |
Correct |
7 ms |
14396 KB |
Output is correct |
17 |
Correct |
7 ms |
14412 KB |
Output is correct |
18 |
Correct |
7 ms |
14412 KB |
Output is correct |
19 |
Correct |
8 ms |
14388 KB |
Output is correct |
20 |
Correct |
7 ms |
14396 KB |
Output is correct |
21 |
Correct |
8 ms |
14404 KB |
Output is correct |
22 |
Correct |
8 ms |
14420 KB |
Output is correct |
23 |
Correct |
9 ms |
14388 KB |
Output is correct |
24 |
Correct |
8 ms |
14412 KB |
Output is correct |
25 |
Correct |
9 ms |
14544 KB |
Output is correct |
26 |
Correct |
9 ms |
14540 KB |
Output is correct |
27 |
Correct |
7 ms |
14404 KB |
Output is correct |
28 |
Correct |
9 ms |
14404 KB |
Output is correct |
29 |
Correct |
8 ms |
14412 KB |
Output is correct |
30 |
Correct |
9 ms |
14408 KB |
Output is correct |
31 |
Correct |
10 ms |
14428 KB |
Output is correct |
32 |
Correct |
8 ms |
14512 KB |
Output is correct |
33 |
Correct |
9 ms |
14540 KB |
Output is correct |
34 |
Correct |
8 ms |
14516 KB |
Output is correct |
35 |
Correct |
9 ms |
14524 KB |
Output is correct |
36 |
Correct |
10 ms |
14532 KB |
Output is correct |
37 |
Correct |
8 ms |
14528 KB |
Output is correct |
38 |
Correct |
8 ms |
14404 KB |
Output is correct |
39 |
Correct |
101 ms |
21440 KB |
Output is correct |
40 |
Correct |
105 ms |
21408 KB |
Output is correct |
41 |
Correct |
96 ms |
21320 KB |
Output is correct |
42 |
Correct |
95 ms |
21436 KB |
Output is correct |
43 |
Correct |
131 ms |
21972 KB |
Output is correct |
44 |
Correct |
98 ms |
21572 KB |
Output is correct |
45 |
Correct |
87 ms |
30912 KB |
Output is correct |
46 |
Correct |
59 ms |
39528 KB |
Output is correct |
47 |
Correct |
170 ms |
29016 KB |
Output is correct |
48 |
Correct |
273 ms |
65092 KB |
Output is correct |
49 |
Correct |
253 ms |
63664 KB |
Output is correct |
50 |
Correct |
172 ms |
28868 KB |
Output is correct |
51 |
Correct |
164 ms |
28868 KB |
Output is correct |
52 |
Correct |
208 ms |
29116 KB |
Output is correct |
53 |
Correct |
186 ms |
28284 KB |
Output is correct |
54 |
Correct |
274 ms |
38584 KB |
Output is correct |
55 |
Correct |
23 ms |
15476 KB |
Output is correct |
56 |
Correct |
12 ms |
15040 KB |
Output is correct |
57 |
Correct |
57 ms |
21572 KB |
Output is correct |
58 |
Correct |
48 ms |
21232 KB |
Output is correct |
59 |
Correct |
82 ms |
39560 KB |
Output is correct |
60 |
Correct |
269 ms |
64068 KB |
Output is correct |
61 |
Correct |
200 ms |
30304 KB |
Output is correct |
62 |
Correct |
157 ms |
29036 KB |
Output is correct |
63 |
Correct |
203 ms |
29128 KB |
Output is correct |
64 |
Correct |
363 ms |
35580 KB |
Output is correct |
65 |
Correct |
369 ms |
39048 KB |
Output is correct |
66 |
Correct |
266 ms |
60844 KB |
Output is correct |
67 |
Correct |
134 ms |
29620 KB |
Output is correct |
68 |
Correct |
281 ms |
37572 KB |
Output is correct |
69 |
Correct |
314 ms |
41096 KB |
Output is correct |
70 |
Correct |
259 ms |
36548 KB |
Output is correct |