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 "race.h"
#include "iostream"
#include "algorithm"
#include "iomanip"
#include "vector"
#include "set"
#include "sstream"
#include "fstream"
#include "cstring"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "bitset"
#include "cassert"
#include "deque"
#define sp <<" "
#define el <<"\n"
#define S second
#define F first
#define all(ar) ar.begin(),ar.end()
#define pb push_back
#define pii pair<ll,ll>
#define vvp vector<vector<pii>>
using namespace std;
using ll = long long;
const ll mod = 1000000007;
const ll si = 200005;
vvp g;
ll n,mxc,cnt[si],ans = mod,k;
bitset<si> ded;
int H[si][2],L[si],N,K;
void dfs(ll v,ll p,vector<ll> &sz){
sz[v] = 1;
for(pii r: g[v]){
if(r.F == p || ded[r.F]) continue;
dfs(r.F,v,sz);
sz[v] += sz[r.F];
}
return;
}
ll f_cen(ll v,ll p,ll lim,vector<ll> &sz){
for(pii r: g[v]){
if(r.F ==p || ded[r.F]) continue;
if(sz[r.F] > lim) return f_cen(r.F,v,lim,sz);
}
return v;
}
void count(ll v,ll p,ll cost,ll dep,bool ad){
if(cost > k) return;
mxc = max(cost,mxc);
if(ad) cnt[cost] = min(cnt[cost],dep);
else ans = min(ans,dep + cnt[k - cost]);
for(pii r: g[v]){
if(r.F == p || ded[r.F]) continue;
count(r.F,v,cost + r.S,dep+1,ad);
}
return;
}
void dec(ll v){
static vector<ll> sz(n+1);
dfs(v,0,sz);
ll lim = (sz[v]/2);
ll cen = f_cen(v,0,lim,sz);
mxc = 0;
cnt[0] = 0;
ded[cen] = 1;
for(pii r: g[cen]){
if(ded[r.F]) continue;
count(r.F,cen,r.S,1,0);
count(r.F,cen,r.S,1,1);
}
for(ll i = 0; i <= mxc; i++) cnt[i] = mod;
for(pii r: g[cen]){
if(ded[r.F]) continue;
dec(r.F);
}
return;
}
int best_path(int N, int K, int H[][2], int L[])
{
n = N;
k = K;
g.assign(n+1,{});
for(int i = 0; i < n - 1; i++){
int a = H[i][0], b = H[i][1], c = L[i];
g[a].pb({b,c});
g[b].pb({a,c});
}
fill(cnt,cnt+n,mod);
dec(0);
return (ans == mod? -1 : ans);
}
# | 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... |