#include "race.h"
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
int n,ans=1e9,k,tam[200005],vis[200005],min_longi[1000005];
vector<pair<int,int> > v[200005];
void tamanio_tree(int u,int p){
tam[u]=1;
for(auto [node,w]:v[u]){
if(vis[node]==0 && node!=p){
tamanio_tree(node,u);
tam[u]+=tam[node];
}
}
}
int centroid_descomposition(int u,int p,int want){
for(auto [node,w]:v[u]){
if(node!=p && vis[node]==0){
if(tam[node]>want)return centroid_descomposition(node,u,want);
}
}
return u;
}
void dfs(int u,int p,int type,int depth,int lv){
if(depth>k)return;
if(type==1){
ans=min(ans,lv+min_longi[k-depth]);
}else if(type==0){
min_longi[depth]=min(min_longi[depth],lv);
}else{
min_longi[depth]=1e9,min_longi[k-depth]=1e9;
}
for(auto [node,w]:v[u]){
if(node!=p && vis[node]==0){
dfs(node,u,type,depth+w,lv+1);
}
}
}
void all_trees(int u){
tamanio_tree(u,u);
int cen=centroid_descomposition(u,u,tam[u]/2);
vis[cen]=1;
for(auto [node,w]:v[cen]){
if(vis[node]==0){
dfs(node,node,2,w,1);
}
}
min_longi[0]=0;
for(auto [node,w]:v[cen]){
if(vis[node]==0){
dfs(node,node,1,w,1);
dfs(node,node,0,w,1);
}
}
for(auto [node,w]:v[cen]){
if(vis[node]==0){
all_trees(node);
}
}
}
int best_path(int N, int K, int H[][2], int L[]){
n=N,k=K;
for(int i=0;i<n-1;i++){
v[H[i][0]+1].pb({H[i][1]+1,L[i]});
v[H[i][1]+1].pb({H[i][0]+1,L[i]});
}
for(int i=1;i<=n;i++){
vis[i]=0;
}
for(int i=1;i<=1000000;i++){
min_longi[i]=1e9;
}
all_trees(1);
return (ans == 1e9) ? -1 : ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8916 KB |
Output is correct |
2 |
Correct |
5 ms |
8916 KB |
Output is correct |
3 |
Correct |
7 ms |
8916 KB |
Output is correct |
4 |
Correct |
5 ms |
8916 KB |
Output is correct |
5 |
Correct |
5 ms |
8916 KB |
Output is correct |
6 |
Correct |
6 ms |
8916 KB |
Output is correct |
7 |
Correct |
5 ms |
8916 KB |
Output is correct |
8 |
Correct |
5 ms |
8916 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
5 ms |
8916 KB |
Output is correct |
11 |
Correct |
5 ms |
8916 KB |
Output is correct |
12 |
Correct |
5 ms |
8916 KB |
Output is correct |
13 |
Correct |
4 ms |
8916 KB |
Output is correct |
14 |
Correct |
5 ms |
8900 KB |
Output is correct |
15 |
Correct |
5 ms |
8916 KB |
Output is correct |
16 |
Correct |
7 ms |
8916 KB |
Output is correct |
17 |
Correct |
6 ms |
8852 KB |
Output is correct |
18 |
Correct |
5 ms |
8828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8916 KB |
Output is correct |
2 |
Correct |
5 ms |
8916 KB |
Output is correct |
3 |
Correct |
7 ms |
8916 KB |
Output is correct |
4 |
Correct |
5 ms |
8916 KB |
Output is correct |
5 |
Correct |
5 ms |
8916 KB |
Output is correct |
6 |
Correct |
6 ms |
8916 KB |
Output is correct |
7 |
Correct |
5 ms |
8916 KB |
Output is correct |
8 |
Correct |
5 ms |
8916 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
5 ms |
8916 KB |
Output is correct |
11 |
Correct |
5 ms |
8916 KB |
Output is correct |
12 |
Correct |
5 ms |
8916 KB |
Output is correct |
13 |
Correct |
4 ms |
8916 KB |
Output is correct |
14 |
Correct |
5 ms |
8900 KB |
Output is correct |
15 |
Correct |
5 ms |
8916 KB |
Output is correct |
16 |
Correct |
7 ms |
8916 KB |
Output is correct |
17 |
Correct |
6 ms |
8852 KB |
Output is correct |
18 |
Correct |
5 ms |
8828 KB |
Output is correct |
19 |
Correct |
5 ms |
8916 KB |
Output is correct |
20 |
Correct |
5 ms |
8860 KB |
Output is correct |
21 |
Correct |
6 ms |
9004 KB |
Output is correct |
22 |
Correct |
6 ms |
8928 KB |
Output is correct |
23 |
Correct |
6 ms |
9004 KB |
Output is correct |
24 |
Correct |
6 ms |
8940 KB |
Output is correct |
25 |
Correct |
6 ms |
8928 KB |
Output is correct |
26 |
Correct |
6 ms |
9008 KB |
Output is correct |
27 |
Correct |
6 ms |
8928 KB |
Output is correct |
28 |
Correct |
6 ms |
8932 KB |
Output is correct |
29 |
Correct |
7 ms |
8996 KB |
Output is correct |
30 |
Correct |
6 ms |
8916 KB |
Output is correct |
31 |
Correct |
6 ms |
8996 KB |
Output is correct |
32 |
Correct |
6 ms |
8916 KB |
Output is correct |
33 |
Correct |
6 ms |
8916 KB |
Output is correct |
34 |
Correct |
6 ms |
8916 KB |
Output is correct |
35 |
Correct |
5 ms |
8916 KB |
Output is correct |
36 |
Correct |
5 ms |
8916 KB |
Output is correct |
37 |
Correct |
5 ms |
8916 KB |
Output is correct |
38 |
Correct |
6 ms |
9044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8916 KB |
Output is correct |
2 |
Correct |
5 ms |
8916 KB |
Output is correct |
3 |
Correct |
7 ms |
8916 KB |
Output is correct |
4 |
Correct |
5 ms |
8916 KB |
Output is correct |
5 |
Correct |
5 ms |
8916 KB |
Output is correct |
6 |
Correct |
6 ms |
8916 KB |
Output is correct |
7 |
Correct |
5 ms |
8916 KB |
Output is correct |
8 |
Correct |
5 ms |
8916 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
5 ms |
8916 KB |
Output is correct |
11 |
Correct |
5 ms |
8916 KB |
Output is correct |
12 |
Correct |
5 ms |
8916 KB |
Output is correct |
13 |
Correct |
4 ms |
8916 KB |
Output is correct |
14 |
Correct |
5 ms |
8900 KB |
Output is correct |
15 |
Correct |
5 ms |
8916 KB |
Output is correct |
16 |
Correct |
7 ms |
8916 KB |
Output is correct |
17 |
Correct |
6 ms |
8852 KB |
Output is correct |
18 |
Correct |
5 ms |
8828 KB |
Output is correct |
19 |
Correct |
155 ms |
16000 KB |
Output is correct |
20 |
Correct |
142 ms |
15888 KB |
Output is correct |
21 |
Correct |
158 ms |
15904 KB |
Output is correct |
22 |
Correct |
161 ms |
15992 KB |
Output is correct |
23 |
Correct |
80 ms |
16344 KB |
Output is correct |
24 |
Correct |
57 ms |
16100 KB |
Output is correct |
25 |
Correct |
130 ms |
19472 KB |
Output is correct |
26 |
Correct |
137 ms |
23180 KB |
Output is correct |
27 |
Correct |
178 ms |
23188 KB |
Output is correct |
28 |
Correct |
290 ms |
37840 KB |
Output is correct |
29 |
Correct |
304 ms |
36624 KB |
Output is correct |
30 |
Correct |
178 ms |
23172 KB |
Output is correct |
31 |
Correct |
166 ms |
23368 KB |
Output is correct |
32 |
Correct |
215 ms |
23412 KB |
Output is correct |
33 |
Correct |
247 ms |
22160 KB |
Output is correct |
34 |
Correct |
214 ms |
23116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8916 KB |
Output is correct |
2 |
Correct |
5 ms |
8916 KB |
Output is correct |
3 |
Correct |
7 ms |
8916 KB |
Output is correct |
4 |
Correct |
5 ms |
8916 KB |
Output is correct |
5 |
Correct |
5 ms |
8916 KB |
Output is correct |
6 |
Correct |
6 ms |
8916 KB |
Output is correct |
7 |
Correct |
5 ms |
8916 KB |
Output is correct |
8 |
Correct |
5 ms |
8916 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
5 ms |
8916 KB |
Output is correct |
11 |
Correct |
5 ms |
8916 KB |
Output is correct |
12 |
Correct |
5 ms |
8916 KB |
Output is correct |
13 |
Correct |
4 ms |
8916 KB |
Output is correct |
14 |
Correct |
5 ms |
8900 KB |
Output is correct |
15 |
Correct |
5 ms |
8916 KB |
Output is correct |
16 |
Correct |
7 ms |
8916 KB |
Output is correct |
17 |
Correct |
6 ms |
8852 KB |
Output is correct |
18 |
Correct |
5 ms |
8828 KB |
Output is correct |
19 |
Correct |
5 ms |
8916 KB |
Output is correct |
20 |
Correct |
5 ms |
8860 KB |
Output is correct |
21 |
Correct |
6 ms |
9004 KB |
Output is correct |
22 |
Correct |
6 ms |
8928 KB |
Output is correct |
23 |
Correct |
6 ms |
9004 KB |
Output is correct |
24 |
Correct |
6 ms |
8940 KB |
Output is correct |
25 |
Correct |
6 ms |
8928 KB |
Output is correct |
26 |
Correct |
6 ms |
9008 KB |
Output is correct |
27 |
Correct |
6 ms |
8928 KB |
Output is correct |
28 |
Correct |
6 ms |
8932 KB |
Output is correct |
29 |
Correct |
7 ms |
8996 KB |
Output is correct |
30 |
Correct |
6 ms |
8916 KB |
Output is correct |
31 |
Correct |
6 ms |
8996 KB |
Output is correct |
32 |
Correct |
6 ms |
8916 KB |
Output is correct |
33 |
Correct |
6 ms |
8916 KB |
Output is correct |
34 |
Correct |
6 ms |
8916 KB |
Output is correct |
35 |
Correct |
5 ms |
8916 KB |
Output is correct |
36 |
Correct |
5 ms |
8916 KB |
Output is correct |
37 |
Correct |
5 ms |
8916 KB |
Output is correct |
38 |
Correct |
6 ms |
9044 KB |
Output is correct |
39 |
Correct |
155 ms |
16000 KB |
Output is correct |
40 |
Correct |
142 ms |
15888 KB |
Output is correct |
41 |
Correct |
158 ms |
15904 KB |
Output is correct |
42 |
Correct |
161 ms |
15992 KB |
Output is correct |
43 |
Correct |
80 ms |
16344 KB |
Output is correct |
44 |
Correct |
57 ms |
16100 KB |
Output is correct |
45 |
Correct |
130 ms |
19472 KB |
Output is correct |
46 |
Correct |
137 ms |
23180 KB |
Output is correct |
47 |
Correct |
178 ms |
23188 KB |
Output is correct |
48 |
Correct |
290 ms |
37840 KB |
Output is correct |
49 |
Correct |
304 ms |
36624 KB |
Output is correct |
50 |
Correct |
178 ms |
23172 KB |
Output is correct |
51 |
Correct |
166 ms |
23368 KB |
Output is correct |
52 |
Correct |
215 ms |
23412 KB |
Output is correct |
53 |
Correct |
247 ms |
22160 KB |
Output is correct |
54 |
Correct |
214 ms |
23116 KB |
Output is correct |
55 |
Correct |
13 ms |
9636 KB |
Output is correct |
56 |
Correct |
19 ms |
9508 KB |
Output is correct |
57 |
Correct |
93 ms |
16124 KB |
Output is correct |
58 |
Correct |
39 ms |
15780 KB |
Output is correct |
59 |
Correct |
101 ms |
23064 KB |
Output is correct |
60 |
Correct |
606 ms |
36872 KB |
Output is correct |
61 |
Correct |
231 ms |
23512 KB |
Output is correct |
62 |
Correct |
334 ms |
23360 KB |
Output is correct |
63 |
Correct |
379 ms |
23392 KB |
Output is correct |
64 |
Correct |
570 ms |
23608 KB |
Output is correct |
65 |
Correct |
246 ms |
24200 KB |
Output is correct |
66 |
Correct |
414 ms |
34180 KB |
Output is correct |
67 |
Correct |
127 ms |
24016 KB |
Output is correct |
68 |
Correct |
290 ms |
24112 KB |
Output is correct |
69 |
Correct |
318 ms |
24312 KB |
Output is correct |
70 |
Correct |
252 ms |
23540 KB |
Output is correct |