//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
#include "race.h"
using namespace std;
typedef long long ll;
typedef pair<int, ll> T;
const ll oo = 1e18;
int best_path(int n, int k, int H[][2], int L[]){
vector<vector<T>>g(n+1);
for (int i = 0; i<n-1; i++){
H[i][0]++;H[i][1]++;
}
for (int i = 0; i<n-1; i++){
g[H[i][0]].emplace_back(H[i][1], L[i]);
g[H[i][1]].emplace_back(H[i][0], L[i]);
}
vector<int>used(n+1), sz(n+1);
function<int(int, int)>dfssize = [&](int v, int pa){
sz[v] = 1;
for (auto [x, c]: g[v]){
if (x == pa || used[x]) continue;
sz[v] += dfssize(x, v);
}
return sz[v];
};
function<int(int, int, int)>get_centroid = [&](int v, int pa, int ms){
for (auto [x, c]: g[v]){
if (x == pa || used[x]) continue;
if (sz[x] * 2 > ms) return get_centroid(x, v, ms);
}
return v;
};
ll ans = oo;
vector<ll>depth(n+1), sum(n+1), mn(k+1, oo);
mn[0] = 0;
vector<int>now, to_clear;
function<void(int)>centroid = [&](int v){
v = get_centroid(v, v, dfssize(v, v));
function<void(int, int)>dfs = [&](int u, int pa){
now.emplace_back(u);
for (auto [x, c]: g[u]){
if (x == pa || used[x]) continue;
depth[x] = depth[u]+1;
sum[x] = sum[u]+c;
dfs(x, u);
}
};
for (auto [x, c]: g[v]){
if (used[x]) continue;
now.clear();
depth[x] = 1;
sum[x] = c;
dfs(x, v);
for (auto u: now){
if (k >= sum[u]) {
ans = min(ans, depth[u] + mn[k-sum[u]]);
}
}
for (auto u: now){
if (sum[u] <= k){
mn[sum[u]] = min(mn[sum[u]], depth[u]);
to_clear.emplace_back(sum[u]);
}
depth[u] = 0;
sum[u] = 0;
}
}
for (auto x: to_clear) mn[x] = oo;
to_clear.clear();
used[v] = 1;
for (auto [x, c]: g[v]){
if (!used[x]){
centroid(x);
}
}
};
centroid(1);
if (ans == oo) ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
288 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
304 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
288 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
304 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
4 ms |
7636 KB |
Output is correct |
23 |
Correct |
4 ms |
6356 KB |
Output is correct |
24 |
Correct |
4 ms |
7124 KB |
Output is correct |
25 |
Correct |
4 ms |
6976 KB |
Output is correct |
26 |
Correct |
2 ms |
3004 KB |
Output is correct |
27 |
Correct |
3 ms |
6612 KB |
Output is correct |
28 |
Correct |
2 ms |
2004 KB |
Output is correct |
29 |
Correct |
2 ms |
2900 KB |
Output is correct |
30 |
Correct |
3 ms |
3284 KB |
Output is correct |
31 |
Correct |
3 ms |
5568 KB |
Output is correct |
32 |
Correct |
3 ms |
5972 KB |
Output is correct |
33 |
Correct |
4 ms |
6484 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
4 ms |
6740 KB |
Output is correct |
36 |
Correct |
4 ms |
7636 KB |
Output is correct |
37 |
Correct |
3 ms |
6584 KB |
Output is correct |
38 |
Correct |
3 ms |
4308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
288 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
304 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
190 ms |
13008 KB |
Output is correct |
20 |
Correct |
185 ms |
13000 KB |
Output is correct |
21 |
Correct |
180 ms |
13184 KB |
Output is correct |
22 |
Correct |
155 ms |
13492 KB |
Output is correct |
23 |
Correct |
141 ms |
13416 KB |
Output is correct |
24 |
Correct |
75 ms |
12536 KB |
Output is correct |
25 |
Correct |
184 ms |
18340 KB |
Output is correct |
26 |
Correct |
129 ms |
23932 KB |
Output is correct |
27 |
Correct |
205 ms |
25604 KB |
Output is correct |
28 |
Correct |
528 ms |
47048 KB |
Output is correct |
29 |
Correct |
523 ms |
45364 KB |
Output is correct |
30 |
Correct |
203 ms |
25672 KB |
Output is correct |
31 |
Correct |
201 ms |
25724 KB |
Output is correct |
32 |
Correct |
285 ms |
25920 KB |
Output is correct |
33 |
Correct |
372 ms |
25140 KB |
Output is correct |
34 |
Correct |
372 ms |
25944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
288 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
304 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
4 ms |
7636 KB |
Output is correct |
23 |
Correct |
4 ms |
6356 KB |
Output is correct |
24 |
Correct |
4 ms |
7124 KB |
Output is correct |
25 |
Correct |
4 ms |
6976 KB |
Output is correct |
26 |
Correct |
2 ms |
3004 KB |
Output is correct |
27 |
Correct |
3 ms |
6612 KB |
Output is correct |
28 |
Correct |
2 ms |
2004 KB |
Output is correct |
29 |
Correct |
2 ms |
2900 KB |
Output is correct |
30 |
Correct |
3 ms |
3284 KB |
Output is correct |
31 |
Correct |
3 ms |
5568 KB |
Output is correct |
32 |
Correct |
3 ms |
5972 KB |
Output is correct |
33 |
Correct |
4 ms |
6484 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
4 ms |
6740 KB |
Output is correct |
36 |
Correct |
4 ms |
7636 KB |
Output is correct |
37 |
Correct |
3 ms |
6584 KB |
Output is correct |
38 |
Correct |
3 ms |
4308 KB |
Output is correct |
39 |
Correct |
190 ms |
13008 KB |
Output is correct |
40 |
Correct |
185 ms |
13000 KB |
Output is correct |
41 |
Correct |
180 ms |
13184 KB |
Output is correct |
42 |
Correct |
155 ms |
13492 KB |
Output is correct |
43 |
Correct |
141 ms |
13416 KB |
Output is correct |
44 |
Correct |
75 ms |
12536 KB |
Output is correct |
45 |
Correct |
184 ms |
18340 KB |
Output is correct |
46 |
Correct |
129 ms |
23932 KB |
Output is correct |
47 |
Correct |
205 ms |
25604 KB |
Output is correct |
48 |
Correct |
528 ms |
47048 KB |
Output is correct |
49 |
Correct |
523 ms |
45364 KB |
Output is correct |
50 |
Correct |
203 ms |
25672 KB |
Output is correct |
51 |
Correct |
201 ms |
25724 KB |
Output is correct |
52 |
Correct |
285 ms |
25920 KB |
Output is correct |
53 |
Correct |
372 ms |
25140 KB |
Output is correct |
54 |
Correct |
372 ms |
25944 KB |
Output is correct |
55 |
Correct |
10 ms |
1568 KB |
Output is correct |
56 |
Correct |
11 ms |
1620 KB |
Output is correct |
57 |
Correct |
108 ms |
13692 KB |
Output is correct |
58 |
Correct |
40 ms |
12828 KB |
Output is correct |
59 |
Correct |
134 ms |
25400 KB |
Output is correct |
60 |
Correct |
591 ms |
54560 KB |
Output is correct |
61 |
Correct |
209 ms |
26200 KB |
Output is correct |
62 |
Correct |
221 ms |
34748 KB |
Output is correct |
63 |
Correct |
295 ms |
34684 KB |
Output is correct |
64 |
Correct |
546 ms |
31224 KB |
Output is correct |
65 |
Correct |
405 ms |
26804 KB |
Output is correct |
66 |
Correct |
574 ms |
49760 KB |
Output is correct |
67 |
Correct |
134 ms |
34480 KB |
Output is correct |
68 |
Correct |
300 ms |
34780 KB |
Output is correct |
69 |
Correct |
292 ms |
34648 KB |
Output is correct |
70 |
Correct |
274 ms |
33388 KB |
Output is correct |