#include <bits/stdc++.h>
#include "race.h"
using namespace std;
const int N = 1e6 + 5;
int n, cnt, k;
int dd[N], sz[N], d[N], f[N];
vector <pair<int, int>> ad[N], val;
int ans;
void dfs(int u, int par) {
cnt++;
sz[u] = 1;
for(auto v : ad[u]) {
if(v.first != par && !dd[v.first]) dfs(v.first, u), sz[u] += sz[v.first];
}
}
int ftr(int u, int par) {
for(auto &v : ad[u]) if(v.first != par && !dd[v.first] && sz[v.first] > cnt / 2) return ftr(v.first, u);
return u;
}
void bfs(int u, int par, int len, int cnt) {
if(len > k) return;
val.emplace_back(len, cnt);
for(auto &v : ad[u]) {
if(v.first != par && !dd[v.first] && v.second + len <= k) {
bfs(v.first, u, v.second + len, cnt + 1);
}
}
}
void ctr(int u, int par) {
cnt = 0;
dfs(u, 0);
int c = ftr(u, u);
dd[c] = 1;
f[0] = 0;
vector<int> del;
for(auto v : ad[c]) {
if(!dd[v.first]) {
bfs(v.first, c, v.second, 1);
for(auto &i : val) if(k >= i.first && f[k - i.first] != -1) ans = min(ans, i.second + f[k - i.first]);
for(auto &i : val) f[i.first] = (f[i.first] < 0) ? i.second : min(i.second, f[i.first]), del.emplace_back(i.first);
val.clear();
}
}
for(auto &v : del) f[v] = -1;
for(auto &v : ad[c]) if(!dd[v.first]) ctr(v.first, c);
}
int best_path(int _n, int _k, int h[][2], int l[]) {
n = _n, k = _k;
for(int i = 0; i < n - 1; ++i) {
int u = h[i][0], v = h[i][1], c = l[i]; u++; v++;
ad[u].emplace_back(v, c);
ad[v].emplace_back(u, c);
}
memset(f, -1, sizeof(f));
ans = INT_MAX;
ctr(1, 0);
return (ans == INT_MAX) ? -1 : ans;
}
/*
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> k;
static int h[N][2], le[N];
for(int i = 0; i < n - 1; ++i) cin >> h[i][0] >> h[i][1] >> le[i];
cout << best_path(n, k, h, le);
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
27776 KB |
Output is correct |
2 |
Correct |
22 ms |
27776 KB |
Output is correct |
3 |
Correct |
19 ms |
27776 KB |
Output is correct |
4 |
Correct |
20 ms |
27776 KB |
Output is correct |
5 |
Correct |
19 ms |
27776 KB |
Output is correct |
6 |
Correct |
20 ms |
27776 KB |
Output is correct |
7 |
Correct |
20 ms |
27776 KB |
Output is correct |
8 |
Correct |
19 ms |
27776 KB |
Output is correct |
9 |
Correct |
20 ms |
27776 KB |
Output is correct |
10 |
Correct |
19 ms |
27776 KB |
Output is correct |
11 |
Correct |
19 ms |
27776 KB |
Output is correct |
12 |
Correct |
19 ms |
27776 KB |
Output is correct |
13 |
Correct |
20 ms |
27776 KB |
Output is correct |
14 |
Correct |
19 ms |
27776 KB |
Output is correct |
15 |
Correct |
19 ms |
27776 KB |
Output is correct |
16 |
Correct |
19 ms |
27776 KB |
Output is correct |
17 |
Correct |
19 ms |
27776 KB |
Output is correct |
18 |
Correct |
19 ms |
27776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
27776 KB |
Output is correct |
2 |
Correct |
22 ms |
27776 KB |
Output is correct |
3 |
Correct |
19 ms |
27776 KB |
Output is correct |
4 |
Correct |
20 ms |
27776 KB |
Output is correct |
5 |
Correct |
19 ms |
27776 KB |
Output is correct |
6 |
Correct |
20 ms |
27776 KB |
Output is correct |
7 |
Correct |
20 ms |
27776 KB |
Output is correct |
8 |
Correct |
19 ms |
27776 KB |
Output is correct |
9 |
Correct |
20 ms |
27776 KB |
Output is correct |
10 |
Correct |
19 ms |
27776 KB |
Output is correct |
11 |
Correct |
19 ms |
27776 KB |
Output is correct |
12 |
Correct |
19 ms |
27776 KB |
Output is correct |
13 |
Correct |
20 ms |
27776 KB |
Output is correct |
14 |
Correct |
19 ms |
27776 KB |
Output is correct |
15 |
Correct |
19 ms |
27776 KB |
Output is correct |
16 |
Correct |
19 ms |
27776 KB |
Output is correct |
17 |
Correct |
19 ms |
27776 KB |
Output is correct |
18 |
Correct |
19 ms |
27776 KB |
Output is correct |
19 |
Correct |
19 ms |
27776 KB |
Output is correct |
20 |
Correct |
19 ms |
27776 KB |
Output is correct |
21 |
Correct |
20 ms |
27904 KB |
Output is correct |
22 |
Correct |
20 ms |
27776 KB |
Output is correct |
23 |
Correct |
21 ms |
27776 KB |
Output is correct |
24 |
Correct |
21 ms |
27776 KB |
Output is correct |
25 |
Correct |
20 ms |
27776 KB |
Output is correct |
26 |
Correct |
20 ms |
27776 KB |
Output is correct |
27 |
Correct |
20 ms |
27776 KB |
Output is correct |
28 |
Correct |
20 ms |
27776 KB |
Output is correct |
29 |
Correct |
20 ms |
27776 KB |
Output is correct |
30 |
Correct |
21 ms |
27776 KB |
Output is correct |
31 |
Correct |
20 ms |
27904 KB |
Output is correct |
32 |
Correct |
21 ms |
27776 KB |
Output is correct |
33 |
Correct |
20 ms |
27776 KB |
Output is correct |
34 |
Correct |
21 ms |
27776 KB |
Output is correct |
35 |
Correct |
20 ms |
27776 KB |
Output is correct |
36 |
Correct |
20 ms |
27776 KB |
Output is correct |
37 |
Correct |
20 ms |
27776 KB |
Output is correct |
38 |
Correct |
20 ms |
27904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
27776 KB |
Output is correct |
2 |
Correct |
22 ms |
27776 KB |
Output is correct |
3 |
Correct |
19 ms |
27776 KB |
Output is correct |
4 |
Correct |
20 ms |
27776 KB |
Output is correct |
5 |
Correct |
19 ms |
27776 KB |
Output is correct |
6 |
Correct |
20 ms |
27776 KB |
Output is correct |
7 |
Correct |
20 ms |
27776 KB |
Output is correct |
8 |
Correct |
19 ms |
27776 KB |
Output is correct |
9 |
Correct |
20 ms |
27776 KB |
Output is correct |
10 |
Correct |
19 ms |
27776 KB |
Output is correct |
11 |
Correct |
19 ms |
27776 KB |
Output is correct |
12 |
Correct |
19 ms |
27776 KB |
Output is correct |
13 |
Correct |
20 ms |
27776 KB |
Output is correct |
14 |
Correct |
19 ms |
27776 KB |
Output is correct |
15 |
Correct |
19 ms |
27776 KB |
Output is correct |
16 |
Correct |
19 ms |
27776 KB |
Output is correct |
17 |
Correct |
19 ms |
27776 KB |
Output is correct |
18 |
Correct |
19 ms |
27776 KB |
Output is correct |
19 |
Correct |
155 ms |
33656 KB |
Output is correct |
20 |
Correct |
177 ms |
33528 KB |
Output is correct |
21 |
Correct |
168 ms |
33944 KB |
Output is correct |
22 |
Correct |
154 ms |
34544 KB |
Output is correct |
23 |
Correct |
112 ms |
33656 KB |
Output is correct |
24 |
Correct |
83 ms |
33528 KB |
Output is correct |
25 |
Correct |
149 ms |
36216 KB |
Output is correct |
26 |
Correct |
119 ms |
40484 KB |
Output is correct |
27 |
Correct |
215 ms |
39288 KB |
Output is correct |
28 |
Correct |
334 ms |
50424 KB |
Output is correct |
29 |
Correct |
338 ms |
49528 KB |
Output is correct |
30 |
Correct |
216 ms |
39288 KB |
Output is correct |
31 |
Correct |
223 ms |
39288 KB |
Output is correct |
32 |
Correct |
265 ms |
39248 KB |
Output is correct |
33 |
Correct |
281 ms |
38136 KB |
Output is correct |
34 |
Correct |
226 ms |
38008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
27776 KB |
Output is correct |
2 |
Correct |
22 ms |
27776 KB |
Output is correct |
3 |
Correct |
19 ms |
27776 KB |
Output is correct |
4 |
Correct |
20 ms |
27776 KB |
Output is correct |
5 |
Correct |
19 ms |
27776 KB |
Output is correct |
6 |
Correct |
20 ms |
27776 KB |
Output is correct |
7 |
Correct |
20 ms |
27776 KB |
Output is correct |
8 |
Correct |
19 ms |
27776 KB |
Output is correct |
9 |
Correct |
20 ms |
27776 KB |
Output is correct |
10 |
Correct |
19 ms |
27776 KB |
Output is correct |
11 |
Correct |
19 ms |
27776 KB |
Output is correct |
12 |
Correct |
19 ms |
27776 KB |
Output is correct |
13 |
Correct |
20 ms |
27776 KB |
Output is correct |
14 |
Correct |
19 ms |
27776 KB |
Output is correct |
15 |
Correct |
19 ms |
27776 KB |
Output is correct |
16 |
Correct |
19 ms |
27776 KB |
Output is correct |
17 |
Correct |
19 ms |
27776 KB |
Output is correct |
18 |
Correct |
19 ms |
27776 KB |
Output is correct |
19 |
Correct |
19 ms |
27776 KB |
Output is correct |
20 |
Correct |
19 ms |
27776 KB |
Output is correct |
21 |
Correct |
20 ms |
27904 KB |
Output is correct |
22 |
Correct |
20 ms |
27776 KB |
Output is correct |
23 |
Correct |
21 ms |
27776 KB |
Output is correct |
24 |
Correct |
21 ms |
27776 KB |
Output is correct |
25 |
Correct |
20 ms |
27776 KB |
Output is correct |
26 |
Correct |
20 ms |
27776 KB |
Output is correct |
27 |
Correct |
20 ms |
27776 KB |
Output is correct |
28 |
Correct |
20 ms |
27776 KB |
Output is correct |
29 |
Correct |
20 ms |
27776 KB |
Output is correct |
30 |
Correct |
21 ms |
27776 KB |
Output is correct |
31 |
Correct |
20 ms |
27904 KB |
Output is correct |
32 |
Correct |
21 ms |
27776 KB |
Output is correct |
33 |
Correct |
20 ms |
27776 KB |
Output is correct |
34 |
Correct |
21 ms |
27776 KB |
Output is correct |
35 |
Correct |
20 ms |
27776 KB |
Output is correct |
36 |
Correct |
20 ms |
27776 KB |
Output is correct |
37 |
Correct |
20 ms |
27776 KB |
Output is correct |
38 |
Correct |
20 ms |
27904 KB |
Output is correct |
39 |
Correct |
155 ms |
33656 KB |
Output is correct |
40 |
Correct |
177 ms |
33528 KB |
Output is correct |
41 |
Correct |
168 ms |
33944 KB |
Output is correct |
42 |
Correct |
154 ms |
34544 KB |
Output is correct |
43 |
Correct |
112 ms |
33656 KB |
Output is correct |
44 |
Correct |
83 ms |
33528 KB |
Output is correct |
45 |
Correct |
149 ms |
36216 KB |
Output is correct |
46 |
Correct |
119 ms |
40484 KB |
Output is correct |
47 |
Correct |
215 ms |
39288 KB |
Output is correct |
48 |
Correct |
334 ms |
50424 KB |
Output is correct |
49 |
Correct |
338 ms |
49528 KB |
Output is correct |
50 |
Correct |
216 ms |
39288 KB |
Output is correct |
51 |
Correct |
223 ms |
39288 KB |
Output is correct |
52 |
Correct |
265 ms |
39248 KB |
Output is correct |
53 |
Correct |
281 ms |
38136 KB |
Output is correct |
54 |
Correct |
226 ms |
38008 KB |
Output is correct |
55 |
Correct |
28 ms |
28544 KB |
Output is correct |
56 |
Correct |
28 ms |
28544 KB |
Output is correct |
57 |
Correct |
111 ms |
35828 KB |
Output is correct |
58 |
Correct |
57 ms |
35564 KB |
Output is correct |
59 |
Correct |
123 ms |
41900 KB |
Output is correct |
60 |
Correct |
491 ms |
55268 KB |
Output is correct |
61 |
Correct |
249 ms |
42620 KB |
Output is correct |
62 |
Correct |
244 ms |
43128 KB |
Output is correct |
63 |
Correct |
308 ms |
43260 KB |
Output is correct |
64 |
Correct |
501 ms |
44908 KB |
Output is correct |
65 |
Correct |
283 ms |
43000 KB |
Output is correct |
66 |
Correct |
427 ms |
50936 KB |
Output is correct |
67 |
Correct |
147 ms |
44644 KB |
Output is correct |
68 |
Correct |
303 ms |
44016 KB |
Output is correct |
69 |
Correct |
308 ms |
44172 KB |
Output is correct |
70 |
Correct |
279 ms |
43632 KB |
Output is correct |