#include <bits/stdc++.h>
using namespace std;
#define pb emplace_back
const int mxN = 2e6+10;
const int INF = (int)1e9;
vector<pair<int,int>> adj[mxN];
int sz[mxN],N,K,ans=INF;
bool ok[mxN];
vector<int> mnDist((int)1e6+50,INF);
void dfs1(int node,int par){
sz[node]=0;
for(auto it:adj[node]){
if(it.first==par||ok[it.first]) continue;
dfs1(it.first,node);
sz[node]+=sz[it.first];
}
sz[node]++;
}
int find(int node,int par){
for(auto it:adj[node]){
if(it.first==par||ok[it.first]) continue;
if(sz[it.first]>=((N+1)/2)){
return find(it.first,node);
}
}
return node;
}
void dfs2(int src,int par,int dist,int h,bool f){
if(dist>K) return;
if(f){
mnDist[dist]=min(mnDist[dist],h);
}else{
ans=min(ans,mnDist[K-dist]+h);
}
for(auto it:adj[src]){
if(!ok[it.first]&&it.first!=par){
dfs2(it.first,src,dist+it.second,h+1,f);
}
}
}
void res(int u,int p,int dist){
if(dist>K) return;
mnDist[dist]=INF;
for(auto it:adj[u]){
if(it.first!=p&&!ok[it.first]){
res(it.first,u,dist+it.second);
}
}
}
void decompose(int u){
dfs1(u,-1);
int cen=find(u,-1);
mnDist[0]=0;
for(auto it:adj[u]){
if(!ok[it.first]){
dfs2(it.first,u,it.second,1,0);
dfs2(it.first,u,it.second,1,1);
}
}
res(u,-1,0);
ok[u]=1;
for(auto it:adj[u]){
if(!ok[it.first]){
decompose(it.first);
}
}
}
int best_path(int n,int k,int h[][2],int l[]){
for(int i=0;i<n-1;++i){
adj[h[i][0]].pb(h[i][1],l[i]);
adj[h[i][1]].pb(h[i][0],l[i]);
}
N=n,K=k;
decompose(0);
return ans>=INF?-1:ans;
}
Compilation message
race.cpp: In function 'void decompose(int)':
race.cpp:60:9: warning: unused variable 'cen' [-Wunused-variable]
60 | int cen=find(u,-1);
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
51292 KB |
Output is correct |
2 |
Correct |
24 ms |
51172 KB |
Output is correct |
3 |
Correct |
23 ms |
51292 KB |
Output is correct |
4 |
Correct |
24 ms |
51292 KB |
Output is correct |
5 |
Correct |
22 ms |
51292 KB |
Output is correct |
6 |
Correct |
23 ms |
51292 KB |
Output is correct |
7 |
Correct |
23 ms |
51292 KB |
Output is correct |
8 |
Correct |
24 ms |
51292 KB |
Output is correct |
9 |
Correct |
23 ms |
51348 KB |
Output is correct |
10 |
Correct |
23 ms |
51340 KB |
Output is correct |
11 |
Correct |
26 ms |
51288 KB |
Output is correct |
12 |
Correct |
24 ms |
51152 KB |
Output is correct |
13 |
Correct |
25 ms |
51292 KB |
Output is correct |
14 |
Correct |
26 ms |
51288 KB |
Output is correct |
15 |
Correct |
24 ms |
51288 KB |
Output is correct |
16 |
Correct |
23 ms |
51140 KB |
Output is correct |
17 |
Correct |
23 ms |
51288 KB |
Output is correct |
18 |
Correct |
23 ms |
51184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
51292 KB |
Output is correct |
2 |
Correct |
24 ms |
51172 KB |
Output is correct |
3 |
Correct |
23 ms |
51292 KB |
Output is correct |
4 |
Correct |
24 ms |
51292 KB |
Output is correct |
5 |
Correct |
22 ms |
51292 KB |
Output is correct |
6 |
Correct |
23 ms |
51292 KB |
Output is correct |
7 |
Correct |
23 ms |
51292 KB |
Output is correct |
8 |
Correct |
24 ms |
51292 KB |
Output is correct |
9 |
Correct |
23 ms |
51348 KB |
Output is correct |
10 |
Correct |
23 ms |
51340 KB |
Output is correct |
11 |
Correct |
26 ms |
51288 KB |
Output is correct |
12 |
Correct |
24 ms |
51152 KB |
Output is correct |
13 |
Correct |
25 ms |
51292 KB |
Output is correct |
14 |
Correct |
26 ms |
51288 KB |
Output is correct |
15 |
Correct |
24 ms |
51288 KB |
Output is correct |
16 |
Correct |
23 ms |
51140 KB |
Output is correct |
17 |
Correct |
23 ms |
51288 KB |
Output is correct |
18 |
Correct |
23 ms |
51184 KB |
Output is correct |
19 |
Correct |
23 ms |
51288 KB |
Output is correct |
20 |
Correct |
23 ms |
51292 KB |
Output is correct |
21 |
Correct |
23 ms |
51292 KB |
Output is correct |
22 |
Correct |
24 ms |
51428 KB |
Output is correct |
23 |
Correct |
24 ms |
51360 KB |
Output is correct |
24 |
Correct |
25 ms |
51248 KB |
Output is correct |
25 |
Correct |
24 ms |
51292 KB |
Output is correct |
26 |
Correct |
24 ms |
51292 KB |
Output is correct |
27 |
Correct |
23 ms |
51292 KB |
Output is correct |
28 |
Correct |
24 ms |
51288 KB |
Output is correct |
29 |
Correct |
24 ms |
51292 KB |
Output is correct |
30 |
Correct |
24 ms |
51292 KB |
Output is correct |
31 |
Correct |
27 ms |
51292 KB |
Output is correct |
32 |
Correct |
23 ms |
51332 KB |
Output is correct |
33 |
Correct |
26 ms |
51168 KB |
Output is correct |
34 |
Correct |
26 ms |
51292 KB |
Output is correct |
35 |
Correct |
25 ms |
51432 KB |
Output is correct |
36 |
Correct |
27 ms |
51284 KB |
Output is correct |
37 |
Correct |
28 ms |
51292 KB |
Output is correct |
38 |
Correct |
28 ms |
51284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
51292 KB |
Output is correct |
2 |
Correct |
24 ms |
51172 KB |
Output is correct |
3 |
Correct |
23 ms |
51292 KB |
Output is correct |
4 |
Correct |
24 ms |
51292 KB |
Output is correct |
5 |
Correct |
22 ms |
51292 KB |
Output is correct |
6 |
Correct |
23 ms |
51292 KB |
Output is correct |
7 |
Correct |
23 ms |
51292 KB |
Output is correct |
8 |
Correct |
24 ms |
51292 KB |
Output is correct |
9 |
Correct |
23 ms |
51348 KB |
Output is correct |
10 |
Correct |
23 ms |
51340 KB |
Output is correct |
11 |
Correct |
26 ms |
51288 KB |
Output is correct |
12 |
Correct |
24 ms |
51152 KB |
Output is correct |
13 |
Correct |
25 ms |
51292 KB |
Output is correct |
14 |
Correct |
26 ms |
51288 KB |
Output is correct |
15 |
Correct |
24 ms |
51288 KB |
Output is correct |
16 |
Correct |
23 ms |
51140 KB |
Output is correct |
17 |
Correct |
23 ms |
51288 KB |
Output is correct |
18 |
Correct |
23 ms |
51184 KB |
Output is correct |
19 |
Correct |
237 ms |
57816 KB |
Output is correct |
20 |
Correct |
198 ms |
57936 KB |
Output is correct |
21 |
Correct |
196 ms |
57940 KB |
Output is correct |
22 |
Correct |
164 ms |
57988 KB |
Output is correct |
23 |
Correct |
87 ms |
58232 KB |
Output is correct |
24 |
Correct |
63 ms |
58192 KB |
Output is correct |
25 |
Execution timed out |
3091 ms |
61528 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
51292 KB |
Output is correct |
2 |
Correct |
24 ms |
51172 KB |
Output is correct |
3 |
Correct |
23 ms |
51292 KB |
Output is correct |
4 |
Correct |
24 ms |
51292 KB |
Output is correct |
5 |
Correct |
22 ms |
51292 KB |
Output is correct |
6 |
Correct |
23 ms |
51292 KB |
Output is correct |
7 |
Correct |
23 ms |
51292 KB |
Output is correct |
8 |
Correct |
24 ms |
51292 KB |
Output is correct |
9 |
Correct |
23 ms |
51348 KB |
Output is correct |
10 |
Correct |
23 ms |
51340 KB |
Output is correct |
11 |
Correct |
26 ms |
51288 KB |
Output is correct |
12 |
Correct |
24 ms |
51152 KB |
Output is correct |
13 |
Correct |
25 ms |
51292 KB |
Output is correct |
14 |
Correct |
26 ms |
51288 KB |
Output is correct |
15 |
Correct |
24 ms |
51288 KB |
Output is correct |
16 |
Correct |
23 ms |
51140 KB |
Output is correct |
17 |
Correct |
23 ms |
51288 KB |
Output is correct |
18 |
Correct |
23 ms |
51184 KB |
Output is correct |
19 |
Correct |
23 ms |
51288 KB |
Output is correct |
20 |
Correct |
23 ms |
51292 KB |
Output is correct |
21 |
Correct |
23 ms |
51292 KB |
Output is correct |
22 |
Correct |
24 ms |
51428 KB |
Output is correct |
23 |
Correct |
24 ms |
51360 KB |
Output is correct |
24 |
Correct |
25 ms |
51248 KB |
Output is correct |
25 |
Correct |
24 ms |
51292 KB |
Output is correct |
26 |
Correct |
24 ms |
51292 KB |
Output is correct |
27 |
Correct |
23 ms |
51292 KB |
Output is correct |
28 |
Correct |
24 ms |
51288 KB |
Output is correct |
29 |
Correct |
24 ms |
51292 KB |
Output is correct |
30 |
Correct |
24 ms |
51292 KB |
Output is correct |
31 |
Correct |
27 ms |
51292 KB |
Output is correct |
32 |
Correct |
23 ms |
51332 KB |
Output is correct |
33 |
Correct |
26 ms |
51168 KB |
Output is correct |
34 |
Correct |
26 ms |
51292 KB |
Output is correct |
35 |
Correct |
25 ms |
51432 KB |
Output is correct |
36 |
Correct |
27 ms |
51284 KB |
Output is correct |
37 |
Correct |
28 ms |
51292 KB |
Output is correct |
38 |
Correct |
28 ms |
51284 KB |
Output is correct |
39 |
Correct |
237 ms |
57816 KB |
Output is correct |
40 |
Correct |
198 ms |
57936 KB |
Output is correct |
41 |
Correct |
196 ms |
57940 KB |
Output is correct |
42 |
Correct |
164 ms |
57988 KB |
Output is correct |
43 |
Correct |
87 ms |
58232 KB |
Output is correct |
44 |
Correct |
63 ms |
58192 KB |
Output is correct |
45 |
Execution timed out |
3091 ms |
61528 KB |
Time limit exceeded |
46 |
Halted |
0 ms |
0 KB |
- |