#include<bits/stdc++.h>
using namespace std;
#define INF 30000000
#define pb push_back
#define ss second
#define ff first
int m,n,sz[200005],cnt[1000005],i,j,x,y,k,p,D[200005],ans;
bool R[200005];
vector< pair<int,int> > v[200005];
vector<int> remover;
int sizer(int node, int pre=-1){
sz[node]=1;
for(pair<int,int> i:v[node])
if(i.ff!=pre && !R[i.ff]) sz[node]+=sizer(i.ff,node);
return sz[node];
}
int get_centroid(int node, int pre=-1){
for(pair<int,int> i:v[node])
if(i.ff!=pre && !R[i.ff] && sz[i.ff]>n/2) return get_centroid(i.ff,node);
return node;
}
void dfs(int node, int pre, int depth){
if(D[node]>k || depth > ans) return;
cnt[D[node]]=min(cnt[D[node]],depth);
remover.pb(D[node]);
for(pair<int,int> i : v[node])
if(i.ff!=pre && !R[i.ff]){
D[i.ff]=D[node]+i.ss;
dfs(i.ff,node,depth+1);
}
}
void shaa(int node, int pre, int depth){
if(D[node]>k || depth > ans) return;
ans=min(ans,depth+cnt[k-D[node]]);
for(pair<int,int> i : v[node])
if(i.ff!=pre && !R[i.ff]){
D[i.ff]=D[node]+i.ss;
shaa(i.ff,node,depth+1);
}
}
void solve(int node){
n=sizer(node);
int C=get_centroid(node);
R[C]=true;
cnt[0]=0;
for(pair<int,int> child : v[C])
if(!R[child.ff]){
D[child.ff]=child.ss;
shaa(child.ff,C,1);
dfs(child.ff,C,1);
}
for(int i : remover) cnt[i]=INF;
remover.clear();
for(pair<int,int> child : v[C])
if(!R[child.ff]) solve(child.ff);
}
int best_path(int N, int K, int H[][2], int L[])
{
k=K;
for(int i=0;i<=k;i++) cnt[i]=INF;
for(int i=0;i<N-1;i++){
v[H[i][0]].pb({H[i][1],L[i]});
v[H[i][1]].pb({H[i][0],L[i]});
}
ans=INF;
solve(1);
if(ans>=N) return -1;
else return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
4 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4952 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
4 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4952 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
5 ms |
8660 KB |
Output is correct |
23 |
Correct |
5 ms |
8020 KB |
Output is correct |
24 |
Correct |
5 ms |
8404 KB |
Output is correct |
25 |
Correct |
5 ms |
8276 KB |
Output is correct |
26 |
Correct |
5 ms |
6380 KB |
Output is correct |
27 |
Correct |
5 ms |
8148 KB |
Output is correct |
28 |
Correct |
4 ms |
5844 KB |
Output is correct |
29 |
Correct |
4 ms |
6356 KB |
Output is correct |
30 |
Correct |
4 ms |
6484 KB |
Output is correct |
31 |
Correct |
5 ms |
7636 KB |
Output is correct |
32 |
Correct |
5 ms |
7764 KB |
Output is correct |
33 |
Correct |
5 ms |
8148 KB |
Output is correct |
34 |
Correct |
5 ms |
7380 KB |
Output is correct |
35 |
Correct |
5 ms |
8148 KB |
Output is correct |
36 |
Correct |
5 ms |
8660 KB |
Output is correct |
37 |
Correct |
5 ms |
8148 KB |
Output is correct |
38 |
Correct |
5 ms |
6996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
4 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4952 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
111 ms |
10760 KB |
Output is correct |
20 |
Correct |
106 ms |
10744 KB |
Output is correct |
21 |
Correct |
122 ms |
10700 KB |
Output is correct |
22 |
Correct |
119 ms |
11120 KB |
Output is correct |
23 |
Correct |
86 ms |
10956 KB |
Output is correct |
24 |
Correct |
61 ms |
10876 KB |
Output is correct |
25 |
Correct |
146 ms |
15856 KB |
Output is correct |
26 |
Correct |
98 ms |
16944 KB |
Output is correct |
27 |
Correct |
159 ms |
16588 KB |
Output is correct |
28 |
Correct |
293 ms |
27008 KB |
Output is correct |
29 |
Correct |
266 ms |
22860 KB |
Output is correct |
30 |
Correct |
161 ms |
16820 KB |
Output is correct |
31 |
Correct |
162 ms |
16684 KB |
Output is correct |
32 |
Correct |
189 ms |
16688 KB |
Output is correct |
33 |
Correct |
188 ms |
15500 KB |
Output is correct |
34 |
Correct |
181 ms |
15552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
4 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4952 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
5 ms |
8660 KB |
Output is correct |
23 |
Correct |
5 ms |
8020 KB |
Output is correct |
24 |
Correct |
5 ms |
8404 KB |
Output is correct |
25 |
Correct |
5 ms |
8276 KB |
Output is correct |
26 |
Correct |
5 ms |
6380 KB |
Output is correct |
27 |
Correct |
5 ms |
8148 KB |
Output is correct |
28 |
Correct |
4 ms |
5844 KB |
Output is correct |
29 |
Correct |
4 ms |
6356 KB |
Output is correct |
30 |
Correct |
4 ms |
6484 KB |
Output is correct |
31 |
Correct |
5 ms |
7636 KB |
Output is correct |
32 |
Correct |
5 ms |
7764 KB |
Output is correct |
33 |
Correct |
5 ms |
8148 KB |
Output is correct |
34 |
Correct |
5 ms |
7380 KB |
Output is correct |
35 |
Correct |
5 ms |
8148 KB |
Output is correct |
36 |
Correct |
5 ms |
8660 KB |
Output is correct |
37 |
Correct |
5 ms |
8148 KB |
Output is correct |
38 |
Correct |
5 ms |
6996 KB |
Output is correct |
39 |
Correct |
111 ms |
10760 KB |
Output is correct |
40 |
Correct |
106 ms |
10744 KB |
Output is correct |
41 |
Correct |
122 ms |
10700 KB |
Output is correct |
42 |
Correct |
119 ms |
11120 KB |
Output is correct |
43 |
Correct |
86 ms |
10956 KB |
Output is correct |
44 |
Correct |
61 ms |
10876 KB |
Output is correct |
45 |
Correct |
146 ms |
15856 KB |
Output is correct |
46 |
Correct |
98 ms |
16944 KB |
Output is correct |
47 |
Correct |
159 ms |
16588 KB |
Output is correct |
48 |
Correct |
293 ms |
27008 KB |
Output is correct |
49 |
Correct |
266 ms |
22860 KB |
Output is correct |
50 |
Correct |
161 ms |
16820 KB |
Output is correct |
51 |
Correct |
162 ms |
16684 KB |
Output is correct |
52 |
Correct |
189 ms |
16688 KB |
Output is correct |
53 |
Correct |
188 ms |
15500 KB |
Output is correct |
54 |
Correct |
181 ms |
15552 KB |
Output is correct |
55 |
Correct |
11 ms |
5668 KB |
Output is correct |
56 |
Correct |
13 ms |
5816 KB |
Output is correct |
57 |
Correct |
87 ms |
11464 KB |
Output is correct |
58 |
Correct |
33 ms |
11856 KB |
Output is correct |
59 |
Correct |
92 ms |
17740 KB |
Output is correct |
60 |
Correct |
468 ms |
28096 KB |
Output is correct |
61 |
Correct |
184 ms |
16848 KB |
Output is correct |
62 |
Correct |
201 ms |
21516 KB |
Output is correct |
63 |
Correct |
251 ms |
21508 KB |
Output is correct |
64 |
Correct |
458 ms |
19364 KB |
Output is correct |
65 |
Correct |
193 ms |
16588 KB |
Output is correct |
66 |
Correct |
347 ms |
29716 KB |
Output is correct |
67 |
Correct |
125 ms |
22576 KB |
Output is correct |
68 |
Correct |
206 ms |
20736 KB |
Output is correct |
69 |
Correct |
219 ms |
20728 KB |
Output is correct |
70 |
Correct |
160 ms |
20176 KB |
Output is correct |