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;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<x.F<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
#include "race.h"
const int N=1e6+99;
int n,k,cent,ans=N,sz[N],vis[N],res[N];
int a[N][2],b[N];
vector<int> alh;
vector<pair<int,int>> vec,g[N];
void dfs1(int u,int p,int n){
sz[u]=1;
for(auto v : g[u]){
if(p==v.F || vis[v.F]) continue ;
dfs1(v.F,u,n);
sz[u]+=sz[v.F];
}
if(2*sz[u]>=n && cent==-1) cent=u;
}
void dfs2(int u,int p,int h1,int h2){
sz[u]=1;
if(h2<=k){
minm(ans,h1+res[k-h2]);
alh.pb(h2);
vec.pb({h2,h1});
}
for(auto v : g[u]){
if(p==v.F || vis[v.F]) continue ;
dfs2(v.F,u,h1+1,h2+v.S);
sz[u]+=v.F;
}
}
void solve(int rt,int n){
cent=-1;
dfs1(rt,rt,n);
//eror(cent);
res[0]=0;
alh.clear();
for(auto v : g[cent]){
if(vis[v.F]) continue ;
//erorp(v);
vec.clear();
dfs2(v.F,rt,1,v.S);
for(auto p : vec){
//erorp(p);
minm(res[p.F],p.S);
}
}
for(auto x : alh) res[x]=N;
vis[cent]=1;
for(auto v : g[cent]){
if(vis[v.F]) continue ;
solve(v.F,sz[v.F]);
}
}
int best_path(int _n, int _k,int H[][2],int L[]){
fill(res,res+N,N);
n=_n,k=_k;
f(i,0,n-1){
int u=H[i][0],v=H[i][1],w=L[i];
g[u].pb({v,w});
g[v].pb({u,w});
}
solve(1,n);
if(ans==N) return -1;
return ans;
}
/*
int32_t main(){
int n,k;
cin>>n>>k;
f(i,0,n-1) cin>>a[i][0]>>a[i][1];
f(i,0,n-1) cin>>b[i];
cout<<best_path(n,k,a,b);
}
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |