#include<race.h>
#include<bits/stdc++.h>
using namespace std;
vector<int>subtree;
vector<bool>removed;
vector<vector<pair<int,int>>>v;
int get_size(int node,int parent = -1){
subtree[node]=1;
for(auto [x,w]:v[node]){
if(!removed[x] && x!=parent){
subtree[node]+=get_size(x,node);
}
}
return subtree[node];
}
int get_centroid(int node, int n, int parent = -1){
for(auto [x,w]:v[node]){
if(!removed[x] && x!=parent && subtree[x]*2>n)
return get_centroid(x,n,node);
}
return node;
}
int n,k;
int ans = 1e9;
void get_cnt(int node, map<int,int> &cnt, int dis, bool filling, int depth = 1, int parent = -1){
if(dis>k) return;
if(filling) cnt[dis]=dis == k ? depth : (cnt[dis]==0?depth:min(depth,cnt[dis]));
else ans = min(ans,dis==k ? depth : (cnt[k-dis]==0?(int)1e9:cnt[k-dis])+depth);
for(auto [x,w]:v[node]){
if(x!=parent && !removed[x]){
get_cnt(x,cnt,dis+w,filling,depth+1,node);
}
}
}
void build_centroid(int node = 0){
int centroid = get_centroid(node,get_size(node));
removed[centroid]=true;
map<int,int>cnt;
for(auto [x,w]:v[centroid]){
if(!removed[x]){
get_cnt(x,cnt,w,false);
get_cnt(x,cnt,w,true);
}
}
for(auto [x,w]:v[centroid]){
if(!removed[x]){
build_centroid(x);
}
}
}
int best_path(int N, int K, int H[][2], int L[])
{
n=N;
k=K;
subtree.resize(n);
v.resize(n);
removed.assign(n,false);
for(int i=0;i<n-1;i++){
v[H[i][0]].push_back({H[i][1],L[i]});
v[H[i][1]].push_back({H[i][0],L[i]});
}
build_centroid();
return ans == (int)1e9 ? -1 : ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
0 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 |
0 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 |
0 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 |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
0 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 |
0 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 |
0 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 |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
2 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
2 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2504 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
2 ms |
2672 KB |
Output is correct |
29 |
Correct |
2 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2652 KB |
Output is correct |
31 |
Correct |
3 ms |
2652 KB |
Output is correct |
32 |
Correct |
2 ms |
2652 KB |
Output is correct |
33 |
Correct |
3 ms |
2652 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
2 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
2 ms |
2652 KB |
Output is correct |
38 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
0 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 |
0 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 |
0 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 |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
159 ms |
11096 KB |
Output is correct |
20 |
Correct |
148 ms |
12116 KB |
Output is correct |
21 |
Correct |
162 ms |
11860 KB |
Output is correct |
22 |
Correct |
151 ms |
12372 KB |
Output is correct |
23 |
Correct |
77 ms |
12628 KB |
Output is correct |
24 |
Correct |
58 ms |
12584 KB |
Output is correct |
25 |
Correct |
109 ms |
15696 KB |
Output is correct |
26 |
Correct |
87 ms |
19188 KB |
Output is correct |
27 |
Correct |
168 ms |
19260 KB |
Output is correct |
28 |
Correct |
170 ms |
33360 KB |
Output is correct |
29 |
Correct |
166 ms |
32608 KB |
Output is correct |
30 |
Correct |
149 ms |
19804 KB |
Output is correct |
31 |
Correct |
163 ms |
19796 KB |
Output is correct |
32 |
Correct |
134 ms |
19792 KB |
Output is correct |
33 |
Correct |
191 ms |
18520 KB |
Output is correct |
34 |
Correct |
96 ms |
18768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
0 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 |
0 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 |
0 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 |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
2 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
2 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2504 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
2 ms |
2672 KB |
Output is correct |
29 |
Correct |
2 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2652 KB |
Output is correct |
31 |
Correct |
3 ms |
2652 KB |
Output is correct |
32 |
Correct |
2 ms |
2652 KB |
Output is correct |
33 |
Correct |
3 ms |
2652 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
2 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
2 ms |
2652 KB |
Output is correct |
38 |
Correct |
2 ms |
2652 KB |
Output is correct |
39 |
Correct |
159 ms |
11096 KB |
Output is correct |
40 |
Correct |
148 ms |
12116 KB |
Output is correct |
41 |
Correct |
162 ms |
11860 KB |
Output is correct |
42 |
Correct |
151 ms |
12372 KB |
Output is correct |
43 |
Correct |
77 ms |
12628 KB |
Output is correct |
44 |
Correct |
58 ms |
12584 KB |
Output is correct |
45 |
Correct |
109 ms |
15696 KB |
Output is correct |
46 |
Correct |
87 ms |
19188 KB |
Output is correct |
47 |
Correct |
168 ms |
19260 KB |
Output is correct |
48 |
Correct |
170 ms |
33360 KB |
Output is correct |
49 |
Correct |
166 ms |
32608 KB |
Output is correct |
50 |
Correct |
149 ms |
19804 KB |
Output is correct |
51 |
Correct |
163 ms |
19796 KB |
Output is correct |
52 |
Correct |
134 ms |
19792 KB |
Output is correct |
53 |
Correct |
191 ms |
18520 KB |
Output is correct |
54 |
Correct |
96 ms |
18768 KB |
Output is correct |
55 |
Correct |
19 ms |
3676 KB |
Output is correct |
56 |
Correct |
12 ms |
3164 KB |
Output is correct |
57 |
Correct |
124 ms |
11588 KB |
Output is correct |
58 |
Correct |
29 ms |
12220 KB |
Output is correct |
59 |
Correct |
316 ms |
32852 KB |
Output is correct |
60 |
Correct |
815 ms |
66716 KB |
Output is correct |
61 |
Correct |
213 ms |
19404 KB |
Output is correct |
62 |
Correct |
235 ms |
20048 KB |
Output is correct |
63 |
Correct |
267 ms |
20240 KB |
Output is correct |
64 |
Correct |
971 ms |
40592 KB |
Output is correct |
65 |
Correct |
95 ms |
20308 KB |
Output is correct |
66 |
Correct |
527 ms |
31012 KB |
Output is correct |
67 |
Correct |
81 ms |
21128 KB |
Output is correct |
68 |
Correct |
328 ms |
33616 KB |
Output is correct |
69 |
Correct |
361 ms |
31824 KB |
Output is correct |
70 |
Correct |
296 ms |
32848 KB |
Output is correct |