#include "race.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 200200;
const int L = 2000000;
const int INF = 1e9;
struct edge {
int to;
int w;
};
int n, k;
int sz[N];
bool rmv[N];
vector<edge> g[N];
int calc(int s, int p) {
sz[s] = 1;
for (auto [to, w] : g[s]) {
if (to == p || rmv[to]) continue;
sz[s] += calc(to, s);
}
return sz[s];
}
int find_centroid(int s, int p, int m) {
for (auto [to, w] : g[s]) {
if (to == p || rmv[to]) continue;
if (sz[to] * 2 > m)
return find_centroid(to, s, m);
}
return s;
}
int answer;
int mp[L];
void get_answer(int s, int p, int dep, int len) {
if (len <= k)
answer = min(answer, mp[k - len] + dep);
for (auto [to, w] : g[s]) {
if (to == p || rmv[to]) continue;
get_answer(to, s, dep + 1, len + w);
}
}
void add_to_map(int s, int p, int dep, int len) {
if (len <= k)
mp[len] = min(mp[len], dep);
for (auto [to, w] : g[s]) {
if (to == p || rmv[to]) continue;
add_to_map(to, s, dep + 1, len + w);
}
}
void clear_map(int s, int p, int dep, int len) {
if (len <= k)
mp[len] = INF;
for (auto [to, w] : g[s]) {
if (to == p || rmv[to]) continue;
clear_map(to, s, dep + 1, len + w);
}
}
void decompose(int s) {
int C = find_centroid(s, s, calc(s, s));
mp[0] = 0;
for (auto [to, w] : g[C]) {
if (rmv[to]) continue;
get_answer(to, C, 1, w);
add_to_map(to, C, 1, w);
}
for (auto [to, w] : g[C]) {
if (rmv[to]) continue;
clear_map(to, C, 1, w);
}
mp[0] = INF;
rmv[C] = true;
for (auto [to, w] : g[C]) {
if (!rmv[to])
decompose(to);
}
}
int best_path(int n_inside, int k_inside, int H[][2], int L[]) {
n = n_inside;
k = k_inside;
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]});
}
answer = INF;
for (int x = 0; x <= k; x++)
mp[x] = INF;
decompose(1);
if (answer == INF)
return -1;
return answer;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10584 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10584 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10620 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
1 ms |
10588 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10584 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10584 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10620 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
1 ms |
10588 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
3 ms |
10588 KB |
Output is correct |
21 |
Correct |
3 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
14684 KB |
Output is correct |
23 |
Correct |
3 ms |
14684 KB |
Output is correct |
24 |
Correct |
2 ms |
14684 KB |
Output is correct |
25 |
Correct |
3 ms |
14684 KB |
Output is correct |
26 |
Correct |
3 ms |
12636 KB |
Output is correct |
27 |
Correct |
3 ms |
14812 KB |
Output is correct |
28 |
Correct |
2 ms |
12636 KB |
Output is correct |
29 |
Correct |
2 ms |
12636 KB |
Output is correct |
30 |
Correct |
2 ms |
12636 KB |
Output is correct |
31 |
Correct |
3 ms |
14804 KB |
Output is correct |
32 |
Correct |
2 ms |
14684 KB |
Output is correct |
33 |
Correct |
3 ms |
14684 KB |
Output is correct |
34 |
Correct |
2 ms |
12636 KB |
Output is correct |
35 |
Correct |
2 ms |
14684 KB |
Output is correct |
36 |
Correct |
3 ms |
14796 KB |
Output is correct |
37 |
Correct |
2 ms |
14684 KB |
Output is correct |
38 |
Correct |
3 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10584 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10584 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10620 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
1 ms |
10588 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
19 |
Correct |
101 ms |
17024 KB |
Output is correct |
20 |
Correct |
100 ms |
17748 KB |
Output is correct |
21 |
Correct |
93 ms |
17492 KB |
Output is correct |
22 |
Correct |
76 ms |
18264 KB |
Output is correct |
23 |
Correct |
68 ms |
18540 KB |
Output is correct |
24 |
Correct |
39 ms |
18168 KB |
Output is correct |
25 |
Correct |
115 ms |
23220 KB |
Output is correct |
26 |
Correct |
84 ms |
23380 KB |
Output is correct |
27 |
Correct |
133 ms |
22612 KB |
Output is correct |
28 |
Correct |
256 ms |
33108 KB |
Output is correct |
29 |
Correct |
250 ms |
29524 KB |
Output is correct |
30 |
Correct |
116 ms |
23180 KB |
Output is correct |
31 |
Correct |
119 ms |
23380 KB |
Output is correct |
32 |
Correct |
144 ms |
23248 KB |
Output is correct |
33 |
Correct |
182 ms |
22096 KB |
Output is correct |
34 |
Correct |
192 ms |
22520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10584 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10584 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10620 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
1 ms |
10588 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
3 ms |
10588 KB |
Output is correct |
21 |
Correct |
3 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
14684 KB |
Output is correct |
23 |
Correct |
3 ms |
14684 KB |
Output is correct |
24 |
Correct |
2 ms |
14684 KB |
Output is correct |
25 |
Correct |
3 ms |
14684 KB |
Output is correct |
26 |
Correct |
3 ms |
12636 KB |
Output is correct |
27 |
Correct |
3 ms |
14812 KB |
Output is correct |
28 |
Correct |
2 ms |
12636 KB |
Output is correct |
29 |
Correct |
2 ms |
12636 KB |
Output is correct |
30 |
Correct |
2 ms |
12636 KB |
Output is correct |
31 |
Correct |
3 ms |
14804 KB |
Output is correct |
32 |
Correct |
2 ms |
14684 KB |
Output is correct |
33 |
Correct |
3 ms |
14684 KB |
Output is correct |
34 |
Correct |
2 ms |
12636 KB |
Output is correct |
35 |
Correct |
2 ms |
14684 KB |
Output is correct |
36 |
Correct |
3 ms |
14796 KB |
Output is correct |
37 |
Correct |
2 ms |
14684 KB |
Output is correct |
38 |
Correct |
3 ms |
12636 KB |
Output is correct |
39 |
Correct |
101 ms |
17024 KB |
Output is correct |
40 |
Correct |
100 ms |
17748 KB |
Output is correct |
41 |
Correct |
93 ms |
17492 KB |
Output is correct |
42 |
Correct |
76 ms |
18264 KB |
Output is correct |
43 |
Correct |
68 ms |
18540 KB |
Output is correct |
44 |
Correct |
39 ms |
18168 KB |
Output is correct |
45 |
Correct |
115 ms |
23220 KB |
Output is correct |
46 |
Correct |
84 ms |
23380 KB |
Output is correct |
47 |
Correct |
133 ms |
22612 KB |
Output is correct |
48 |
Correct |
256 ms |
33108 KB |
Output is correct |
49 |
Correct |
250 ms |
29524 KB |
Output is correct |
50 |
Correct |
116 ms |
23180 KB |
Output is correct |
51 |
Correct |
119 ms |
23380 KB |
Output is correct |
52 |
Correct |
144 ms |
23248 KB |
Output is correct |
53 |
Correct |
182 ms |
22096 KB |
Output is correct |
54 |
Correct |
192 ms |
22520 KB |
Output is correct |
55 |
Correct |
11 ms |
11100 KB |
Output is correct |
56 |
Correct |
8 ms |
11100 KB |
Output is correct |
57 |
Correct |
46 ms |
17252 KB |
Output is correct |
58 |
Correct |
21 ms |
18128 KB |
Output is correct |
59 |
Correct |
67 ms |
25556 KB |
Output is correct |
60 |
Correct |
261 ms |
33604 KB |
Output is correct |
61 |
Correct |
118 ms |
22776 KB |
Output is correct |
62 |
Correct |
117 ms |
27184 KB |
Output is correct |
63 |
Correct |
132 ms |
27220 KB |
Output is correct |
64 |
Correct |
243 ms |
23892 KB |
Output is correct |
65 |
Correct |
172 ms |
24272 KB |
Output is correct |
66 |
Correct |
262 ms |
34644 KB |
Output is correct |
67 |
Correct |
66 ms |
28868 KB |
Output is correct |
68 |
Correct |
142 ms |
27728 KB |
Output is correct |
69 |
Correct |
121 ms |
25648 KB |
Output is correct |
70 |
Correct |
131 ms |
27732 KB |
Output is correct |