#include <bits/stdc++.h>
using namespace std;
int sz[200007];long long all = 0;
int vis[200007];
vector<pair<int,int>> adj[200007];
bool ss = 0;
map<long long,long long> mp;
long long k;
long long ans = 1e18;
void calc(int no,int pr,long long dep,long long cost){
if(cost>k)return ;
if(ss){
if(mp[cost]==0)mp[cost] = dep;
else mp[cost] = min(mp[cost],dep);
}else{
if(mp[k-cost]!=0){
ans = min(ans,dep+mp[k-cost]-2);
}
}
for(auto i:adj[no]){
if(i.first==pr||vis[i.first])continue;
calc(i.first,no,dep+1,cost+i.second);
}
}
int dfs(int no,int pr){
sz[no] = 1;
for(auto i:adj[no]){
if(i.first==pr||vis[i.first]!=0)continue;
sz[no]+=dfs(i.first,no);
}
return sz[no];
}
int get_centroid(int no,int ss,int pr){
for(auto i:adj[no]){
if(pr==i.first||vis[i.first]!=0)continue;
if(sz[i.first]*2>ss){
return get_centroid(i.first,ss,no);
}
}
return no;
}
void centroid(int no){
int cen = get_centroid(no,dfs(no,-1),-1);
mp.clear();
mp[0] = 1;
vis[cen] = 1;
for(auto i:adj[cen]){
if(vis[i.first])continue;
ss = 0;
calc(i.first,-1,2,i.second);
ss = 1;
calc(i.first,-1,2,i.second);
}
for(auto i:adj[cen]){
if(vis[i.first])continue;
centroid(i.first);
}
}
int best_path(int N,int K,int H[][2],int L[]){
k = K;
for(int i = 0;i<N-1;i++){
adj[H[i][0]+1].push_back({H[i][1]+1,L[i]});
adj[H[i][1]+1].push_back({H[i][0]+1,L[i]});
}
centroid(1);
return (ans==1e18?-1:ans);
}
/*
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int N = 3 , K = 3;
int L[] = {1,2,4};
int H[3][2] = {{0,1},{1,2},{1,3}};
cout<<best_path(N,K,H,L);
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
5016 KB |
Output is correct |
4 |
Correct |
3 ms |
5012 KB |
Output is correct |
5 |
Correct |
3 ms |
5020 KB |
Output is correct |
6 |
Correct |
3 ms |
5020 KB |
Output is correct |
7 |
Correct |
3 ms |
5020 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 |
5016 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 |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 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 |
5016 KB |
Output is correct |
4 |
Correct |
3 ms |
5012 KB |
Output is correct |
5 |
Correct |
3 ms |
5020 KB |
Output is correct |
6 |
Correct |
3 ms |
5020 KB |
Output is correct |
7 |
Correct |
3 ms |
5020 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 |
5016 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 |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
5 ms |
5020 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5024 KB |
Output is correct |
24 |
Correct |
4 ms |
5076 KB |
Output is correct |
25 |
Correct |
5 ms |
5156 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5036 KB |
Output is correct |
28 |
Correct |
5 ms |
5156 KB |
Output is correct |
29 |
Correct |
5 ms |
5204 KB |
Output is correct |
30 |
Correct |
5 ms |
5204 KB |
Output is correct |
31 |
Correct |
5 ms |
5152 KB |
Output is correct |
32 |
Correct |
5 ms |
5204 KB |
Output is correct |
33 |
Correct |
6 ms |
5204 KB |
Output is correct |
34 |
Correct |
4 ms |
5028 KB |
Output is correct |
35 |
Correct |
5 ms |
5076 KB |
Output is correct |
36 |
Correct |
5 ms |
5020 KB |
Output is correct |
37 |
Correct |
5 ms |
5204 KB |
Output is correct |
38 |
Correct |
5 ms |
5148 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 |
5016 KB |
Output is correct |
4 |
Correct |
3 ms |
5012 KB |
Output is correct |
5 |
Correct |
3 ms |
5020 KB |
Output is correct |
6 |
Correct |
3 ms |
5020 KB |
Output is correct |
7 |
Correct |
3 ms |
5020 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 |
5016 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 |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
227 ms |
11972 KB |
Output is correct |
20 |
Correct |
227 ms |
11980 KB |
Output is correct |
21 |
Correct |
229 ms |
11992 KB |
Output is correct |
22 |
Correct |
220 ms |
12096 KB |
Output is correct |
23 |
Correct |
92 ms |
12304 KB |
Output is correct |
24 |
Correct |
83 ms |
12192 KB |
Output is correct |
25 |
Correct |
155 ms |
14736 KB |
Output is correct |
26 |
Correct |
109 ms |
17660 KB |
Output is correct |
27 |
Correct |
234 ms |
18124 KB |
Output is correct |
28 |
Correct |
268 ms |
29968 KB |
Output is correct |
29 |
Correct |
276 ms |
29064 KB |
Output is correct |
30 |
Correct |
239 ms |
18796 KB |
Output is correct |
31 |
Correct |
230 ms |
18708 KB |
Output is correct |
32 |
Correct |
259 ms |
18860 KB |
Output is correct |
33 |
Correct |
354 ms |
17536 KB |
Output is correct |
34 |
Correct |
182 ms |
17628 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 |
5016 KB |
Output is correct |
4 |
Correct |
3 ms |
5012 KB |
Output is correct |
5 |
Correct |
3 ms |
5020 KB |
Output is correct |
6 |
Correct |
3 ms |
5020 KB |
Output is correct |
7 |
Correct |
3 ms |
5020 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 |
5016 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 |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
5 ms |
5020 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5024 KB |
Output is correct |
24 |
Correct |
4 ms |
5076 KB |
Output is correct |
25 |
Correct |
5 ms |
5156 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5036 KB |
Output is correct |
28 |
Correct |
5 ms |
5156 KB |
Output is correct |
29 |
Correct |
5 ms |
5204 KB |
Output is correct |
30 |
Correct |
5 ms |
5204 KB |
Output is correct |
31 |
Correct |
5 ms |
5152 KB |
Output is correct |
32 |
Correct |
5 ms |
5204 KB |
Output is correct |
33 |
Correct |
6 ms |
5204 KB |
Output is correct |
34 |
Correct |
4 ms |
5028 KB |
Output is correct |
35 |
Correct |
5 ms |
5076 KB |
Output is correct |
36 |
Correct |
5 ms |
5020 KB |
Output is correct |
37 |
Correct |
5 ms |
5204 KB |
Output is correct |
38 |
Correct |
5 ms |
5148 KB |
Output is correct |
39 |
Correct |
227 ms |
11972 KB |
Output is correct |
40 |
Correct |
227 ms |
11980 KB |
Output is correct |
41 |
Correct |
229 ms |
11992 KB |
Output is correct |
42 |
Correct |
220 ms |
12096 KB |
Output is correct |
43 |
Correct |
92 ms |
12304 KB |
Output is correct |
44 |
Correct |
83 ms |
12192 KB |
Output is correct |
45 |
Correct |
155 ms |
14736 KB |
Output is correct |
46 |
Correct |
109 ms |
17660 KB |
Output is correct |
47 |
Correct |
234 ms |
18124 KB |
Output is correct |
48 |
Correct |
268 ms |
29968 KB |
Output is correct |
49 |
Correct |
276 ms |
29064 KB |
Output is correct |
50 |
Correct |
239 ms |
18796 KB |
Output is correct |
51 |
Correct |
230 ms |
18708 KB |
Output is correct |
52 |
Correct |
259 ms |
18860 KB |
Output is correct |
53 |
Correct |
354 ms |
17536 KB |
Output is correct |
54 |
Correct |
182 ms |
17628 KB |
Output is correct |
55 |
Correct |
26 ms |
5972 KB |
Output is correct |
56 |
Correct |
19 ms |
5588 KB |
Output is correct |
57 |
Correct |
183 ms |
12216 KB |
Output is correct |
58 |
Correct |
46 ms |
11948 KB |
Output is correct |
59 |
Correct |
422 ms |
26532 KB |
Output is correct |
60 |
Correct |
1289 ms |
51184 KB |
Output is correct |
61 |
Correct |
341 ms |
18988 KB |
Output is correct |
62 |
Correct |
365 ms |
19148 KB |
Output is correct |
63 |
Correct |
429 ms |
19236 KB |
Output is correct |
64 |
Correct |
1392 ms |
30716 KB |
Output is correct |
65 |
Correct |
211 ms |
18788 KB |
Output is correct |
66 |
Correct |
819 ms |
27596 KB |
Output is correct |
67 |
Correct |
140 ms |
19276 KB |
Output is correct |
68 |
Correct |
558 ms |
28484 KB |
Output is correct |
69 |
Correct |
533 ms |
28236 KB |
Output is correct |
70 |
Correct |
523 ms |
27684 KB |
Output is correct |