#include "race.h"
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
int n, k;
vector<vector<pii> > graph;
vector<bool> marked;
vector<int> sz;
int min_len = 1e9;
int find_centroid(int curr, int par, int all) {
for (auto &[a, w] : graph[curr])
if (a != par and !marked[a] and sz[a] * 2 > all)
return find_centroid(a, curr, all);
return curr;
}
vector<int> min_length_for_siz;
void reset_mlfs(int curr, int par, int depth) {
sz[curr] = 0;
if (depth > k)
return;
min_length_for_siz[depth] = 1e9;
sz[curr] = 1;
for (auto &[a, w] : graph[curr])
if (!marked[a] and a != par)
reset_mlfs(a, curr, depth + w), sz[curr] += sz[a];
}
void update_mlfs(int curr, int par, int depth, int len) {
if (depth > k)
return;
min_length_for_siz[depth] = min(min_length_for_siz[depth], len);
for (auto &[a, w] : graph[curr])
if (!marked[a] and a != par)
update_mlfs(a, curr, depth + w, len + 1);
}
void update_k(int curr, int par, int depth, int len) {
if (depth > k)
return;
min_len = min(min_len, len + min_length_for_siz[k-depth]);
for (auto &[a, w] : graph[curr])
if (!marked[a] and a != par)
update_k(a, curr, depth + w, len + 1);
}
void dfs(int curr) {
reset_mlfs(curr, 0, 0);
int cent = find_centroid(curr, 0, sz[curr]);
marked[cent] = true;
min_length_for_siz[0] = 0;
for (auto &[a, w] : graph[cent]) {
if (marked[a])
continue;
update_k(a, cent, w, 1);
update_mlfs(a, cent, w, 1);
}
reset_mlfs(cent, 0, 0);
for (auto &[a, w] : graph[cent])
if (!marked[a])
dfs(a);
}
int best_path(int N, int K, int H[][2], int L[])
{
n = N;
k = K;
graph.resize(n+1);
marked.resize(n+1);
sz.resize(n+1);
min_len = 1e9;
min_length_for_siz.resize(K+1, 1e9);
for (int i = 0; i < n - 1; i++) {
graph[H[i][0]].push_back(pair<int,int>(H[i][1], L[i]));
graph[H[i][1]].push_back(pair<int,int>(H[i][0], L[i]));
}
dfs(1);
if (min_len == 1e9)
return -1;
return min_len;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
308 KB |
Output is correct |
6 |
Correct |
1 ms |
304 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
264 KB |
Output is correct |
10 |
Correct |
0 ms |
312 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
304 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
308 KB |
Output is correct |
6 |
Correct |
1 ms |
304 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
264 KB |
Output is correct |
10 |
Correct |
0 ms |
312 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
304 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
308 KB |
Output is correct |
21 |
Correct |
1 ms |
312 KB |
Output is correct |
22 |
Correct |
2 ms |
3924 KB |
Output is correct |
23 |
Correct |
2 ms |
3284 KB |
Output is correct |
24 |
Correct |
2 ms |
3796 KB |
Output is correct |
25 |
Correct |
2 ms |
3648 KB |
Output is correct |
26 |
Correct |
1 ms |
1620 KB |
Output is correct |
27 |
Correct |
2 ms |
3540 KB |
Output is correct |
28 |
Correct |
1 ms |
1108 KB |
Output is correct |
29 |
Correct |
2 ms |
1620 KB |
Output is correct |
30 |
Correct |
2 ms |
1748 KB |
Output is correct |
31 |
Correct |
2 ms |
2900 KB |
Output is correct |
32 |
Correct |
3 ms |
3132 KB |
Output is correct |
33 |
Correct |
2 ms |
3412 KB |
Output is correct |
34 |
Correct |
2 ms |
2644 KB |
Output is correct |
35 |
Correct |
3 ms |
3540 KB |
Output is correct |
36 |
Correct |
2 ms |
3924 KB |
Output is correct |
37 |
Correct |
2 ms |
3412 KB |
Output is correct |
38 |
Correct |
2 ms |
2364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
308 KB |
Output is correct |
6 |
Correct |
1 ms |
304 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
264 KB |
Output is correct |
10 |
Correct |
0 ms |
312 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
304 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
125 ms |
9252 KB |
Output is correct |
20 |
Correct |
85 ms |
9164 KB |
Output is correct |
21 |
Correct |
118 ms |
9272 KB |
Output is correct |
22 |
Correct |
99 ms |
9240 KB |
Output is correct |
23 |
Correct |
39 ms |
9548 KB |
Output is correct |
24 |
Correct |
38 ms |
9460 KB |
Output is correct |
25 |
Correct |
74 ms |
8792 KB |
Output is correct |
26 |
Correct |
81 ms |
11868 KB |
Output is correct |
27 |
Correct |
136 ms |
18552 KB |
Output is correct |
28 |
Correct |
90 ms |
23588 KB |
Output is correct |
29 |
Correct |
126 ms |
21488 KB |
Output is correct |
30 |
Correct |
102 ms |
18552 KB |
Output is correct |
31 |
Correct |
110 ms |
18556 KB |
Output is correct |
32 |
Correct |
155 ms |
18660 KB |
Output is correct |
33 |
Correct |
121 ms |
17272 KB |
Output is correct |
34 |
Correct |
87 ms |
18248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
308 KB |
Output is correct |
6 |
Correct |
1 ms |
304 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
264 KB |
Output is correct |
10 |
Correct |
0 ms |
312 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
304 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
308 KB |
Output is correct |
21 |
Correct |
1 ms |
312 KB |
Output is correct |
22 |
Correct |
2 ms |
3924 KB |
Output is correct |
23 |
Correct |
2 ms |
3284 KB |
Output is correct |
24 |
Correct |
2 ms |
3796 KB |
Output is correct |
25 |
Correct |
2 ms |
3648 KB |
Output is correct |
26 |
Correct |
1 ms |
1620 KB |
Output is correct |
27 |
Correct |
2 ms |
3540 KB |
Output is correct |
28 |
Correct |
1 ms |
1108 KB |
Output is correct |
29 |
Correct |
2 ms |
1620 KB |
Output is correct |
30 |
Correct |
2 ms |
1748 KB |
Output is correct |
31 |
Correct |
2 ms |
2900 KB |
Output is correct |
32 |
Correct |
3 ms |
3132 KB |
Output is correct |
33 |
Correct |
2 ms |
3412 KB |
Output is correct |
34 |
Correct |
2 ms |
2644 KB |
Output is correct |
35 |
Correct |
3 ms |
3540 KB |
Output is correct |
36 |
Correct |
2 ms |
3924 KB |
Output is correct |
37 |
Correct |
2 ms |
3412 KB |
Output is correct |
38 |
Correct |
2 ms |
2364 KB |
Output is correct |
39 |
Correct |
125 ms |
9252 KB |
Output is correct |
40 |
Correct |
85 ms |
9164 KB |
Output is correct |
41 |
Correct |
118 ms |
9272 KB |
Output is correct |
42 |
Correct |
99 ms |
9240 KB |
Output is correct |
43 |
Correct |
39 ms |
9548 KB |
Output is correct |
44 |
Correct |
38 ms |
9460 KB |
Output is correct |
45 |
Correct |
74 ms |
8792 KB |
Output is correct |
46 |
Correct |
81 ms |
11868 KB |
Output is correct |
47 |
Correct |
136 ms |
18552 KB |
Output is correct |
48 |
Correct |
90 ms |
23588 KB |
Output is correct |
49 |
Correct |
126 ms |
21488 KB |
Output is correct |
50 |
Correct |
102 ms |
18552 KB |
Output is correct |
51 |
Correct |
110 ms |
18556 KB |
Output is correct |
52 |
Correct |
155 ms |
18660 KB |
Output is correct |
53 |
Correct |
121 ms |
17272 KB |
Output is correct |
54 |
Correct |
87 ms |
18248 KB |
Output is correct |
55 |
Correct |
7 ms |
1180 KB |
Output is correct |
56 |
Correct |
9 ms |
1108 KB |
Output is correct |
57 |
Correct |
79 ms |
9492 KB |
Output is correct |
58 |
Correct |
32 ms |
9140 KB |
Output is correct |
59 |
Correct |
87 ms |
15260 KB |
Output is correct |
60 |
Correct |
364 ms |
27756 KB |
Output is correct |
61 |
Correct |
122 ms |
18680 KB |
Output is correct |
62 |
Correct |
165 ms |
22584 KB |
Output is correct |
63 |
Correct |
183 ms |
22656 KB |
Output is correct |
64 |
Correct |
474 ms |
20976 KB |
Output is correct |
65 |
Correct |
110 ms |
19524 KB |
Output is correct |
66 |
Correct |
276 ms |
21652 KB |
Output is correct |
67 |
Correct |
107 ms |
23268 KB |
Output is correct |
68 |
Correct |
176 ms |
23196 KB |
Output is correct |
69 |
Correct |
187 ms |
23372 KB |
Output is correct |
70 |
Correct |
166 ms |
22448 KB |
Output is correct |