# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
441219 | julian33 | Race (IOI11_race) | C++14 | 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;
#ifdef LOCAL
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {
cerr<<vars<<" = ";
string delim="";
(...,(cerr<<delim<<values,delim=", "));
cerr<<"\n";
}
#else
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {}
#endif
#define pb push_back
#define sz(x) (int)(x.size())
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template<typename T> inline void maxa(T& a,T b){a=max(a,b);}
template<typename T> inline void mina(T& a,T b){a=min(a,b);}
const int mxN=2e5+5;
vector<pii> graph[mxN];
int seen[mxN],sub[mxN],ans=1e9,K;
unordered_map<int,int> mp;
void dfs(int at,int p){
sub[at]=1;
for(auto &[i,w]:graph[at]){
if(i==p || seen[i])
continue;
dfs(i,at);
sub[at]+=sub[i];
}
}
int centroid(int at,int p,int s){
for(auto &[i,w]:graph[at]){
if(i==p || seen[i])
continue;
if(sub[i]>s/2)
return centroid(i,at,s);
}
return at;
}
void solve(int at,int p,int len,int filling,int h){
if(len>K)
return;
if(filling){
if(mp.count(len))
mina(mp[len],h);
else
mp[len]=h;
} else{
if(mp.count(K-len)){
mina(ans,mp[K-len]+h);
}
}
for(auto &[i,w]:graph[at]){
if(i==p || seen[i])
continue;
solve(i,at,len+w,filling,h+1);
}
}
void build(int at){
dfs(at,-1);
int s=sub[at];
int cent=centroid(at,-1,s);
mp.clear();
for(auto &[i,w]:graph[cent]){
solve(i,cent,w,0,1);
solve(i,cent,w,1,1);
}
seen[cent]=1;
for(auto &[i,w]:graph[cent]){
if(seen[i])
continue;
build(i);
}
}
int best_path(int n,int k,vector<vector<int>> h,vector<int> l){
K=k;
for(int i=0;i<n-1;i++){
graph[h[0][i]].pb({h[1][i],l[i]});
graph[h[1][i]].pb({h[0][i],l[i]});
}
build(0);
return (ans==1e9?-1:ans);
}