이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "race.h"
#include <bits/stdc++.h>
#define sp <<" "
#define el <<"\n"
#define S second
#define F first
#define pb push_back
#define all(ar) ar.begin(),ar.end()
#define pii pair<ll,ll>
using namespace std;
using ll = long long;
using ld = long double;
const ll mxr = 200005;
const ll big = 2000000999;
ll n,k,path[1000011],mac,ans = big;
vector<vector<pii>> g;
bitset<mxr> ded;
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 fcen(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 fcen(r.F,v,lim,sz);
}
return v;
}
void fil(ll v,ll p,ll cost,ll dep,bool ad){
if(cost > k) return;
mac = max(mac,cost);
if(ad) path[cost] = min(path[cost],dep);
else ans = min(ans,dep + path[cost - k]);
for(pii r: g[v]){
if(r.F == p || ded[r.F]) continue;
fil(v,p,cost + r.S,dep+1,ad);
}
return;
}
void decompose(ll v){
static vector<ll> sz(n+1);
dfs(v,-1,sz);
ll lim = sz[v];
ll cen = fcen(v,-1,lim,sz);
ded[cen] = 1;
mac = 0;
path[0] = 0;
for(pii r: g[cen]){
fil(r.F,cen,r.S,1,0);
fil(r.F,cen,r.S,1,1);
}
for(ll i = 0; i <= mac; i++) path[i] = big;
for(pii r: g[cen]){
if(!ded[r.F]) decompose(r.F);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
n = N;
k = K;
g.assign(n,{});
for(ll i = 1; i < n; i++){
ll a = H[i][0], b = H[i][1], c = L[i];
g[a].pb({b,c});
g[b].pb({a,c});
}
fill(path,path + 1000010,big);
decompose(0);
return ans == big ? -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... |