#include<bits/stdc++.h>
#include "race.h"
using namespace std;
#define ll long long
#define f first
#define s second
#define all(x) x,begin(),x.end()
const int mxn=2e5+5;
vector<pair<int,ll>> adj[mxn];
int depth[mxn];
ll len[mxn];
multiset<pair<ll,int>> S[mxn];
int ans=1e9;
ll k;
void dfs(int v,int p,int w=0){
depth[v]=depth[p]+1;
len[v]=len[p]+w;
S[v].insert({len[v],depth[v]});
for(auto u:adj[v]){
if(u.f==p) continue;
dfs(u.f,v,u.s);
if(S[v].size()<S[u.f].size()){
S[v].swap(S[u.f]);
}
for(auto &i:S[u.f]){
ll target=k-i.f+2*len[v];
auto it=S[v].lower_bound({target,0});
if(it!=S[v].end() and (*it).f==target){
ans=min(ans,i.s+(*it).s-2*depth[v]);
}
}
for(auto &i:S[u.f]){
S[v].insert(i);
}
}
}
int best_path(int N, int K, int H[][2], int L[])
{
k=K;
for(int i=0;i<N-1;i++){
int a=H[i][0];
int b=H[i][1];
int w=L[i];
adj[a].push_back({b,w});
adj[b].push_back({a,w});
}
dfs(0,0);
return (ans==1e9?-1:ans);
}
/*int main(){
int n,k;
cin>>n>>k;
int H[n-1][2];
int L[n-1];
for(int i=0;i<n-1;i++){
cin>>H[i][0]>>H[i][1];
}
for(int i=0;i<n-1;i++){
cin>>L[i];
}
cout<<best_path(n,k,H,L);
}*/
/*
11 12
0 1
0 2
2 3
3 4
4 5
0 6
6 7
6 8
8 9
8 10
3
4
5
4
6
3
2
5
6
7
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14448 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
9 ms |
14420 KB |
Output is correct |
7 |
Correct |
10 ms |
14440 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
9 ms |
14444 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
9 ms |
14420 KB |
Output is correct |
12 |
Correct |
7 ms |
14420 KB |
Output is correct |
13 |
Correct |
6 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
8 ms |
14368 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
9 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14448 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
9 ms |
14420 KB |
Output is correct |
7 |
Correct |
10 ms |
14440 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
9 ms |
14444 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
9 ms |
14420 KB |
Output is correct |
12 |
Correct |
7 ms |
14420 KB |
Output is correct |
13 |
Correct |
6 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
8 ms |
14368 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
9 ms |
14420 KB |
Output is correct |
19 |
Correct |
9 ms |
14420 KB |
Output is correct |
20 |
Correct |
7 ms |
14420 KB |
Output is correct |
21 |
Correct |
9 ms |
14676 KB |
Output is correct |
22 |
Correct |
9 ms |
14676 KB |
Output is correct |
23 |
Correct |
13 ms |
14660 KB |
Output is correct |
24 |
Correct |
8 ms |
14724 KB |
Output is correct |
25 |
Correct |
8 ms |
14720 KB |
Output is correct |
26 |
Correct |
8 ms |
14676 KB |
Output is correct |
27 |
Correct |
11 ms |
14676 KB |
Output is correct |
28 |
Correct |
8 ms |
14676 KB |
Output is correct |
29 |
Correct |
8 ms |
14676 KB |
Output is correct |
30 |
Correct |
8 ms |
14676 KB |
Output is correct |
31 |
Correct |
8 ms |
14676 KB |
Output is correct |
32 |
Correct |
11 ms |
14676 KB |
Output is correct |
33 |
Correct |
8 ms |
14676 KB |
Output is correct |
34 |
Correct |
10 ms |
14548 KB |
Output is correct |
35 |
Correct |
10 ms |
14604 KB |
Output is correct |
36 |
Correct |
8 ms |
14660 KB |
Output is correct |
37 |
Correct |
11 ms |
14588 KB |
Output is correct |
38 |
Correct |
9 ms |
14676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14448 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
9 ms |
14420 KB |
Output is correct |
7 |
Correct |
10 ms |
14440 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
9 ms |
14444 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
9 ms |
14420 KB |
Output is correct |
12 |
Correct |
7 ms |
14420 KB |
Output is correct |
13 |
Correct |
6 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
8 ms |
14368 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
9 ms |
14420 KB |
Output is correct |
19 |
Correct |
240 ms |
62624 KB |
Output is correct |
20 |
Correct |
227 ms |
61852 KB |
Output is correct |
21 |
Correct |
210 ms |
62208 KB |
Output is correct |
22 |
Correct |
201 ms |
57012 KB |
Output is correct |
23 |
Correct |
275 ms |
74244 KB |
Output is correct |
24 |
Correct |
195 ms |
58124 KB |
Output is correct |
25 |
Correct |
145 ms |
43428 KB |
Output is correct |
26 |
Correct |
83 ms |
46496 KB |
Output is correct |
27 |
Correct |
335 ms |
67236 KB |
Output is correct |
28 |
Correct |
320 ms |
81672 KB |
Output is correct |
29 |
Correct |
312 ms |
80604 KB |
Output is correct |
30 |
Correct |
294 ms |
69924 KB |
Output is correct |
31 |
Correct |
312 ms |
69968 KB |
Output is correct |
32 |
Correct |
388 ms |
70100 KB |
Output is correct |
33 |
Correct |
377 ms |
90468 KB |
Output is correct |
34 |
Correct |
334 ms |
89356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14448 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
9 ms |
14420 KB |
Output is correct |
7 |
Correct |
10 ms |
14440 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
9 ms |
14444 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
9 ms |
14420 KB |
Output is correct |
12 |
Correct |
7 ms |
14420 KB |
Output is correct |
13 |
Correct |
6 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
8 ms |
14368 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
9 ms |
14420 KB |
Output is correct |
19 |
Correct |
9 ms |
14420 KB |
Output is correct |
20 |
Correct |
7 ms |
14420 KB |
Output is correct |
21 |
Correct |
9 ms |
14676 KB |
Output is correct |
22 |
Correct |
9 ms |
14676 KB |
Output is correct |
23 |
Correct |
13 ms |
14660 KB |
Output is correct |
24 |
Correct |
8 ms |
14724 KB |
Output is correct |
25 |
Correct |
8 ms |
14720 KB |
Output is correct |
26 |
Correct |
8 ms |
14676 KB |
Output is correct |
27 |
Correct |
11 ms |
14676 KB |
Output is correct |
28 |
Correct |
8 ms |
14676 KB |
Output is correct |
29 |
Correct |
8 ms |
14676 KB |
Output is correct |
30 |
Correct |
8 ms |
14676 KB |
Output is correct |
31 |
Correct |
8 ms |
14676 KB |
Output is correct |
32 |
Correct |
11 ms |
14676 KB |
Output is correct |
33 |
Correct |
8 ms |
14676 KB |
Output is correct |
34 |
Correct |
10 ms |
14548 KB |
Output is correct |
35 |
Correct |
10 ms |
14604 KB |
Output is correct |
36 |
Correct |
8 ms |
14660 KB |
Output is correct |
37 |
Correct |
11 ms |
14588 KB |
Output is correct |
38 |
Correct |
9 ms |
14676 KB |
Output is correct |
39 |
Correct |
240 ms |
62624 KB |
Output is correct |
40 |
Correct |
227 ms |
61852 KB |
Output is correct |
41 |
Correct |
210 ms |
62208 KB |
Output is correct |
42 |
Correct |
201 ms |
57012 KB |
Output is correct |
43 |
Correct |
275 ms |
74244 KB |
Output is correct |
44 |
Correct |
195 ms |
58124 KB |
Output is correct |
45 |
Correct |
145 ms |
43428 KB |
Output is correct |
46 |
Correct |
83 ms |
46496 KB |
Output is correct |
47 |
Correct |
335 ms |
67236 KB |
Output is correct |
48 |
Correct |
320 ms |
81672 KB |
Output is correct |
49 |
Correct |
312 ms |
80604 KB |
Output is correct |
50 |
Correct |
294 ms |
69924 KB |
Output is correct |
51 |
Correct |
312 ms |
69968 KB |
Output is correct |
52 |
Correct |
388 ms |
70100 KB |
Output is correct |
53 |
Correct |
377 ms |
90468 KB |
Output is correct |
54 |
Correct |
334 ms |
89356 KB |
Output is correct |
55 |
Correct |
21 ms |
18048 KB |
Output is correct |
56 |
Correct |
30 ms |
18520 KB |
Output is correct |
57 |
Correct |
221 ms |
77628 KB |
Output is correct |
58 |
Correct |
88 ms |
34952 KB |
Output is correct |
59 |
Correct |
80 ms |
47844 KB |
Output is correct |
60 |
Correct |
275 ms |
80852 KB |
Output is correct |
61 |
Correct |
312 ms |
70148 KB |
Output is correct |
62 |
Correct |
264 ms |
69968 KB |
Output is correct |
63 |
Correct |
372 ms |
70076 KB |
Output is correct |
64 |
Correct |
474 ms |
115012 KB |
Output is correct |
65 |
Correct |
480 ms |
104968 KB |
Output is correct |
66 |
Correct |
307 ms |
78728 KB |
Output is correct |
67 |
Correct |
300 ms |
57092 KB |
Output is correct |
68 |
Correct |
396 ms |
69964 KB |
Output is correct |
69 |
Correct |
398 ms |
75376 KB |
Output is correct |
70 |
Correct |
354 ms |
67476 KB |
Output is correct |