#include "race.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using pll= pair<ll,ll>;
#define pb push_back
vector<pll> adj[200010];
bool hide[200010];
ll sz[200010],k,ans=1e9;
map<ll,ll> mp;
void plant(ll v , ll p = 0){
sz[v] = 1;
for(auto [u,w] : adj[v])
if(u != p && !hide[u]){
plant(u , v);
sz[v] += sz[u];
}
}
ll find_centroid(ll v , ll n , ll p = 0){
bool found = 1;
while(found){
found = 0;
for(auto [u,w] : adj[v])
if(u != p && !hide[u] && sz[u] * 2 > n){
found = 1;
p = v;
v = u;
break;
}
}
return v;
}
void dfs(ll v, ll x, ll h, ll p=0){
if(x<=k && mp.count(k-x)) ans=min(ans,h+mp[k-x]);
for(auto [u,w] : adj[v]) if(!hide[u] && u!=p) dfs(u,x+w,h+1,v);
}
void sfd(ll v, ll x, ll h, ll p=0){
if(x<=k && (!mp.count(x) || mp[x]>h)) mp[x]=h;
for(auto [u,w] : adj[v]) if(!hide[u] && u!=p) sfd(u,x+w,h+1,v);
}
void solve(ll v){
plant(v);
v = find_centroid(v , sz[v]);
hide[v] = 1;
mp.clear();
mp[0]=0;
for(auto [u,w] : adj[v]){
if(!hide[u]){
dfs(u,w,1);
sfd(u,w,1);
}
}
for(auto [u,w] : adj[v]) if(!hide[u]) solve(u);
}
int best_path(int N, int K, int H[][2], int L[]){
for(ll i=0; i<N-1; i++){
ll v=H[i][0]+1,u=H[i][1]+1;
adj[v].pb({u,L[i]});
adj[u].pb({v,L[i]});
}
k=K;
solve(1);
if(ans==1e9) ans=-1;
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 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 |
5 ms |
4924 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
5 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 |
4 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 |
4 ms |
4948 KB |
Output is correct |
14 |
Correct |
4 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 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 |
5 ms |
4924 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
5 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 |
4 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 |
4 ms |
4948 KB |
Output is correct |
14 |
Correct |
4 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 |
5000 KB |
Output is correct |
20 |
Correct |
4 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5076 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
4 ms |
5076 KB |
Output is correct |
25 |
Correct |
4 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
6 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5076 KB |
Output is correct |
30 |
Correct |
6 ms |
5148 KB |
Output is correct |
31 |
Correct |
5 ms |
5076 KB |
Output is correct |
32 |
Correct |
4 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5076 KB |
Output is correct |
34 |
Correct |
4 ms |
5076 KB |
Output is correct |
35 |
Correct |
5 ms |
5076 KB |
Output is correct |
36 |
Correct |
4 ms |
5076 KB |
Output is correct |
37 |
Correct |
4 ms |
5076 KB |
Output is correct |
38 |
Correct |
6 ms |
5156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 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 |
5 ms |
4924 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
5 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 |
4 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 |
4 ms |
4948 KB |
Output is correct |
14 |
Correct |
4 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 |
299 ms |
12160 KB |
Output is correct |
20 |
Correct |
357 ms |
12164 KB |
Output is correct |
21 |
Correct |
314 ms |
12108 KB |
Output is correct |
22 |
Correct |
304 ms |
12204 KB |
Output is correct |
23 |
Correct |
169 ms |
12544 KB |
Output is correct |
24 |
Correct |
101 ms |
11664 KB |
Output is correct |
25 |
Correct |
203 ms |
16700 KB |
Output is correct |
26 |
Correct |
150 ms |
18264 KB |
Output is correct |
27 |
Correct |
293 ms |
20144 KB |
Output is correct |
28 |
Correct |
590 ms |
31332 KB |
Output is correct |
29 |
Correct |
623 ms |
30428 KB |
Output is correct |
30 |
Correct |
280 ms |
20232 KB |
Output is correct |
31 |
Correct |
308 ms |
20224 KB |
Output is correct |
32 |
Correct |
374 ms |
20124 KB |
Output is correct |
33 |
Correct |
523 ms |
18928 KB |
Output is correct |
34 |
Correct |
432 ms |
18944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 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 |
5 ms |
4924 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
5 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 |
4 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 |
4 ms |
4948 KB |
Output is correct |
14 |
Correct |
4 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 |
5000 KB |
Output is correct |
20 |
Correct |
4 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5076 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
4 ms |
5076 KB |
Output is correct |
25 |
Correct |
4 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
6 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5076 KB |
Output is correct |
30 |
Correct |
6 ms |
5148 KB |
Output is correct |
31 |
Correct |
5 ms |
5076 KB |
Output is correct |
32 |
Correct |
4 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5076 KB |
Output is correct |
34 |
Correct |
4 ms |
5076 KB |
Output is correct |
35 |
Correct |
5 ms |
5076 KB |
Output is correct |
36 |
Correct |
4 ms |
5076 KB |
Output is correct |
37 |
Correct |
4 ms |
5076 KB |
Output is correct |
38 |
Correct |
6 ms |
5156 KB |
Output is correct |
39 |
Correct |
299 ms |
12160 KB |
Output is correct |
40 |
Correct |
357 ms |
12164 KB |
Output is correct |
41 |
Correct |
314 ms |
12108 KB |
Output is correct |
42 |
Correct |
304 ms |
12204 KB |
Output is correct |
43 |
Correct |
169 ms |
12544 KB |
Output is correct |
44 |
Correct |
101 ms |
11664 KB |
Output is correct |
45 |
Correct |
203 ms |
16700 KB |
Output is correct |
46 |
Correct |
150 ms |
18264 KB |
Output is correct |
47 |
Correct |
293 ms |
20144 KB |
Output is correct |
48 |
Correct |
590 ms |
31332 KB |
Output is correct |
49 |
Correct |
623 ms |
30428 KB |
Output is correct |
50 |
Correct |
280 ms |
20232 KB |
Output is correct |
51 |
Correct |
308 ms |
20224 KB |
Output is correct |
52 |
Correct |
374 ms |
20124 KB |
Output is correct |
53 |
Correct |
523 ms |
18928 KB |
Output is correct |
54 |
Correct |
432 ms |
18944 KB |
Output is correct |
55 |
Correct |
26 ms |
6012 KB |
Output is correct |
56 |
Correct |
18 ms |
5764 KB |
Output is correct |
57 |
Correct |
158 ms |
12780 KB |
Output is correct |
58 |
Correct |
50 ms |
12068 KB |
Output is correct |
59 |
Correct |
322 ms |
22964 KB |
Output is correct |
60 |
Correct |
1139 ms |
42256 KB |
Output is correct |
61 |
Correct |
343 ms |
20560 KB |
Output is correct |
62 |
Correct |
336 ms |
20408 KB |
Output is correct |
63 |
Correct |
401 ms |
20412 KB |
Output is correct |
64 |
Correct |
1243 ms |
25756 KB |
Output is correct |
65 |
Correct |
354 ms |
19764 KB |
Output is correct |
66 |
Correct |
724 ms |
28588 KB |
Output is correct |
67 |
Correct |
130 ms |
18880 KB |
Output is correct |
68 |
Correct |
432 ms |
24532 KB |
Output is correct |
69 |
Correct |
458 ms |
24280 KB |
Output is correct |
70 |
Correct |
452 ms |
23668 KB |
Output is correct |