#include "race.h"
#include<bits/stdc++.h>
using namespace std;
const int N=200005;
int sz[N], e[N], z1[N], z2[N], o, k, K, Ans=N;
vector <pair<int, int> > v[N];
map<int, int> m[N];
void solve(int x=0, int pa=0){
sz[x]=1;
int mx=0, Y, D;
for (auto [y,d]:v[x]){
if(y==pa)continue;
solve(y, x);
sz[x]+=sz[y];
if(sz[y]>mx) mx=sz[y], Y=y, D=d;
}
if(sz[x]==1) e[x]=o++;
else z1[x]=z1[Y]+D, z2[x]=z2[Y]+1, e[x]=e[Y];
m[e[x]][0-z1[x]]=0-z2[x];
for (auto [y,d]:v[x]){
if(y==pa||y==Y)continue;
for(auto [kk,ans]:m[e[y]]){
k=kk+d+z1[y]+z1[x];
ans+=1+z2[y]+z2[x];
if(m[e[x]].find(K-k)!=m[e[x]].end()) Ans=min(Ans, ans+m[e[x]][K-k]);
}
for(auto [kk,ans]:m[e[y]]){
k=kk+d+z1[y]-z1[x];
ans+=1+z2[y]-z2[x];
if(m[e[x]].find(k)==m[e[x]].end() || m[e[x]][k]>ans) m[e[x]][k]=ans;
}
}
if(m[e[x]].find(K-z1[x])!=m[e[x]].end())Ans=min(Ans,z2[x]+m[e[x]][K-z1[x]]);
}
int best_path(int n, int k, int H[][2], int L[]){
K=k;
for (int i=0; i<n-1; i++)
v[H[i][0]].push_back({H[i][1],L[i]}),
v[H[i][1]].push_back({H[i][0],L[i]});
solve();
return (Ans==N)?-1:Ans;
}
Compilation message
race.cpp: In function 'void solve(int, int)':
race.cpp:27:11: warning: 'Y' may be used uninitialized in this function [-Wmaybe-uninitialized]
27 | if(y==pa||y==Y)continue;
| ~~~~~^~~~~~
race.cpp:23:18: warning: 'D' may be used uninitialized in this function [-Wmaybe-uninitialized]
23 | else z1[x]=z1[Y]+D, z2[x]=z2[Y]+1, e[x]=e[Y];
| ~~~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14444 KB |
Output is correct |
2 |
Correct |
10 ms |
14444 KB |
Output is correct |
3 |
Correct |
11 ms |
14444 KB |
Output is correct |
4 |
Correct |
10 ms |
14444 KB |
Output is correct |
5 |
Correct |
10 ms |
14444 KB |
Output is correct |
6 |
Correct |
10 ms |
14444 KB |
Output is correct |
7 |
Correct |
10 ms |
14444 KB |
Output is correct |
8 |
Correct |
10 ms |
14444 KB |
Output is correct |
9 |
Correct |
10 ms |
14444 KB |
Output is correct |
10 |
Correct |
10 ms |
14444 KB |
Output is correct |
11 |
Correct |
12 ms |
14444 KB |
Output is correct |
12 |
Correct |
10 ms |
14444 KB |
Output is correct |
13 |
Correct |
10 ms |
14444 KB |
Output is correct |
14 |
Correct |
10 ms |
14444 KB |
Output is correct |
15 |
Correct |
10 ms |
14444 KB |
Output is correct |
16 |
Correct |
10 ms |
14444 KB |
Output is correct |
17 |
Correct |
10 ms |
14444 KB |
Output is correct |
18 |
Correct |
10 ms |
14444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14444 KB |
Output is correct |
2 |
Correct |
10 ms |
14444 KB |
Output is correct |
3 |
Correct |
11 ms |
14444 KB |
Output is correct |
4 |
Correct |
10 ms |
14444 KB |
Output is correct |
5 |
Correct |
10 ms |
14444 KB |
Output is correct |
6 |
Correct |
10 ms |
14444 KB |
Output is correct |
7 |
Correct |
10 ms |
14444 KB |
Output is correct |
8 |
Correct |
10 ms |
14444 KB |
Output is correct |
9 |
Correct |
10 ms |
14444 KB |
Output is correct |
10 |
Correct |
10 ms |
14444 KB |
Output is correct |
11 |
Correct |
12 ms |
14444 KB |
Output is correct |
12 |
Correct |
10 ms |
14444 KB |
Output is correct |
13 |
Correct |
10 ms |
14444 KB |
Output is correct |
14 |
Correct |
10 ms |
14444 KB |
Output is correct |
15 |
Correct |
10 ms |
14444 KB |
Output is correct |
16 |
Correct |
10 ms |
14444 KB |
Output is correct |
17 |
Correct |
10 ms |
14444 KB |
Output is correct |
18 |
Correct |
10 ms |
14444 KB |
Output is correct |
19 |
Correct |
10 ms |
14444 KB |
Output is correct |
20 |
Correct |
10 ms |
14444 KB |
Output is correct |
21 |
Correct |
12 ms |
14572 KB |
Output is correct |
22 |
Correct |
11 ms |
14700 KB |
Output is correct |
23 |
Correct |
12 ms |
14700 KB |
Output is correct |
24 |
Correct |
12 ms |
14700 KB |
Output is correct |
25 |
Correct |
12 ms |
14828 KB |
Output is correct |
26 |
Correct |
11 ms |
14700 KB |
Output is correct |
27 |
Correct |
11 ms |
14572 KB |
Output is correct |
28 |
Correct |
12 ms |
14700 KB |
Output is correct |
29 |
Correct |
12 ms |
14700 KB |
Output is correct |
30 |
Correct |
12 ms |
14700 KB |
Output is correct |
31 |
Correct |
11 ms |
14700 KB |
Output is correct |
32 |
Correct |
13 ms |
14700 KB |
Output is correct |
33 |
Correct |
12 ms |
14700 KB |
Output is correct |
34 |
Correct |
12 ms |
14700 KB |
Output is correct |
35 |
Correct |
11 ms |
14700 KB |
Output is correct |
36 |
Correct |
11 ms |
14700 KB |
Output is correct |
37 |
Correct |
11 ms |
14572 KB |
Output is correct |
38 |
Correct |
11 ms |
14572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14444 KB |
Output is correct |
2 |
Correct |
10 ms |
14444 KB |
Output is correct |
3 |
Correct |
11 ms |
14444 KB |
Output is correct |
4 |
Correct |
10 ms |
14444 KB |
Output is correct |
5 |
Correct |
10 ms |
14444 KB |
Output is correct |
6 |
Correct |
10 ms |
14444 KB |
Output is correct |
7 |
Correct |
10 ms |
14444 KB |
Output is correct |
8 |
Correct |
10 ms |
14444 KB |
Output is correct |
9 |
Correct |
10 ms |
14444 KB |
Output is correct |
10 |
Correct |
10 ms |
14444 KB |
Output is correct |
11 |
Correct |
12 ms |
14444 KB |
Output is correct |
12 |
Correct |
10 ms |
14444 KB |
Output is correct |
13 |
Correct |
10 ms |
14444 KB |
Output is correct |
14 |
Correct |
10 ms |
14444 KB |
Output is correct |
15 |
Correct |
10 ms |
14444 KB |
Output is correct |
16 |
Correct |
10 ms |
14444 KB |
Output is correct |
17 |
Correct |
10 ms |
14444 KB |
Output is correct |
18 |
Correct |
10 ms |
14444 KB |
Output is correct |
19 |
Correct |
124 ms |
30572 KB |
Output is correct |
20 |
Correct |
128 ms |
30652 KB |
Output is correct |
21 |
Correct |
125 ms |
30472 KB |
Output is correct |
22 |
Correct |
137 ms |
30700 KB |
Output is correct |
23 |
Correct |
179 ms |
41068 KB |
Output is correct |
24 |
Correct |
136 ms |
35436 KB |
Output is correct |
25 |
Correct |
93 ms |
33004 KB |
Output is correct |
26 |
Correct |
68 ms |
41964 KB |
Output is correct |
27 |
Correct |
224 ms |
40044 KB |
Output is correct |
28 |
Correct |
243 ms |
79212 KB |
Output is correct |
29 |
Correct |
249 ms |
76908 KB |
Output is correct |
30 |
Correct |
226 ms |
40044 KB |
Output is correct |
31 |
Correct |
230 ms |
40044 KB |
Output is correct |
32 |
Correct |
269 ms |
40172 KB |
Output is correct |
33 |
Correct |
232 ms |
39148 KB |
Output is correct |
34 |
Correct |
363 ms |
63932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14444 KB |
Output is correct |
2 |
Correct |
10 ms |
14444 KB |
Output is correct |
3 |
Correct |
11 ms |
14444 KB |
Output is correct |
4 |
Correct |
10 ms |
14444 KB |
Output is correct |
5 |
Correct |
10 ms |
14444 KB |
Output is correct |
6 |
Correct |
10 ms |
14444 KB |
Output is correct |
7 |
Correct |
10 ms |
14444 KB |
Output is correct |
8 |
Correct |
10 ms |
14444 KB |
Output is correct |
9 |
Correct |
10 ms |
14444 KB |
Output is correct |
10 |
Correct |
10 ms |
14444 KB |
Output is correct |
11 |
Correct |
12 ms |
14444 KB |
Output is correct |
12 |
Correct |
10 ms |
14444 KB |
Output is correct |
13 |
Correct |
10 ms |
14444 KB |
Output is correct |
14 |
Correct |
10 ms |
14444 KB |
Output is correct |
15 |
Correct |
10 ms |
14444 KB |
Output is correct |
16 |
Correct |
10 ms |
14444 KB |
Output is correct |
17 |
Correct |
10 ms |
14444 KB |
Output is correct |
18 |
Correct |
10 ms |
14444 KB |
Output is correct |
19 |
Correct |
10 ms |
14444 KB |
Output is correct |
20 |
Correct |
10 ms |
14444 KB |
Output is correct |
21 |
Correct |
12 ms |
14572 KB |
Output is correct |
22 |
Correct |
11 ms |
14700 KB |
Output is correct |
23 |
Correct |
12 ms |
14700 KB |
Output is correct |
24 |
Correct |
12 ms |
14700 KB |
Output is correct |
25 |
Correct |
12 ms |
14828 KB |
Output is correct |
26 |
Correct |
11 ms |
14700 KB |
Output is correct |
27 |
Correct |
11 ms |
14572 KB |
Output is correct |
28 |
Correct |
12 ms |
14700 KB |
Output is correct |
29 |
Correct |
12 ms |
14700 KB |
Output is correct |
30 |
Correct |
12 ms |
14700 KB |
Output is correct |
31 |
Correct |
11 ms |
14700 KB |
Output is correct |
32 |
Correct |
13 ms |
14700 KB |
Output is correct |
33 |
Correct |
12 ms |
14700 KB |
Output is correct |
34 |
Correct |
12 ms |
14700 KB |
Output is correct |
35 |
Correct |
11 ms |
14700 KB |
Output is correct |
36 |
Correct |
11 ms |
14700 KB |
Output is correct |
37 |
Correct |
11 ms |
14572 KB |
Output is correct |
38 |
Correct |
11 ms |
14572 KB |
Output is correct |
39 |
Correct |
124 ms |
30572 KB |
Output is correct |
40 |
Correct |
128 ms |
30652 KB |
Output is correct |
41 |
Correct |
125 ms |
30472 KB |
Output is correct |
42 |
Correct |
137 ms |
30700 KB |
Output is correct |
43 |
Correct |
179 ms |
41068 KB |
Output is correct |
44 |
Correct |
136 ms |
35436 KB |
Output is correct |
45 |
Correct |
93 ms |
33004 KB |
Output is correct |
46 |
Correct |
68 ms |
41964 KB |
Output is correct |
47 |
Correct |
224 ms |
40044 KB |
Output is correct |
48 |
Correct |
243 ms |
79212 KB |
Output is correct |
49 |
Correct |
249 ms |
76908 KB |
Output is correct |
50 |
Correct |
226 ms |
40044 KB |
Output is correct |
51 |
Correct |
230 ms |
40044 KB |
Output is correct |
52 |
Correct |
269 ms |
40172 KB |
Output is correct |
53 |
Correct |
232 ms |
39148 KB |
Output is correct |
54 |
Correct |
363 ms |
63932 KB |
Output is correct |
55 |
Correct |
30 ms |
16876 KB |
Output is correct |
56 |
Correct |
20 ms |
15852 KB |
Output is correct |
57 |
Correct |
81 ms |
29420 KB |
Output is correct |
58 |
Correct |
67 ms |
26080 KB |
Output is correct |
59 |
Correct |
90 ms |
46700 KB |
Output is correct |
60 |
Correct |
249 ms |
77540 KB |
Output is correct |
61 |
Correct |
262 ms |
42860 KB |
Output is correct |
62 |
Correct |
213 ms |
40044 KB |
Output is correct |
63 |
Correct |
281 ms |
40300 KB |
Output is correct |
64 |
Correct |
482 ms |
78080 KB |
Output is correct |
65 |
Correct |
487 ms |
78828 KB |
Output is correct |
66 |
Correct |
263 ms |
71916 KB |
Output is correct |
67 |
Correct |
183 ms |
40668 KB |
Output is correct |
68 |
Correct |
376 ms |
54380 KB |
Output is correct |
69 |
Correct |
372 ms |
55020 KB |
Output is correct |
70 |
Correct |
336 ms |
52716 KB |
Output is correct |