이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "race.h"
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<P> vp;
typedef vector<vp> vvp;
const int inf=1<<30,MAX=1000001,NMAX=200000;
int res=inf,c[MAX],s[NMAX];
ll m;
bool vis[NMAX];
vp g[NMAX];
int Cdfs(int v,int p){
s[v]=1;
for(auto i:g[v]){
int u=i.first;
if(!vis[u]&&u!=p) s[v]+=Cdfs(u,v);
}
return s[v];
}
P Cdfs1(int v,int p,int S){
P q={inf,0};
int M=0,S_=S-1;
for(auto i:g[v]){
int u=i.first;
if(!vis[u]&&u!=p){
q=min(q,Cdfs1(u,v,S));
M=max(M,s[u]);
S_-=s[u];
}
}
return min(q,{max(M,S_),v});
}
int Cent(int v){
Cdfs(v,-1);
return Cdfs1(v,-1,s[v]).second;
}
int t,len,tp;
void DFS(int v,int par=-1){
if(len>=res||t>m) return;
if(t==m) res=min(res,len);
if(tp==0&&t<m) res=min(res,c[m-t]+len);
if(tp==1) c[t]=min(c[t],len);
if(tp==2) c[t]=inf;
for(auto p:g[v]){
int u=p.first;
if(!vis[u]&&u!=par){
len++;
t+=p.second;
DFS(u,v);
len--;
t-=p.second;
}
}
}
void Rec(int v){
v=Cent(v);
vis[v]=1;
vvp b;
for(auto p:g[v]){
t=p.second,len=1,tp=0;
DFS(p.first);
t=p.second,len=1,tp=1;
DFS(p.first);
}
for(auto p:g[v]){
t=p.second,len=1,tp=2;
DFS(p.first);
}
for(auto p:g[v]){
int u=p.first;
if(!vis[u]) Rec(u);
}
}
int best_path(int N, int K, int H[][2], int L[]){
m=K;
fill(c,c+MAX,inf);
for(int i=0;i<N-1;i++){
int u=H[i][0],v=H[i][1];
g[u].push_back({v,L[i]});
g[v].push_back({u,L[i]});
}
Rec(0);
if(res==inf) res=-1;
return res;
}
# | 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... |