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>
using namespace std;
vector<pair<int,int>>g[200001];
vector<int>v(200001),sum(200001),sz(200001);
map<long long int,int>*m[200001];
int k,res=10000000;
void dfs(int a,int b,int dep,long long suma)
{
v[a]=dep;
sum[a]=suma;
sz[a]=1;
for(auto p:g[a])
{
if(p.first!=b)
{
dfs(p.first,a,dep+1,suma+p.second);
sz[a]+=sz[p.first];
}
}
}
void findres(int a,int b)
{
int bigc=-1,bigck=-1;
for(auto p:g[a])
{
if(p.first!=b)
{
if(sz[p.first]>bigck)
{
bigc=p.first;
bigck=sz[p.first];
}
findres(p.first,a);
}
}
if(bigc==-1)
{
m[a]= new map<long long int,int>();
}
else
{
m[a]=m[bigc];
}
(*m[a])[sum[a]]=v[a];
int t=2*sum[a]+k;
for(auto p:g[a])
{
if(p.first!=bigc&&p.first!=b)
{
for(auto it:*m[p.first])
{
if((*m[a]).find(t-it.first)!=(*m[a]).end())
{
res=min(res,it.second+(*m[a])[t-it.first]-2*v[a]);
}
}
for(auto it:*m[p.first])
{
if((*m[a]).find(it.first)==(*m[a]).end())
{
(*m[a])[it.first]=it.second;
}
else
{
(*m[a])[it.first]=min(it.second,(*m[a])[it.first]);
}
}
}
}
if((*m[a]).find(sum[a]+k)!=(*m[a]).end())
{
res=min(res,(*m[a])[sum[a]+k]-v[a]);
}
}
int best_path(int N,int K,int H[][2],int L[])
{
k=K;
for(int i=0;i<N-1;i++)
{
int a=H[i][0],b=H[i][1],c=L[i];
g[a].push_back({b,c});
g[b].push_back({a,c});
}
dfs(0,0,0,0);
findres(0,0);
if(res==10000000)
{
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... |