이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define pb emplace_back
const int mxN = 2e5+10;
const int INF = (int)1e9;
vector<pair<int,int>> adj[mxN];
int sz[mxN],N,K,ans=INF;
bool ok[mxN];
vector<int> mnDist((int)1e6+50,INF);
void dfs1(int node,int par){
sz[node]=1;
for(auto it:adj[node]){
if(it.first==par||ok[it.first]) continue;
dfs1(it.first,node);
sz[node]+=sz[it.first];
}
}
int find(int node,int par){
for(auto it:adj[node]){
if(it.first==par||ok[it.first]) continue;
if(sz[it.first]>=((N+1)/2)){
return find(it.first,node);
}
}
return node;
}
void dfs2(int src,int par,int dist,int h,bool f){
if(dist>K) return;
if(f){
mnDist[dist]=min(mnDist[dist],h);
}else{
ans=min(ans,mnDist[K-dist]+h);
}
for(auto it:adj[src]){
if(!ok[it.first]&&it.first!=par){
dfs2(it.first,src,dist+it.second,h+1,f);
}
}
}
void res(int u,int p,int dist){
if(dist>K) return;
mnDist[dist]=INF;
for(auto it:adj[u]){
if(it.first!=p&&!ok[it.first]){
res(it.first,u,dist+it.second);
}
}
}
void decompose(int u){
dfs1(u,-1);
int cen=find(u,-1);
mnDist[0]=0;
for(auto it:adj[u]){
if(!ok[it.first]){
dfs2(it.first,u,it.second,1,1);
dfs2(it.first,u,it.second,1,0);
}
}
res(u,-1,0);
ok[u]=1;
for(auto it:adj[u]){
if(!ok[it.first]){
decompose(it.first);
}
}
}
int best_path(int n,int k,int h[][2],int l[]){
for(int i=0;i<n-1;++i){
adj[h[i][0]].pb(h[i][1],l[i]);
adj[h[i][1]].pb(h[i][0],l[i]);
}
N=n,K=k;
decompose(0);
return ans>=INF?-1:ans;
}
컴파일 시 표준 에러 (stderr) 메시지
race.cpp: In function 'void decompose(int)':
race.cpp:59:9: warning: unused variable 'cen' [-Wunused-variable]
59 | int cen=find(u,-1);
| ^~~
# | 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... |