이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "race.h"
#include<bits/stdc++.h>
using namespace std;
#define INF 300000
#define pb push_back
#define ss second
#define ff first
int m,n,sz[200005],cnt[1000005],i,j,x,y,k,p,D[200005],ans;
bool R[200005];
vector< pair<int,int> > v[200005];
vector<int> remover;
int sizer(int node, int pre=-1){
sz[node]=1;
for(pair<int,int> i:v[node])
if(i.ff!=pre && !R[i.ff]) sz[node]+=sizer(i.ff,node);
return sz[node];
}
int get_centroid(int node, int pre=-1){
for(pair<int,int> i:v[node])
if(i.ff!=pre && !R[i.ff] && sz[i.ff]>n/2) return get_centroid(i.ff,node);
return node;
}
void dfs(int node, int pre, int depth){
if(D[node]>k || depth > ans) return;
cnt[D[node]]=min(cnt[D[node]],depth);
remover.pb(D[node]);
for(pair<int,int> i : v[node])
if(i.ff!=pre && !R[i.ff]){
D[i.ff]=D[node]+i.ss;
dfs(i.ff,node,depth+1);
}
}
void shaa(int node, int pre, int depth){
if(D[node]>k || depth > ans) return;
ans=min(ans,depth+cnt[k-D[node]]);
for(pair<int,int> i : v[node])
if(i.ff!=pre && !R[i.ff]) shaa(i.ff,node,depth+1);
}
void solve(int node){
n=sizer(node);
int C=get_centroid(node);
R[C]=true;
cnt[0]=0;
for(pair<int,int> child : v[C])
if(!R[child.ff]){
D[child.ff]=child.ss;
dfs(child.ff,C,1);
shaa(child.ff,C,1);
}
for(int i : remover) cnt[i]=INF;
remover.clear();
for(pair<int,int> child : v[C])
if(!R[child.ff]) solve(child.ff);
}
int best_path(int N, int K, int H[][2], int L[])
{
k=K;
for(int i=0;i<=k;i++) cnt[i]=INF;
for(int i=0;i<N-1;i++){
v[H[i][0]].pb({H[i][1],L[i]});
v[H[i][1]].pb({H[i][0],L[i]});
}
ans=INF;
solve(1);
if(ans>N) return -1;
else return 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... |