이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "race.h"
#include <cstdio>
#include <set>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
typedef pair<int,int> ii;
vector<ii> al[200005];
int offset[200005];
int offset2[200005];
set<ii> rope[200005];
int ans=1000000;
int k;
void dfs(int i,int p){
rope[i].insert(ii(offset[i],offset2[i]));
int target;
set<ii>::iterator sit;
for (auto it:al[i]){
if (it.first==p) continue;
offset[it.first]=offset[i]+it.second;
offset2[it.first]=offset2[i]+1;
dfs(it.first,i);
if (rope[it.first].size()>rope[i].size()) swap(rope[it.first],rope[i]);
for (auto x:rope[it.first]){
target=k-x.first+offset[i]*2;
sit=rope[i].lower_bound(ii(target,-1));
if (sit==rope[i].end() || (*sit).first!=target) continue;
ans=min(ans,(*sit).second+x.second-offset2[i]*2);
}
rope[i].insert(rope[it.first].begin(),rope[it.first].end());
}
}
int best_path(int n, int K, int h[][2], int l[]){
k=K;
for (int x=0;x<n-1;x++){
al[h[x][0]].push_back(ii(h[x][1],l[x]));
al[h[x][1]].push_back(ii(h[x][0],l[x]));
}
dfs(0,-1);
return (ans==1000000)?-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... |