#include <bits/stdc++.h>
#include "race.h"
using namespace std;
int n , k;
vector<vector<pair<int , int>>> g;
vector<int> s;
vector<bool> removed;
vector<int> cnt;
int mdis =0;
int ans = INT_MAX;
int sz;
void get_subtree_size(int p){
removed[p] = true;
s[p] = 1;
for(auto [x , y]:g[p]){
if(!removed[x]){
get_subtree_size(x);
s[p] += s[x];
}
}
removed[p] = false;
}
int get_centroid(int p){
removed[p] = true;
int centroid = p;
for(auto [x , y]:g[p]){
if(!removed[x] && s[x] > sz / 2){
centroid = get_centroid(x);
break;
}
}
removed[p] = false;
return centroid;
}
void dfs1(int p , int dis , int citys)
{
if(k >= dis){
ans = min(cnt[k-dis]+citys , ans);
}else return;
mdis = max(mdis , dis);
removed[p] = true;
for(auto [x , y]:g[p]){
if(!removed[x]){
dfs1(x , dis+y , citys+1);
}
}
removed[p] = false;
}
void dfs2(int p , int dis , int citys)
{
if(dis <= k)cnt[dis] = min(cnt[dis] , citys);else return;
removed[p] = true;
for(auto [x , y]:g[p]){
if(!removed[x]){
dfs2(x , dis+y , citys+1);
}
}
removed[p] = false;
}
void solve(int p){
mdis = 0;
cnt[0] = 0;
get_subtree_size(p);
sz = s[p];
int centroid = get_centroid(p);
removed[centroid] = true;
for(auto [x , y]: g[centroid]){
if(!removed[x]){
dfs1(x, y , 1);
dfs2(x, y , 1);
}
}
for(int i = 1; i <= mdis; i++){
cnt[i] = INT_MAX-n;
}
for(auto [x , y]: g[centroid]){
if(!removed[x]){
solve(x);
}
}
}
int best_path(int N ,int K, int H[][2], int L[]){
n = N;
k = K;
g.resize(n);
s.resize(n);
removed.resize(n);
cnt.resize(k+1 , INT_MAX-n);
for(int i = 0; i < n-1; i++){
g[H[i][0]].push_back({H[i][1] , L[i]});
g[H[i][1]].push_back({H[i][0] , L[i]});
}
solve(0);
if(ans < n)return ans; else return -1;
}
Compilation message
race.cpp: In function 'void get_subtree_size(int)':
race.cpp:17:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
17 | for(auto [x , y]:g[p]){
| ^
race.cpp: In function 'int get_centroid(int)':
race.cpp:29:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
29 | for(auto [x , y]:g[p]){
| ^
race.cpp: In function 'void dfs1(int, int, int)':
race.cpp:46:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
46 | for(auto [x , y]:g[p]){
| ^
race.cpp: In function 'void dfs2(int, int, int)':
race.cpp:58:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
58 | for(auto [x , y]:g[p]){
| ^
race.cpp: In function 'void solve(int)':
race.cpp:73:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
73 | for(auto [x , y]: g[centroid]){
| ^
race.cpp:82:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
82 | for(auto [x , y]: g[centroid]){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2548 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2548 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
152 ms |
5984 KB |
Output is correct |
23 |
Correct |
106 ms |
5508 KB |
Output is correct |
24 |
Correct |
130 ms |
5724 KB |
Output is correct |
25 |
Correct |
21 ms |
5724 KB |
Output is correct |
26 |
Correct |
50 ms |
3872 KB |
Output is correct |
27 |
Correct |
2 ms |
5464 KB |
Output is correct |
28 |
Correct |
17 ms |
3160 KB |
Output is correct |
29 |
Correct |
18 ms |
3672 KB |
Output is correct |
30 |
Correct |
18 ms |
3932 KB |
Output is correct |
31 |
Correct |
20 ms |
4956 KB |
Output is correct |
32 |
Correct |
20 ms |
5352 KB |
Output is correct |
33 |
Correct |
25 ms |
5464 KB |
Output is correct |
34 |
Correct |
31 ms |
4696 KB |
Output is correct |
35 |
Correct |
33 ms |
5720 KB |
Output is correct |
36 |
Correct |
31 ms |
6228 KB |
Output is correct |
37 |
Correct |
6 ms |
5468 KB |
Output is correct |
38 |
Correct |
5 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2548 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
82 ms |
10900 KB |
Output is correct |
20 |
Correct |
81 ms |
11092 KB |
Output is correct |
21 |
Correct |
84 ms |
11100 KB |
Output is correct |
22 |
Correct |
78 ms |
11108 KB |
Output is correct |
23 |
Correct |
62 ms |
11356 KB |
Output is correct |
24 |
Correct |
50 ms |
11112 KB |
Output is correct |
25 |
Correct |
91 ms |
13648 KB |
Output is correct |
26 |
Correct |
73 ms |
16728 KB |
Output is correct |
27 |
Correct |
103 ms |
17748 KB |
Output is correct |
28 |
Correct |
173 ms |
28756 KB |
Output is correct |
29 |
Correct |
177 ms |
28036 KB |
Output is correct |
30 |
Correct |
101 ms |
17700 KB |
Output is correct |
31 |
Correct |
107 ms |
17884 KB |
Output is correct |
32 |
Correct |
145 ms |
17512 KB |
Output is correct |
33 |
Correct |
129 ms |
16464 KB |
Output is correct |
34 |
Correct |
112 ms |
16464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2548 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
152 ms |
5984 KB |
Output is correct |
23 |
Correct |
106 ms |
5508 KB |
Output is correct |
24 |
Correct |
130 ms |
5724 KB |
Output is correct |
25 |
Correct |
21 ms |
5724 KB |
Output is correct |
26 |
Correct |
50 ms |
3872 KB |
Output is correct |
27 |
Correct |
2 ms |
5464 KB |
Output is correct |
28 |
Correct |
17 ms |
3160 KB |
Output is correct |
29 |
Correct |
18 ms |
3672 KB |
Output is correct |
30 |
Correct |
18 ms |
3932 KB |
Output is correct |
31 |
Correct |
20 ms |
4956 KB |
Output is correct |
32 |
Correct |
20 ms |
5352 KB |
Output is correct |
33 |
Correct |
25 ms |
5464 KB |
Output is correct |
34 |
Correct |
31 ms |
4696 KB |
Output is correct |
35 |
Correct |
33 ms |
5720 KB |
Output is correct |
36 |
Correct |
31 ms |
6228 KB |
Output is correct |
37 |
Correct |
6 ms |
5468 KB |
Output is correct |
38 |
Correct |
5 ms |
4444 KB |
Output is correct |
39 |
Correct |
82 ms |
10900 KB |
Output is correct |
40 |
Correct |
81 ms |
11092 KB |
Output is correct |
41 |
Correct |
84 ms |
11100 KB |
Output is correct |
42 |
Correct |
78 ms |
11108 KB |
Output is correct |
43 |
Correct |
62 ms |
11356 KB |
Output is correct |
44 |
Correct |
50 ms |
11112 KB |
Output is correct |
45 |
Correct |
91 ms |
13648 KB |
Output is correct |
46 |
Correct |
73 ms |
16728 KB |
Output is correct |
47 |
Correct |
103 ms |
17748 KB |
Output is correct |
48 |
Correct |
173 ms |
28756 KB |
Output is correct |
49 |
Correct |
177 ms |
28036 KB |
Output is correct |
50 |
Correct |
101 ms |
17700 KB |
Output is correct |
51 |
Correct |
107 ms |
17884 KB |
Output is correct |
52 |
Correct |
145 ms |
17512 KB |
Output is correct |
53 |
Correct |
129 ms |
16464 KB |
Output is correct |
54 |
Correct |
112 ms |
16464 KB |
Output is correct |
55 |
Correct |
10 ms |
3160 KB |
Output is correct |
56 |
Correct |
9 ms |
3164 KB |
Output is correct |
57 |
Correct |
57 ms |
11356 KB |
Output is correct |
58 |
Correct |
28 ms |
11208 KB |
Output is correct |
59 |
Correct |
76 ms |
17500 KB |
Output is correct |
60 |
Correct |
262 ms |
35212 KB |
Output is correct |
61 |
Correct |
124 ms |
20684 KB |
Output is correct |
62 |
Correct |
127 ms |
24400 KB |
Output is correct |
63 |
Correct |
152 ms |
24532 KB |
Output is correct |
64 |
Correct |
869 ms |
23016 KB |
Output is correct |
65 |
Correct |
198 ms |
21580 KB |
Output is correct |
66 |
Correct |
542 ms |
33136 KB |
Output is correct |
67 |
Correct |
72 ms |
25284 KB |
Output is correct |
68 |
Correct |
382 ms |
25172 KB |
Output is correct |
69 |
Correct |
332 ms |
25424 KB |
Output is correct |
70 |
Correct |
275 ms |
24656 KB |
Output is correct |