이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "race.h"
#include<bits/stdc++.h>
using namespace std;
vector<set<pair<int64_t,int64_t>>>adj;
int64_t k=0;
vector<int64_t>sub(1e6,0);
vector<int64_t>ans;
vector<int64_t>aux;
int64_t answer = LLONG_MAX;
int64_t kk = -1;
int64_t bookmark = 0;
void dfs (int64_t cur,int64_t par,int64_t cost,int64_t len,int64_t fill){
if (cost>kk)return;
if (fill){
if (aux[cost]<bookmark){
aux[cost] = bookmark;
ans[cost] = len;
}
else if (len<ans[cost]){
ans[cost] = len;
aux[cost] = bookmark;
}
}
else{
if (aux[kk - cost]==bookmark){
if (len + ans[kk - cost]<answer){
answer = min(answer,len + ans[kk-cost]);
}
}
if (cost ==kk){
answer = min(answer,len);
}
}
for (auto x:adj[cur]){
if (x.first!=par){
dfs(x.first,cur,cost + x.second,len + 1,fill);
}
}
}
void getsub(int64_t u,int64_t par){
sub[u]=1;
k++;
for (auto x:adj[u]){
if (x.first!=par){
getsub(x.first,u);
sub[u]+=sub[x.first];
}
}
}
int64_t getcentroid(int64_t u,int64_t par){
for (auto x:adj[u]){
if (x.first!=par){
if (sub[x.first]>k/2){
return getcentroid(x.first,u);
}
}
}
return u;
}
void decomposition(int64_t u,int64_t par){
k = 0;
getsub(u,-1);
int64_t c = getcentroid(u,-1);
bookmark++;
for (auto x:adj[c]){
dfs(x.first,c,x.second,1,0);
dfs(x.first,c,x.second,1,1);
}
for (auto x:adj[c]){
adj[x.first].erase({c,x.second});
//adj[c].erase(x);
decomposition(x.first,c);
}
}
void build(){
decomposition(0,-1);
}
int best_path(int N, int K, int H[][2], int L[])
{
aux.resize(K + 1,0);
ans.resize(K + 1,0);
adj.resize(N + 1);
kk = K;
for (int64_t i = 0;i<N-1;++i){
adj[H[i][0]].insert({H[i][1],L[i]});
adj[H[i][1]].insert({H[i][0],L[i]});
}
build();
if (answer==LLONG_MAX){
return -1;
}
return answer;
}
# | 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... |