#include <bits/stdc++.h>
#include "race.h"
using namespace std;
const int MX = 1e6;
vector<vector<pair<int,int>>> g;
vector<int> sz, dead;
int dfs_sz(int v, int p){
sz[v] = 1;
for(auto [u, w]: g[v]) if(u != p && !dead[u]){
sz[v] += dfs_sz(u, v);
}
return sz[v];
}
int cent(int v, int p, int n){
for(auto [u, w]: g[v]) if(u != p && !dead[u] && sz[u] > n / 2){
return cent(u, v, n);
}
return v;
}
void dfs(int v, int p, int ws, vector<pair<int,int>> &ps, int ln){
if(ws > MX) return;
ps.emplace_back(ws, ln);
for(auto [u, w]: g[v]) if(u != p && !dead[u]){
dfs(u, v, ws + w, ps, ln + 1);
}
}
int dc(int v, int k){
int n = dfs_sz(v, -1);
int c = cent(v, -1, n);
dead[c] = 1;
vector<vector<pair<int,int>>> ps;
for(auto [u, w]: g[c]) if(!dead[u]){
ps.emplace_back();
dfs(u, c, w, ps.back(), 1);
}
int ans = 1e9;
map<int,int> mins;
mins[0] = 0;
for(auto chld: ps){
for(auto [x, ln]: chld){
if(k - x >= 0){
if(mins.find(k - x) != mins.end()) ans = min(ans, mins[k - x] + ln);
}
}
for(auto [x, ln]: chld){
if(mins.find(x) != mins.end()) mins[x] = min(mins[x], ln);
else mins[x] = ln;
}
}
for(auto [u, w]: g[c]) if(!dead[u]){
ans = min(ans, dc(u, k));
}
return ans;
}
int best_path(int n, int k, int h[][2], int l[]){
g.resize(n);
sz.resize(n);
dead.resize(n);
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]);
}
int ans = dc(0, k);
if(ans >= 1e9) ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 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 |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
2 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
182 ms |
9952 KB |
Output is correct |
20 |
Correct |
172 ms |
11452 KB |
Output is correct |
21 |
Correct |
169 ms |
11460 KB |
Output is correct |
22 |
Correct |
155 ms |
11392 KB |
Output is correct |
23 |
Correct |
228 ms |
11704 KB |
Output is correct |
24 |
Correct |
118 ms |
11116 KB |
Output is correct |
25 |
Correct |
150 ms |
14664 KB |
Output is correct |
26 |
Correct |
87 ms |
17328 KB |
Output is correct |
27 |
Correct |
238 ms |
21744 KB |
Output is correct |
28 |
Correct |
560 ms |
40388 KB |
Output is correct |
29 |
Correct |
578 ms |
39444 KB |
Output is correct |
30 |
Correct |
277 ms |
21776 KB |
Output is correct |
31 |
Correct |
232 ms |
21780 KB |
Output is correct |
32 |
Correct |
274 ms |
21916 KB |
Output is correct |
33 |
Correct |
404 ms |
22016 KB |
Output is correct |
34 |
Correct |
166 ms |
19020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 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 |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
2 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
2 ms |
468 KB |
Output is correct |
39 |
Correct |
182 ms |
9952 KB |
Output is correct |
40 |
Correct |
172 ms |
11452 KB |
Output is correct |
41 |
Correct |
169 ms |
11460 KB |
Output is correct |
42 |
Correct |
155 ms |
11392 KB |
Output is correct |
43 |
Correct |
228 ms |
11704 KB |
Output is correct |
44 |
Correct |
118 ms |
11116 KB |
Output is correct |
45 |
Correct |
150 ms |
14664 KB |
Output is correct |
46 |
Correct |
87 ms |
17328 KB |
Output is correct |
47 |
Correct |
238 ms |
21744 KB |
Output is correct |
48 |
Correct |
560 ms |
40388 KB |
Output is correct |
49 |
Correct |
578 ms |
39444 KB |
Output is correct |
50 |
Correct |
277 ms |
21776 KB |
Output is correct |
51 |
Correct |
232 ms |
21780 KB |
Output is correct |
52 |
Correct |
274 ms |
21916 KB |
Output is correct |
53 |
Correct |
404 ms |
22016 KB |
Output is correct |
54 |
Correct |
166 ms |
19020 KB |
Output is correct |
55 |
Correct |
18 ms |
2024 KB |
Output is correct |
56 |
Correct |
12 ms |
1364 KB |
Output is correct |
57 |
Correct |
119 ms |
11320 KB |
Output is correct |
58 |
Correct |
47 ms |
14960 KB |
Output is correct |
59 |
Correct |
248 ms |
24224 KB |
Output is correct |
60 |
Correct |
667 ms |
50852 KB |
Output is correct |
61 |
Correct |
270 ms |
23196 KB |
Output is correct |
62 |
Correct |
232 ms |
21736 KB |
Output is correct |
63 |
Correct |
267 ms |
21768 KB |
Output is correct |
64 |
Correct |
636 ms |
34160 KB |
Output is correct |
65 |
Correct |
178 ms |
20348 KB |
Output is correct |
66 |
Correct |
466 ms |
29588 KB |
Output is correct |
67 |
Correct |
119 ms |
30936 KB |
Output is correct |
68 |
Correct |
320 ms |
28916 KB |
Output is correct |
69 |
Correct |
313 ms |
29212 KB |
Output is correct |
70 |
Correct |
287 ms |
27864 KB |
Output is correct |