#include "race.h"
#include <bits/stdc++.h>
#define N 200200
using namespace std;
vector<pair<int, int> > g[N];
int d[N];
int p[N];
int get_centroid(int v, int parent, int n, int ¢roid) {
int size = 1;
for (auto[x, y]:g[v]) {
if (x != parent && d[x] == -1) {
size += get_centroid(x, v, n, centroid);
}
}
if (size * 2 >= n && centroid == -1) centroid = v;
return size;
}
int K;
int ans = 1e9;
void get_compo_dists(int v, int dist, int len, int pass_limit, vector<pair<int, int> > &vec, int p = -1) {
if (dist > K) return;
vec.emplace_back(dist, len);
for (auto[x, y]:g[v]) {
if ((d[x] > pass_limit || d[x] == -1) && p != x) {
get_compo_dists(x, dist + y, len + 1, pass_limit, vec, v);
}
}
}
void build(int v, int parent, int depth, int n) {
int centroid = -1;
get_centroid(v, -1, n, centroid);
if (centroid == -1) centroid = v;
d[centroid] = depth;
p[centroid] = parent;
map<int, int> dist;
dist[0] = 0;
for (auto[x, y]:g[centroid]) {
if (d[x] == -1) {
vector<pair<int, int>> vec;
get_compo_dists(x, y, 1, d[centroid], vec);
for (auto[distance, cost]: vec) {
if (dist.count(K - distance)) {
ans = min(ans, cost + dist[K - distance]);
}
}
for (auto[distance, cost]: vec) {
if (dist.count(distance) == 0) {
dist[distance] = cost;
} else dist[distance] = min(dist[distance], cost);
}
build(x, centroid, depth + 1, (n + 1) / 2);
}
}
// cerr << "Centroid = " << centroid << endl;
// for (auto[x, y]:dist) {
// cerr << x << ' ' << y << endl;
// }
}
int best_path(int n, int K, int H[][2], int L[]) {
::K = K;
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]);
}
fill(d, d + N, -1);
build(0, -1, 1, n);
return (ans == 1e9 ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5708 KB |
Output is correct |
2 |
Correct |
4 ms |
5708 KB |
Output is correct |
3 |
Correct |
4 ms |
5708 KB |
Output is correct |
4 |
Correct |
4 ms |
5712 KB |
Output is correct |
5 |
Correct |
4 ms |
5708 KB |
Output is correct |
6 |
Correct |
5 ms |
5708 KB |
Output is correct |
7 |
Correct |
4 ms |
5708 KB |
Output is correct |
8 |
Correct |
4 ms |
5708 KB |
Output is correct |
9 |
Correct |
4 ms |
5708 KB |
Output is correct |
10 |
Correct |
4 ms |
5708 KB |
Output is correct |
11 |
Correct |
4 ms |
5708 KB |
Output is correct |
12 |
Correct |
4 ms |
5708 KB |
Output is correct |
13 |
Correct |
4 ms |
5816 KB |
Output is correct |
14 |
Correct |
4 ms |
5708 KB |
Output is correct |
15 |
Correct |
4 ms |
5708 KB |
Output is correct |
16 |
Correct |
4 ms |
5708 KB |
Output is correct |
17 |
Correct |
4 ms |
5708 KB |
Output is correct |
18 |
Correct |
4 ms |
5812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5708 KB |
Output is correct |
2 |
Correct |
4 ms |
5708 KB |
Output is correct |
3 |
Correct |
4 ms |
5708 KB |
Output is correct |
4 |
Correct |
4 ms |
5712 KB |
Output is correct |
5 |
Correct |
4 ms |
5708 KB |
Output is correct |
6 |
Correct |
5 ms |
5708 KB |
Output is correct |
7 |
Correct |
4 ms |
5708 KB |
Output is correct |
8 |
Correct |
4 ms |
5708 KB |
Output is correct |
9 |
Correct |
4 ms |
5708 KB |
Output is correct |
10 |
Correct |
4 ms |
5708 KB |
Output is correct |
11 |
Correct |
4 ms |
5708 KB |
Output is correct |
12 |
Correct |
4 ms |
5708 KB |
Output is correct |
13 |
Correct |
4 ms |
5816 KB |
Output is correct |
14 |
Correct |
4 ms |
5708 KB |
Output is correct |
15 |
Correct |
4 ms |
5708 KB |
Output is correct |
16 |
Correct |
4 ms |
5708 KB |
Output is correct |
17 |
Correct |
4 ms |
5708 KB |
Output is correct |
18 |
Correct |
4 ms |
5812 KB |
Output is correct |
19 |
Correct |
4 ms |
5708 KB |
Output is correct |
20 |
Correct |
4 ms |
5708 KB |
Output is correct |
21 |
Correct |
5 ms |
5876 KB |
Output is correct |
22 |
Correct |
4 ms |
5836 KB |
Output is correct |
23 |
Correct |
5 ms |
5836 KB |
Output is correct |
24 |
Correct |
5 ms |
5836 KB |
Output is correct |
25 |
Correct |
6 ms |
5836 KB |
Output is correct |
26 |
Correct |
5 ms |
5800 KB |
Output is correct |
27 |
Correct |
5 ms |
5836 KB |
Output is correct |
28 |
Correct |
7 ms |
5836 KB |
Output is correct |
29 |
Correct |
6 ms |
5836 KB |
Output is correct |
30 |
Correct |
6 ms |
5836 KB |
Output is correct |
31 |
Correct |
5 ms |
5836 KB |
Output is correct |
32 |
Correct |
6 ms |
5836 KB |
Output is correct |
33 |
Correct |
6 ms |
5836 KB |
Output is correct |
34 |
Correct |
5 ms |
5836 KB |
Output is correct |
35 |
Correct |
5 ms |
5836 KB |
Output is correct |
36 |
Correct |
5 ms |
5896 KB |
Output is correct |
37 |
Correct |
6 ms |
5912 KB |
Output is correct |
38 |
Correct |
6 ms |
5836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5708 KB |
Output is correct |
2 |
Correct |
4 ms |
5708 KB |
Output is correct |
3 |
Correct |
4 ms |
5708 KB |
Output is correct |
4 |
Correct |
4 ms |
5712 KB |
Output is correct |
5 |
Correct |
4 ms |
5708 KB |
Output is correct |
6 |
Correct |
5 ms |
5708 KB |
Output is correct |
7 |
Correct |
4 ms |
5708 KB |
Output is correct |
8 |
Correct |
4 ms |
5708 KB |
Output is correct |
9 |
Correct |
4 ms |
5708 KB |
Output is correct |
10 |
Correct |
4 ms |
5708 KB |
Output is correct |
11 |
Correct |
4 ms |
5708 KB |
Output is correct |
12 |
Correct |
4 ms |
5708 KB |
Output is correct |
13 |
Correct |
4 ms |
5816 KB |
Output is correct |
14 |
Correct |
4 ms |
5708 KB |
Output is correct |
15 |
Correct |
4 ms |
5708 KB |
Output is correct |
16 |
Correct |
4 ms |
5708 KB |
Output is correct |
17 |
Correct |
4 ms |
5708 KB |
Output is correct |
18 |
Correct |
4 ms |
5812 KB |
Output is correct |
19 |
Correct |
330 ms |
11232 KB |
Output is correct |
20 |
Correct |
321 ms |
11196 KB |
Output is correct |
21 |
Correct |
369 ms |
11584 KB |
Output is correct |
22 |
Correct |
322 ms |
11784 KB |
Output is correct |
23 |
Correct |
135 ms |
11240 KB |
Output is correct |
24 |
Correct |
97 ms |
11192 KB |
Output is correct |
25 |
Correct |
166 ms |
14740 KB |
Output is correct |
26 |
Correct |
128 ms |
19096 KB |
Output is correct |
27 |
Correct |
323 ms |
16504 KB |
Output is correct |
28 |
Correct |
307 ms |
30816 KB |
Output is correct |
29 |
Correct |
380 ms |
29620 KB |
Output is correct |
30 |
Correct |
299 ms |
16520 KB |
Output is correct |
31 |
Correct |
278 ms |
16460 KB |
Output is correct |
32 |
Correct |
420 ms |
16572 KB |
Output is correct |
33 |
Correct |
406 ms |
15556 KB |
Output is correct |
34 |
Correct |
324 ms |
15292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5708 KB |
Output is correct |
2 |
Correct |
4 ms |
5708 KB |
Output is correct |
3 |
Correct |
4 ms |
5708 KB |
Output is correct |
4 |
Correct |
4 ms |
5712 KB |
Output is correct |
5 |
Correct |
4 ms |
5708 KB |
Output is correct |
6 |
Correct |
5 ms |
5708 KB |
Output is correct |
7 |
Correct |
4 ms |
5708 KB |
Output is correct |
8 |
Correct |
4 ms |
5708 KB |
Output is correct |
9 |
Correct |
4 ms |
5708 KB |
Output is correct |
10 |
Correct |
4 ms |
5708 KB |
Output is correct |
11 |
Correct |
4 ms |
5708 KB |
Output is correct |
12 |
Correct |
4 ms |
5708 KB |
Output is correct |
13 |
Correct |
4 ms |
5816 KB |
Output is correct |
14 |
Correct |
4 ms |
5708 KB |
Output is correct |
15 |
Correct |
4 ms |
5708 KB |
Output is correct |
16 |
Correct |
4 ms |
5708 KB |
Output is correct |
17 |
Correct |
4 ms |
5708 KB |
Output is correct |
18 |
Correct |
4 ms |
5812 KB |
Output is correct |
19 |
Correct |
4 ms |
5708 KB |
Output is correct |
20 |
Correct |
4 ms |
5708 KB |
Output is correct |
21 |
Correct |
5 ms |
5876 KB |
Output is correct |
22 |
Correct |
4 ms |
5836 KB |
Output is correct |
23 |
Correct |
5 ms |
5836 KB |
Output is correct |
24 |
Correct |
5 ms |
5836 KB |
Output is correct |
25 |
Correct |
6 ms |
5836 KB |
Output is correct |
26 |
Correct |
5 ms |
5800 KB |
Output is correct |
27 |
Correct |
5 ms |
5836 KB |
Output is correct |
28 |
Correct |
7 ms |
5836 KB |
Output is correct |
29 |
Correct |
6 ms |
5836 KB |
Output is correct |
30 |
Correct |
6 ms |
5836 KB |
Output is correct |
31 |
Correct |
5 ms |
5836 KB |
Output is correct |
32 |
Correct |
6 ms |
5836 KB |
Output is correct |
33 |
Correct |
6 ms |
5836 KB |
Output is correct |
34 |
Correct |
5 ms |
5836 KB |
Output is correct |
35 |
Correct |
5 ms |
5836 KB |
Output is correct |
36 |
Correct |
5 ms |
5896 KB |
Output is correct |
37 |
Correct |
6 ms |
5912 KB |
Output is correct |
38 |
Correct |
6 ms |
5836 KB |
Output is correct |
39 |
Correct |
330 ms |
11232 KB |
Output is correct |
40 |
Correct |
321 ms |
11196 KB |
Output is correct |
41 |
Correct |
369 ms |
11584 KB |
Output is correct |
42 |
Correct |
322 ms |
11784 KB |
Output is correct |
43 |
Correct |
135 ms |
11240 KB |
Output is correct |
44 |
Correct |
97 ms |
11192 KB |
Output is correct |
45 |
Correct |
166 ms |
14740 KB |
Output is correct |
46 |
Correct |
128 ms |
19096 KB |
Output is correct |
47 |
Correct |
323 ms |
16504 KB |
Output is correct |
48 |
Correct |
307 ms |
30816 KB |
Output is correct |
49 |
Correct |
380 ms |
29620 KB |
Output is correct |
50 |
Correct |
299 ms |
16520 KB |
Output is correct |
51 |
Correct |
278 ms |
16460 KB |
Output is correct |
52 |
Correct |
420 ms |
16572 KB |
Output is correct |
53 |
Correct |
406 ms |
15556 KB |
Output is correct |
54 |
Correct |
324 ms |
15292 KB |
Output is correct |
55 |
Correct |
27 ms |
6604 KB |
Output is correct |
56 |
Correct |
22 ms |
6444 KB |
Output is correct |
57 |
Correct |
171 ms |
11764 KB |
Output is correct |
58 |
Correct |
61 ms |
11280 KB |
Output is correct |
59 |
Correct |
385 ms |
26364 KB |
Output is correct |
60 |
Correct |
1179 ms |
49368 KB |
Output is correct |
61 |
Correct |
472 ms |
16804 KB |
Output is correct |
62 |
Correct |
640 ms |
16888 KB |
Output is correct |
63 |
Correct |
775 ms |
17080 KB |
Output is correct |
64 |
Correct |
1456 ms |
27668 KB |
Output is correct |
65 |
Correct |
400 ms |
16340 KB |
Output is correct |
66 |
Correct |
763 ms |
28480 KB |
Output is correct |
67 |
Correct |
202 ms |
16716 KB |
Output is correct |
68 |
Correct |
914 ms |
20900 KB |
Output is correct |
69 |
Correct |
902 ms |
20568 KB |
Output is correct |
70 |
Correct |
890 ms |
19964 KB |
Output is correct |