#include "race.h"
#include <bits/stdc++.h>
using namespace std;
const int INF = 1E9;
int k, ans = INF;
vector<vector<pair<int, int>>> g;
vector<int> sz, dep, to, ds;
vector<bool> centroid;
void calc(int u, int p) {
sz[u] = 1;
for (auto [v, w] : g[u])
if (v ^ p && !centroid[v])
calc(v, u), sz[u] += sz[v];
}
int findcentroid(int u, int p, const int &s) {
for (auto [v, w] : g[u])
if (v ^ p && !centroid[v] && sz[v] > s)
return findcentroid(v, u, s);
return u;
}
void calcdep(int u, int p, int d) {
dep[u] = d;
for (auto [v, w] : g[u])
if (v ^ p && !centroid[v])
calcdep(v, u, d + 1);
}
void addmx(int u, int p, int length) {
if (length > k) return;
ds[length] = min(ds[length], dep[u]);
to.push_back(length);
for (auto [v, w] : g[u])
if (v ^ p && !centroid[v])
addmx(v, u, length + w);
}
void calcmx(int u, int p, int length) {
if (length > k) return;
ans = min(ans, dep[u] + ds[k - length]);
for (auto [v, w] : g[u])
if (v ^ p && !centroid[v])
calcmx(v, u, length + w);
}
void decompose(int u) {
calc(u, u);
int x = findcentroid(u, u, sz[u] >> 1);
calcdep(x, x, 0);
centroid[x] = 1;
for (auto [v, w] : g[x]) {
if (!centroid[v]) {
calcmx(v, x, w);
addmx(v, x, w);
}
}
ans = min(ans, ds[k]);
for (int i : to) ds[i] = INF;
to.clear();
for (auto [v, w] : g[x])
if (!centroid[v])
decompose(v);
}
int best_path(int n, int k_, int h[][2], int l[]) {
k = k_;
g = vector<vector<pair<int, int>>> (n), centroid = vector<bool> (n), dep = vector<int> (n), ds = vector<int>(k + 1, INF), sz = dep;
for (int i = 0; i < n - 1; i++) {
int u = h[i][0], v = h[i][1], w = l[i];
g[u].push_back({v, w});
g[v].push_back({u, w});
}
decompose(0);
return ans == INF ? -1 : ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 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 |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 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 |
212 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 |
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 |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 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 |
212 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 |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 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 |
2 ms |
1620 KB |
Output is correct |
27 |
Correct |
2 ms |
3540 KB |
Output is correct |
28 |
Correct |
1 ms |
1092 KB |
Output is correct |
29 |
Correct |
2 ms |
1600 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 |
3156 KB |
Output is correct |
33 |
Correct |
3 ms |
3412 KB |
Output is correct |
34 |
Correct |
2 ms |
2620 KB |
Output is correct |
35 |
Correct |
3 ms |
3540 KB |
Output is correct |
36 |
Correct |
2 ms |
4028 KB |
Output is correct |
37 |
Correct |
2 ms |
3412 KB |
Output is correct |
38 |
Correct |
2 ms |
2388 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 |
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 |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 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 |
212 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 |
105 ms |
9712 KB |
Output is correct |
20 |
Correct |
109 ms |
9700 KB |
Output is correct |
21 |
Correct |
110 ms |
9980 KB |
Output is correct |
22 |
Correct |
96 ms |
10316 KB |
Output is correct |
23 |
Correct |
83 ms |
9956 KB |
Output is correct |
24 |
Correct |
41 ms |
9828 KB |
Output is correct |
25 |
Correct |
123 ms |
12400 KB |
Output is correct |
26 |
Correct |
94 ms |
15932 KB |
Output is correct |
27 |
Correct |
155 ms |
19380 KB |
Output is correct |
28 |
Correct |
207 ms |
30756 KB |
Output is correct |
29 |
Correct |
233 ms |
29860 KB |
Output is correct |
30 |
Correct |
135 ms |
19360 KB |
Output is correct |
31 |
Correct |
115 ms |
19392 KB |
Output is correct |
32 |
Correct |
194 ms |
19500 KB |
Output is correct |
33 |
Correct |
239 ms |
18232 KB |
Output is correct |
34 |
Correct |
199 ms |
19148 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 |
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 |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 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 |
212 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 |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 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 |
2 ms |
1620 KB |
Output is correct |
27 |
Correct |
2 ms |
3540 KB |
Output is correct |
28 |
Correct |
1 ms |
1092 KB |
Output is correct |
29 |
Correct |
2 ms |
1600 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 |
3156 KB |
Output is correct |
33 |
Correct |
3 ms |
3412 KB |
Output is correct |
34 |
Correct |
2 ms |
2620 KB |
Output is correct |
35 |
Correct |
3 ms |
3540 KB |
Output is correct |
36 |
Correct |
2 ms |
4028 KB |
Output is correct |
37 |
Correct |
2 ms |
3412 KB |
Output is correct |
38 |
Correct |
2 ms |
2388 KB |
Output is correct |
39 |
Correct |
105 ms |
9712 KB |
Output is correct |
40 |
Correct |
109 ms |
9700 KB |
Output is correct |
41 |
Correct |
110 ms |
9980 KB |
Output is correct |
42 |
Correct |
96 ms |
10316 KB |
Output is correct |
43 |
Correct |
83 ms |
9956 KB |
Output is correct |
44 |
Correct |
41 ms |
9828 KB |
Output is correct |
45 |
Correct |
123 ms |
12400 KB |
Output is correct |
46 |
Correct |
94 ms |
15932 KB |
Output is correct |
47 |
Correct |
155 ms |
19380 KB |
Output is correct |
48 |
Correct |
207 ms |
30756 KB |
Output is correct |
49 |
Correct |
233 ms |
29860 KB |
Output is correct |
50 |
Correct |
135 ms |
19360 KB |
Output is correct |
51 |
Correct |
115 ms |
19392 KB |
Output is correct |
52 |
Correct |
194 ms |
19500 KB |
Output is correct |
53 |
Correct |
239 ms |
18232 KB |
Output is correct |
54 |
Correct |
199 ms |
19148 KB |
Output is correct |
55 |
Correct |
7 ms |
1236 KB |
Output is correct |
56 |
Correct |
8 ms |
1340 KB |
Output is correct |
57 |
Correct |
66 ms |
10424 KB |
Output is correct |
58 |
Correct |
37 ms |
10492 KB |
Output is correct |
59 |
Correct |
79 ms |
16648 KB |
Output is correct |
60 |
Correct |
352 ms |
35084 KB |
Output is correct |
61 |
Correct |
132 ms |
19924 KB |
Output is correct |
62 |
Correct |
142 ms |
24428 KB |
Output is correct |
63 |
Correct |
235 ms |
24408 KB |
Output is correct |
64 |
Correct |
357 ms |
22840 KB |
Output is correct |
65 |
Correct |
193 ms |
20316 KB |
Output is correct |
66 |
Correct |
313 ms |
31800 KB |
Output is correct |
67 |
Correct |
85 ms |
25772 KB |
Output is correct |
68 |
Correct |
230 ms |
24620 KB |
Output is correct |
69 |
Correct |
213 ms |
24852 KB |
Output is correct |
70 |
Correct |
227 ms |
23828 KB |
Output is correct |