# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
700196 | mnbvcxz123 | Race (IOI11_race) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using ar=array<int,2>;
int n,k,res=1e6;
vector<ar>g[200000];
int sz[200000];
bool used[200000];
map<ll,int>mp;
void siz(int v, int e){
sz[v]=1;
for(const auto&[i,w]:g[v])
if(i!=e and !used[i])
siz(i,v),sz[v]+=sz[i];
}
int get(int v, int e, int des){
for(const auto&[i,w]:g[v])
if(i!=e and !used[i] and sz[i]>des)
return get(i,v,des);
return v;
}
void help(int v, int e, ll km, int d, bool fill){
if(km>k) return;
if(fill){
if(mp[km]==0)
mp[km]=d;
else
mp[k-km]=min(mp[k-km],d);
}
else{
if(km==k)
res=min(res,d);
else if(mp[k-km]!=0)
res=min(res,d+mp[k-km]);
}
for(const auto&[i,w]:g[v])
if(!used[i])
help(i,v,km+w,d+1,fill);
}
void decomp(int v=0){
siz(v,-1);
int c=get(v,-1,sz[v]>>1);
used[c]=1;
mp[c]=0;
for(const auto&[i,w]:g[c])
if(!used[i]){
help(i,c,w,1,0);
help(i,c,w,1,1);
}
mp.clear();
for(const auto&[i,w]:g[c])
if(!used[i])
decomp(i);
}
int best_path(int N, int K, int H[][2],int L[]){
n=N;
k=K;
for(int i=0;i<n-1;++i){
g[H[i][0]].push_back({H[i][1],L[i]});
g[H[i][1]].push_back({H[i][0],L[i]});
}
decomp();
if(res==1e6) return -1;
return res;
}
int H[200000][2];
int L[200000];
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int N,K;
cin>>N>>K;
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];
res=best_path(N,K,H,L);
cout<<res;
}