# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1091807 | Luca1234 | Race (IOI11_race) | C++17 | 0 ms | 0 KiB |
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;
const int nmax=2e5+3;
vector <pair <int, int> > v[nmax];
map <int, int> m[nmax];
int k, ans=nmax;
void dfs(int nod, int parent, int d, int h)
{
m[nod][d]=h;
for(auto [i, cost]:v[nod])
{
if(i!=parent)
{
dfs(i, nod, d+cost, h+1);
if(m[i].size()>m[nod].size) swap(m[i], m[nod]);
for(auto [dist, he]:m[i])
{
int caut=k-(dist+2*d);
if(m[nod].count(caut))
{
ans=min(ans, he+m[nod][caut]-2*h);
}
}
for(auto [dist, he]:m[i])
{
if(m[nod].count(dist))
{
m[nod][dist]=min(m[nod][dist], he);
}
else m[nod][dist]=he;
}
}
}
}
long long 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];
v[a].push_back({b l[i]});
v[b].push_back({a, l[i]});
}
dfs(0, -1, 0, 1);
if(ans==nmax) return -1;
return ans;
}