#include<bits/stdc++.h>
using namespace std;
using I=int;
using Lli=long long int;
const I N=200000;
const I MAX=1e9;
vector<pair<I,I>>adjs[N];
map<Lli,I>vals[N];
I res=MAX;
I len;
void dfs(I a,I p=-1,I dep1=0,Lli dis1=0){
vals[a][dis1]=dep1;
for(auto[b,l]:adjs[a])if(b!=p){
dfs(b,a,dep1+1,dis1+l);
if(vals[b].size()>vals[a].size())swap(vals[a],vals[b]);
for(auto[dis2,dep2]:vals[b]){
Lli dis3=len-(dis2-dis1)+dis1;
if(!vals[a].count(dis3))continue;
res=min(res,vals[a][dis3]+dep2-dep1*2);
}
for(auto[dis2,dep2]:vals[b]){
if(!vals[a].count(dis2))vals[a][dis2]=MAX;
vals[a][dis2]=min(vals[a][dis2],dep2);
}
}
}
I best_path(I n,I k,I h_arr[][2],I l_arr[]){
len=k;
for(I i=0;i<n-1;i++){
I a=h_arr[i][0],b=h_arr[i][1],l=l_arr[i];
adjs[a].push_back({b,l});
adjs[b].push_back({a,l});
}
dfs(0);
return res==MAX?-1:res;
}
#ifdef ETHANKIM8683
I main(){
cin.tie(0)->sync_with_stdio(0);
I n,k;cin>>n>>k;
I h_arr[n-1][2],l_arr[n-1];
for(I i=0;i<n;i++)cin>>h_arr[i][0]>>h_arr[i][1]>>l_arr[i];
printf("%i\n",best_path(n,k,h_arr,l_arr));
}
#endif
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14428 KB |
Output is correct |
5 |
Correct |
7 ms |
14424 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
9 ms |
14420 KB |
Output is correct |
8 |
Correct |
10 ms |
14368 KB |
Output is correct |
9 |
Correct |
11 ms |
14360 KB |
Output is correct |
10 |
Correct |
8 ms |
14372 KB |
Output is correct |
11 |
Correct |
8 ms |
14360 KB |
Output is correct |
12 |
Correct |
9 ms |
14348 KB |
Output is correct |
13 |
Correct |
7 ms |
14432 KB |
Output is correct |
14 |
Correct |
7 ms |
14420 KB |
Output is correct |
15 |
Correct |
10 ms |
14420 KB |
Output is correct |
16 |
Correct |
9 ms |
14420 KB |
Output is correct |
17 |
Correct |
9 ms |
14400 KB |
Output is correct |
18 |
Correct |
7 ms |
14432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14428 KB |
Output is correct |
5 |
Correct |
7 ms |
14424 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
9 ms |
14420 KB |
Output is correct |
8 |
Correct |
10 ms |
14368 KB |
Output is correct |
9 |
Correct |
11 ms |
14360 KB |
Output is correct |
10 |
Correct |
8 ms |
14372 KB |
Output is correct |
11 |
Correct |
8 ms |
14360 KB |
Output is correct |
12 |
Correct |
9 ms |
14348 KB |
Output is correct |
13 |
Correct |
7 ms |
14432 KB |
Output is correct |
14 |
Correct |
7 ms |
14420 KB |
Output is correct |
15 |
Correct |
10 ms |
14420 KB |
Output is correct |
16 |
Correct |
9 ms |
14420 KB |
Output is correct |
17 |
Correct |
9 ms |
14400 KB |
Output is correct |
18 |
Correct |
7 ms |
14432 KB |
Output is correct |
19 |
Correct |
7 ms |
14292 KB |
Output is correct |
20 |
Correct |
8 ms |
14420 KB |
Output is correct |
21 |
Correct |
8 ms |
14596 KB |
Output is correct |
22 |
Correct |
8 ms |
14676 KB |
Output is correct |
23 |
Correct |
10 ms |
14588 KB |
Output is correct |
24 |
Correct |
9 ms |
14676 KB |
Output is correct |
25 |
Correct |
10 ms |
14676 KB |
Output is correct |
26 |
Correct |
9 ms |
14676 KB |
Output is correct |
27 |
Correct |
8 ms |
14548 KB |
Output is correct |
28 |
Correct |
10 ms |
14676 KB |
Output is correct |
29 |
Correct |
9 ms |
14676 KB |
Output is correct |
30 |
Correct |
10 ms |
14584 KB |
Output is correct |
31 |
Correct |
9 ms |
14676 KB |
Output is correct |
32 |
Correct |
10 ms |
14652 KB |
Output is correct |
33 |
Correct |
11 ms |
14808 KB |
Output is correct |
34 |
Correct |
9 ms |
14580 KB |
Output is correct |
35 |
Correct |
8 ms |
14568 KB |
Output is correct |
36 |
Correct |
8 ms |
14548 KB |
Output is correct |
37 |
Correct |
8 ms |
14548 KB |
Output is correct |
38 |
Correct |
8 ms |
14548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14428 KB |
Output is correct |
5 |
Correct |
7 ms |
14424 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
9 ms |
14420 KB |
Output is correct |
8 |
Correct |
10 ms |
14368 KB |
Output is correct |
9 |
Correct |
11 ms |
14360 KB |
Output is correct |
10 |
Correct |
8 ms |
14372 KB |
Output is correct |
11 |
Correct |
8 ms |
14360 KB |
Output is correct |
12 |
Correct |
9 ms |
14348 KB |
Output is correct |
13 |
Correct |
7 ms |
14432 KB |
Output is correct |
14 |
Correct |
7 ms |
14420 KB |
Output is correct |
15 |
Correct |
10 ms |
14420 KB |
Output is correct |
16 |
Correct |
9 ms |
14420 KB |
Output is correct |
17 |
Correct |
9 ms |
14400 KB |
Output is correct |
18 |
Correct |
7 ms |
14432 KB |
Output is correct |
19 |
Correct |
115 ms |
34848 KB |
Output is correct |
20 |
Correct |
121 ms |
34804 KB |
Output is correct |
21 |
Correct |
116 ms |
34508 KB |
Output is correct |
22 |
Correct |
144 ms |
34056 KB |
Output is correct |
23 |
Correct |
176 ms |
46788 KB |
Output is correct |
24 |
Correct |
131 ms |
37444 KB |
Output is correct |
25 |
Correct |
103 ms |
34948 KB |
Output is correct |
26 |
Correct |
56 ms |
42440 KB |
Output is correct |
27 |
Correct |
183 ms |
43764 KB |
Output is correct |
28 |
Correct |
339 ms |
82744 KB |
Output is correct |
29 |
Correct |
314 ms |
80992 KB |
Output is correct |
30 |
Correct |
204 ms |
43588 KB |
Output is correct |
31 |
Correct |
193 ms |
43628 KB |
Output is correct |
32 |
Correct |
249 ms |
43648 KB |
Output is correct |
33 |
Correct |
246 ms |
48640 KB |
Output is correct |
34 |
Correct |
383 ms |
80580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14428 KB |
Output is correct |
5 |
Correct |
7 ms |
14424 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
9 ms |
14420 KB |
Output is correct |
8 |
Correct |
10 ms |
14368 KB |
Output is correct |
9 |
Correct |
11 ms |
14360 KB |
Output is correct |
10 |
Correct |
8 ms |
14372 KB |
Output is correct |
11 |
Correct |
8 ms |
14360 KB |
Output is correct |
12 |
Correct |
9 ms |
14348 KB |
Output is correct |
13 |
Correct |
7 ms |
14432 KB |
Output is correct |
14 |
Correct |
7 ms |
14420 KB |
Output is correct |
15 |
Correct |
10 ms |
14420 KB |
Output is correct |
16 |
Correct |
9 ms |
14420 KB |
Output is correct |
17 |
Correct |
9 ms |
14400 KB |
Output is correct |
18 |
Correct |
7 ms |
14432 KB |
Output is correct |
19 |
Correct |
7 ms |
14292 KB |
Output is correct |
20 |
Correct |
8 ms |
14420 KB |
Output is correct |
21 |
Correct |
8 ms |
14596 KB |
Output is correct |
22 |
Correct |
8 ms |
14676 KB |
Output is correct |
23 |
Correct |
10 ms |
14588 KB |
Output is correct |
24 |
Correct |
9 ms |
14676 KB |
Output is correct |
25 |
Correct |
10 ms |
14676 KB |
Output is correct |
26 |
Correct |
9 ms |
14676 KB |
Output is correct |
27 |
Correct |
8 ms |
14548 KB |
Output is correct |
28 |
Correct |
10 ms |
14676 KB |
Output is correct |
29 |
Correct |
9 ms |
14676 KB |
Output is correct |
30 |
Correct |
10 ms |
14584 KB |
Output is correct |
31 |
Correct |
9 ms |
14676 KB |
Output is correct |
32 |
Correct |
10 ms |
14652 KB |
Output is correct |
33 |
Correct |
11 ms |
14808 KB |
Output is correct |
34 |
Correct |
9 ms |
14580 KB |
Output is correct |
35 |
Correct |
8 ms |
14568 KB |
Output is correct |
36 |
Correct |
8 ms |
14548 KB |
Output is correct |
37 |
Correct |
8 ms |
14548 KB |
Output is correct |
38 |
Correct |
8 ms |
14548 KB |
Output is correct |
39 |
Correct |
115 ms |
34848 KB |
Output is correct |
40 |
Correct |
121 ms |
34804 KB |
Output is correct |
41 |
Correct |
116 ms |
34508 KB |
Output is correct |
42 |
Correct |
144 ms |
34056 KB |
Output is correct |
43 |
Correct |
176 ms |
46788 KB |
Output is correct |
44 |
Correct |
131 ms |
37444 KB |
Output is correct |
45 |
Correct |
103 ms |
34948 KB |
Output is correct |
46 |
Correct |
56 ms |
42440 KB |
Output is correct |
47 |
Correct |
183 ms |
43764 KB |
Output is correct |
48 |
Correct |
339 ms |
82744 KB |
Output is correct |
49 |
Correct |
314 ms |
80992 KB |
Output is correct |
50 |
Correct |
204 ms |
43588 KB |
Output is correct |
51 |
Correct |
193 ms |
43628 KB |
Output is correct |
52 |
Correct |
249 ms |
43648 KB |
Output is correct |
53 |
Correct |
246 ms |
48640 KB |
Output is correct |
54 |
Correct |
383 ms |
80580 KB |
Output is correct |
55 |
Correct |
23 ms |
17492 KB |
Output is correct |
56 |
Correct |
17 ms |
16212 KB |
Output is correct |
57 |
Correct |
79 ms |
32528 KB |
Output is correct |
58 |
Correct |
52 ms |
25988 KB |
Output is correct |
59 |
Correct |
103 ms |
48628 KB |
Output is correct |
60 |
Correct |
310 ms |
81572 KB |
Output is correct |
61 |
Correct |
235 ms |
47028 KB |
Output is correct |
62 |
Correct |
187 ms |
43544 KB |
Output is correct |
63 |
Correct |
243 ms |
43596 KB |
Output is correct |
64 |
Correct |
504 ms |
98248 KB |
Output is correct |
65 |
Correct |
499 ms |
96092 KB |
Output is correct |
66 |
Correct |
343 ms |
77456 KB |
Output is correct |
67 |
Correct |
165 ms |
37560 KB |
Output is correct |
68 |
Correct |
384 ms |
60040 KB |
Output is correct |
69 |
Correct |
426 ms |
65012 KB |
Output is correct |
70 |
Correct |
370 ms |
58292 KB |
Output is correct |