This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define MAX 205000
typedef std::pair<int,int> pii;
int NA,KA;
std::vector<pii> con[MAX];
std::map<int,long long> mapa[MAX];
int prof[MAX];
long long count=1e6;
void merge(int a,int b) {
if(mapa[a].size()<mapa[b].size())std::swap(mapa[a],mapa[b]);
for(auto&x:mapa[b]){
{
auto it = mapa[a].find(x.first+KA);
if(it!=mapa[a].end())count=std::min(count,it->second+x.second-(2*prof[a]));
}
{
auto it = mapa[a].find(x.first-KA);
if(it!=mapa[a].end())count=std::min(count,it->second+x.second-(2*prof[a]));
}
}
for(auto&x:mapa[b]){
auto it = mapa[a].find(x.first);
if(it==mapa[a].end())
mapa[a][x.first]=x.second;
else mapa[a][x.first]=std::min(x.second,mapa[a][x.first]);
}
}
void dfs(int pos,int prev,int dist,int depth=0) {
mapa[pos][dist]=depth;
prof[pos]=depth;
for(auto&x:con[pos]){
if(prev==x.first)continue;
dfs(x.first,pos,dist+x.second,depth+1);
merge(pos,x.first);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
for(auto&x:mapa)x.clear();
for(auto&x:con)x.clear();
NA=N;KA=K;
for(int i=0;i!=N-1;++i){
int a,b,c;
a=H[i][0],b=H[i][1],c=L[i];
con[a].push_back({b,c});
con[b].push_back({a,c});
}
dfs(0,-1,0);
if(!count)count=-1;
return count;
}
# | 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... |