#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;
}
Compilation message
race.cpp: In function 'int dfs_sz(int, int)':
race.cpp:10:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
10 | for(auto [u, w]: g[v]) if(u != p && !dead[u]){
| ^
race.cpp: In function 'int cent(int, int, int)':
race.cpp:17:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
17 | for(auto [u, w]: g[v]) if(u != p && !dead[u] && sz[u] > n / 2){
| ^
race.cpp: In function 'void dfs(int, int, int, std::vector<std::pair<int, int> >&, int)':
race.cpp:26:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
26 | for(auto [u, w]: g[v]) if(u != p && !dead[u]){
| ^
race.cpp: In function 'int dc(int, int)':
race.cpp:36:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
36 | for(auto [u, w]: g[c]) if(!dead[u]){
| ^
race.cpp:44:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
44 | for(auto [x, ln]: chld){
| ^
race.cpp:49:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
49 | for(auto [x, ln]: chld){
| ^
race.cpp:54:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
54 | for(auto [u, w]: g[c]) if(!dead[u]){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
308 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
304 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 |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
308 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
304 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 |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
412 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
444 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
452 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 |
2 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 |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
308 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
304 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 |
174 ms |
11384 KB |
Output is correct |
20 |
Correct |
170 ms |
11464 KB |
Output is correct |
21 |
Correct |
173 ms |
11456 KB |
Output is correct |
22 |
Correct |
152 ms |
11408 KB |
Output is correct |
23 |
Correct |
229 ms |
11704 KB |
Output is correct |
24 |
Correct |
128 ms |
10984 KB |
Output is correct |
25 |
Correct |
148 ms |
14572 KB |
Output is correct |
26 |
Correct |
87 ms |
17268 KB |
Output is correct |
27 |
Correct |
301 ms |
20344 KB |
Output is correct |
28 |
Correct |
592 ms |
38840 KB |
Output is correct |
29 |
Correct |
592 ms |
37816 KB |
Output is correct |
30 |
Correct |
289 ms |
20332 KB |
Output is correct |
31 |
Correct |
263 ms |
20408 KB |
Output is correct |
32 |
Correct |
327 ms |
20348 KB |
Output is correct |
33 |
Correct |
411 ms |
20656 KB |
Output is correct |
34 |
Correct |
147 ms |
16748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
308 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
304 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 |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
412 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
444 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
452 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 |
2 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 |
174 ms |
11384 KB |
Output is correct |
40 |
Correct |
170 ms |
11464 KB |
Output is correct |
41 |
Correct |
173 ms |
11456 KB |
Output is correct |
42 |
Correct |
152 ms |
11408 KB |
Output is correct |
43 |
Correct |
229 ms |
11704 KB |
Output is correct |
44 |
Correct |
128 ms |
10984 KB |
Output is correct |
45 |
Correct |
148 ms |
14572 KB |
Output is correct |
46 |
Correct |
87 ms |
17268 KB |
Output is correct |
47 |
Correct |
301 ms |
20344 KB |
Output is correct |
48 |
Correct |
592 ms |
38840 KB |
Output is correct |
49 |
Correct |
592 ms |
37816 KB |
Output is correct |
50 |
Correct |
289 ms |
20332 KB |
Output is correct |
51 |
Correct |
263 ms |
20408 KB |
Output is correct |
52 |
Correct |
327 ms |
20348 KB |
Output is correct |
53 |
Correct |
411 ms |
20656 KB |
Output is correct |
54 |
Correct |
147 ms |
16748 KB |
Output is correct |
55 |
Correct |
17 ms |
2020 KB |
Output is correct |
56 |
Correct |
11 ms |
1348 KB |
Output is correct |
57 |
Correct |
116 ms |
11404 KB |
Output is correct |
58 |
Correct |
52 ms |
15032 KB |
Output is correct |
59 |
Correct |
251 ms |
24216 KB |
Output is correct |
60 |
Correct |
670 ms |
49564 KB |
Output is correct |
61 |
Correct |
273 ms |
21580 KB |
Output is correct |
62 |
Correct |
231 ms |
20248 KB |
Output is correct |
63 |
Correct |
267 ms |
20300 KB |
Output is correct |
64 |
Correct |
657 ms |
32180 KB |
Output is correct |
65 |
Correct |
177 ms |
17996 KB |
Output is correct |
66 |
Correct |
447 ms |
27888 KB |
Output is correct |
67 |
Correct |
119 ms |
29000 KB |
Output is correct |
68 |
Correct |
302 ms |
26808 KB |
Output is correct |
69 |
Correct |
314 ms |
27124 KB |
Output is correct |
70 |
Correct |
288 ms |
25996 KB |
Output is correct |