#include <bits/stdc++.h>
using namespace std;
#define in ({int x=0;int c=getchar(),n=0;for(;!isdigit(c);c=getchar()) n=(c=='-');for(;isdigit(c);c=getchar()) x=x*10+c-'0';n?-x:x;})
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l,int r){return l+rng()%(r-l+1);}
#define fasty ios_base::sync_with_stdio(0),cin.tie(0);
#define forinc(a,b,c) for(int a=b,_c=c;a<=_c;++a)
#define fordec(a,b,c) for(int a=b,_c=c;a>=_c;--a)
#define forv(a,b) for(auto&a:b)
#define fi first
#define se second
#define pb push_back
#define ii pair<int,int>
#define mt make_tuple
#define all(a) a.begin(),a.end()
#define reset(f, x) memset(f, x, sizeof(f))
#define bit(x,i) ((x>>(i-1))&1)
#define on(x,i) (x|(1ll<<(i-1)))
#define off(x,i) (x&~(1<<(i-1)))
#define gg exit(0);
//#define unx 1
#ifndef unx
#include "race.h"
#endif
const int N=200010;
int n,cnt,k,ret;
int sz[N],dd[N],f[N*5];
vector<ii> val;
vector<ii> ad[N];
void dfs1(int u,int p=-1){
sz[u]=1; cnt++;
forv(v,ad[u]) if(!dd[v.fi] && v.fi!=p)
dfs1(v.fi,u), sz[u]+=sz[v.fi];
}
int ftr(int u,int p=-1){
forv(v,ad[u]) if(!dd[v.fi] && v.fi!=p && sz[v.fi]>cnt/2) return ftr(v.fi,u);
return u;
}
void dfs2(int u,int p,int c,int d){
if(c>k) return;
val.push_back({c,d});
forv(v,ad[u]) if(!dd[v.fi] && v.fi!=p && v.se+c<=k)
dfs2(v.fi,u,c+v.se,d+1);
}
void ctr(int u){
cnt=0; dfs1(u); dd[u=ftr(u)]=1;
f[0]=0;
vector<int> reval;
forv(v,ad[u]) if(!dd[v.fi]){
dfs2(v.fi,u,v.se,1);
forv(i,val) if(f[k-i.fi]>-1) ret=min(ret,i.se+f[k-i.fi]);
forv(i,val) f[i.fi]=f[i.fi]<0 ? i.se : min(f[i.fi],i.se), reval.push_back(i.fi);
val.clear();
}
forv(i,reval) f[i]=-1;
forv(v,ad[u]) if(!dd[v.fi]) ctr(v.fi);
}
int best_path(int n,int _k,int h[][2],int l[]){ k=_k; ret=n;
forinc(i,0,n-2){
int u=h[i][0], v=h[i][1], w=l[i];
ad[u].push_back({v,w});
ad[v].push_back({u,w});
}
reset(f,-1);
ctr(0);
return ret<n ? ret : -1;
}
#ifdef unx
main(){
#define task "RACE"
freopen(task".inp","r",stdin);
//freopen(task".out","w",stdout);
int n, k; cin>>n>>k;
int H[100][2];
int L[100];
forinc(i,2,n){
cin>>H[i-2][0]>>H[i-2][1];
}
forinc(i,2,n){
cin>>L[i-2];
}
cout<<best_path(n,k,H,L);
}
#endif
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
8960 KB |
Output is correct |
2 |
Correct |
9 ms |
8960 KB |
Output is correct |
3 |
Correct |
10 ms |
8960 KB |
Output is correct |
4 |
Correct |
9 ms |
8960 KB |
Output is correct |
5 |
Correct |
9 ms |
8960 KB |
Output is correct |
6 |
Correct |
9 ms |
8960 KB |
Output is correct |
7 |
Correct |
9 ms |
8960 KB |
Output is correct |
8 |
Correct |
9 ms |
8960 KB |
Output is correct |
9 |
Correct |
10 ms |
8960 KB |
Output is correct |
10 |
Correct |
9 ms |
8960 KB |
Output is correct |
11 |
Correct |
9 ms |
8960 KB |
Output is correct |
12 |
Correct |
10 ms |
8960 KB |
Output is correct |
13 |
Correct |
9 ms |
8960 KB |
Output is correct |
14 |
Correct |
9 ms |
8960 KB |
Output is correct |
15 |
Correct |
9 ms |
8960 KB |
Output is correct |
16 |
Correct |
9 ms |
8960 KB |
Output is correct |
17 |
Correct |
9 ms |
8960 KB |
Output is correct |
18 |
Correct |
10 ms |
8960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
8960 KB |
Output is correct |
2 |
Correct |
9 ms |
8960 KB |
Output is correct |
3 |
Correct |
10 ms |
8960 KB |
Output is correct |
4 |
Correct |
9 ms |
8960 KB |
Output is correct |
5 |
Correct |
9 ms |
8960 KB |
Output is correct |
6 |
Correct |
9 ms |
8960 KB |
Output is correct |
7 |
Correct |
9 ms |
8960 KB |
Output is correct |
8 |
Correct |
9 ms |
8960 KB |
Output is correct |
9 |
Correct |
10 ms |
8960 KB |
Output is correct |
10 |
Correct |
9 ms |
8960 KB |
Output is correct |
11 |
Correct |
9 ms |
8960 KB |
Output is correct |
12 |
Correct |
10 ms |
8960 KB |
Output is correct |
13 |
Correct |
9 ms |
8960 KB |
Output is correct |
14 |
Correct |
9 ms |
8960 KB |
Output is correct |
15 |
Correct |
9 ms |
8960 KB |
Output is correct |
16 |
Correct |
9 ms |
8960 KB |
Output is correct |
17 |
Correct |
9 ms |
8960 KB |
Output is correct |
18 |
Correct |
10 ms |
8960 KB |
Output is correct |
19 |
Correct |
9 ms |
8960 KB |
Output is correct |
20 |
Correct |
9 ms |
9008 KB |
Output is correct |
21 |
Correct |
13 ms |
8960 KB |
Output is correct |
22 |
Correct |
11 ms |
8960 KB |
Output is correct |
23 |
Correct |
10 ms |
9088 KB |
Output is correct |
24 |
Correct |
10 ms |
9088 KB |
Output is correct |
25 |
Correct |
10 ms |
9088 KB |
Output is correct |
26 |
Correct |
11 ms |
9080 KB |
Output is correct |
27 |
Correct |
10 ms |
9088 KB |
Output is correct |
28 |
Correct |
11 ms |
9088 KB |
Output is correct |
29 |
Correct |
10 ms |
9088 KB |
Output is correct |
30 |
Correct |
12 ms |
9088 KB |
Output is correct |
31 |
Correct |
10 ms |
9088 KB |
Output is correct |
32 |
Correct |
10 ms |
9088 KB |
Output is correct |
33 |
Correct |
10 ms |
9088 KB |
Output is correct |
34 |
Correct |
10 ms |
8960 KB |
Output is correct |
35 |
Correct |
10 ms |
8960 KB |
Output is correct |
36 |
Correct |
11 ms |
9088 KB |
Output is correct |
37 |
Correct |
10 ms |
9088 KB |
Output is correct |
38 |
Correct |
11 ms |
9088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
8960 KB |
Output is correct |
2 |
Correct |
9 ms |
8960 KB |
Output is correct |
3 |
Correct |
10 ms |
8960 KB |
Output is correct |
4 |
Correct |
9 ms |
8960 KB |
Output is correct |
5 |
Correct |
9 ms |
8960 KB |
Output is correct |
6 |
Correct |
9 ms |
8960 KB |
Output is correct |
7 |
Correct |
9 ms |
8960 KB |
Output is correct |
8 |
Correct |
9 ms |
8960 KB |
Output is correct |
9 |
Correct |
10 ms |
8960 KB |
Output is correct |
10 |
Correct |
9 ms |
8960 KB |
Output is correct |
11 |
Correct |
9 ms |
8960 KB |
Output is correct |
12 |
Correct |
10 ms |
8960 KB |
Output is correct |
13 |
Correct |
9 ms |
8960 KB |
Output is correct |
14 |
Correct |
9 ms |
8960 KB |
Output is correct |
15 |
Correct |
9 ms |
8960 KB |
Output is correct |
16 |
Correct |
9 ms |
8960 KB |
Output is correct |
17 |
Correct |
9 ms |
8960 KB |
Output is correct |
18 |
Correct |
10 ms |
8960 KB |
Output is correct |
19 |
Correct |
152 ms |
14840 KB |
Output is correct |
20 |
Correct |
142 ms |
14712 KB |
Output is correct |
21 |
Correct |
155 ms |
15128 KB |
Output is correct |
22 |
Correct |
140 ms |
15600 KB |
Output is correct |
23 |
Correct |
103 ms |
14840 KB |
Output is correct |
24 |
Correct |
80 ms |
14712 KB |
Output is correct |
25 |
Correct |
150 ms |
16760 KB |
Output is correct |
26 |
Correct |
120 ms |
20000 KB |
Output is correct |
27 |
Correct |
202 ms |
20600 KB |
Output is correct |
28 |
Correct |
316 ms |
28408 KB |
Output is correct |
29 |
Correct |
307 ms |
27768 KB |
Output is correct |
30 |
Correct |
198 ms |
20476 KB |
Output is correct |
31 |
Correct |
208 ms |
20600 KB |
Output is correct |
32 |
Correct |
249 ms |
20344 KB |
Output is correct |
33 |
Correct |
276 ms |
19192 KB |
Output is correct |
34 |
Correct |
226 ms |
19320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
8960 KB |
Output is correct |
2 |
Correct |
9 ms |
8960 KB |
Output is correct |
3 |
Correct |
10 ms |
8960 KB |
Output is correct |
4 |
Correct |
9 ms |
8960 KB |
Output is correct |
5 |
Correct |
9 ms |
8960 KB |
Output is correct |
6 |
Correct |
9 ms |
8960 KB |
Output is correct |
7 |
Correct |
9 ms |
8960 KB |
Output is correct |
8 |
Correct |
9 ms |
8960 KB |
Output is correct |
9 |
Correct |
10 ms |
8960 KB |
Output is correct |
10 |
Correct |
9 ms |
8960 KB |
Output is correct |
11 |
Correct |
9 ms |
8960 KB |
Output is correct |
12 |
Correct |
10 ms |
8960 KB |
Output is correct |
13 |
Correct |
9 ms |
8960 KB |
Output is correct |
14 |
Correct |
9 ms |
8960 KB |
Output is correct |
15 |
Correct |
9 ms |
8960 KB |
Output is correct |
16 |
Correct |
9 ms |
8960 KB |
Output is correct |
17 |
Correct |
9 ms |
8960 KB |
Output is correct |
18 |
Correct |
10 ms |
8960 KB |
Output is correct |
19 |
Correct |
9 ms |
8960 KB |
Output is correct |
20 |
Correct |
9 ms |
9008 KB |
Output is correct |
21 |
Correct |
13 ms |
8960 KB |
Output is correct |
22 |
Correct |
11 ms |
8960 KB |
Output is correct |
23 |
Correct |
10 ms |
9088 KB |
Output is correct |
24 |
Correct |
10 ms |
9088 KB |
Output is correct |
25 |
Correct |
10 ms |
9088 KB |
Output is correct |
26 |
Correct |
11 ms |
9080 KB |
Output is correct |
27 |
Correct |
10 ms |
9088 KB |
Output is correct |
28 |
Correct |
11 ms |
9088 KB |
Output is correct |
29 |
Correct |
10 ms |
9088 KB |
Output is correct |
30 |
Correct |
12 ms |
9088 KB |
Output is correct |
31 |
Correct |
10 ms |
9088 KB |
Output is correct |
32 |
Correct |
10 ms |
9088 KB |
Output is correct |
33 |
Correct |
10 ms |
9088 KB |
Output is correct |
34 |
Correct |
10 ms |
8960 KB |
Output is correct |
35 |
Correct |
10 ms |
8960 KB |
Output is correct |
36 |
Correct |
11 ms |
9088 KB |
Output is correct |
37 |
Correct |
10 ms |
9088 KB |
Output is correct |
38 |
Correct |
11 ms |
9088 KB |
Output is correct |
39 |
Correct |
152 ms |
14840 KB |
Output is correct |
40 |
Correct |
142 ms |
14712 KB |
Output is correct |
41 |
Correct |
155 ms |
15128 KB |
Output is correct |
42 |
Correct |
140 ms |
15600 KB |
Output is correct |
43 |
Correct |
103 ms |
14840 KB |
Output is correct |
44 |
Correct |
80 ms |
14712 KB |
Output is correct |
45 |
Correct |
150 ms |
16760 KB |
Output is correct |
46 |
Correct |
120 ms |
20000 KB |
Output is correct |
47 |
Correct |
202 ms |
20600 KB |
Output is correct |
48 |
Correct |
316 ms |
28408 KB |
Output is correct |
49 |
Correct |
307 ms |
27768 KB |
Output is correct |
50 |
Correct |
198 ms |
20476 KB |
Output is correct |
51 |
Correct |
208 ms |
20600 KB |
Output is correct |
52 |
Correct |
249 ms |
20344 KB |
Output is correct |
53 |
Correct |
276 ms |
19192 KB |
Output is correct |
54 |
Correct |
226 ms |
19320 KB |
Output is correct |
55 |
Correct |
19 ms |
9728 KB |
Output is correct |
56 |
Correct |
19 ms |
9728 KB |
Output is correct |
57 |
Correct |
114 ms |
17148 KB |
Output is correct |
58 |
Correct |
49 ms |
16748 KB |
Output is correct |
59 |
Correct |
121 ms |
21292 KB |
Output is correct |
60 |
Correct |
464 ms |
33752 KB |
Output is correct |
61 |
Correct |
219 ms |
23804 KB |
Output is correct |
62 |
Correct |
243 ms |
24316 KB |
Output is correct |
63 |
Correct |
296 ms |
24312 KB |
Output is correct |
64 |
Correct |
488 ms |
26216 KB |
Output is correct |
65 |
Correct |
257 ms |
24312 KB |
Output is correct |
66 |
Correct |
420 ms |
29816 KB |
Output is correct |
67 |
Correct |
134 ms |
25904 KB |
Output is correct |
68 |
Correct |
281 ms |
25328 KB |
Output is correct |
69 |
Correct |
287 ms |
25484 KB |
Output is correct |
70 |
Correct |
277 ms |
24688 KB |
Output is correct |