# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1046151 | I_FloPPed21 | Race (IOI11_race) | C++14 | 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;
map<long long,int> mp[200005] ;
vector<int> adj[200005];
long long h[200005][2] , v[200005],target = 0 ,sum[200005],height[200005],ans = 1e9;
void precomp(int nod , int p , int hd)
{
sum[nod] += v[nod];
height[nod] = hd ;
for ( auto u : adj[nod] )
{
if ( u != p )
{
sum[u] += sum[nod];
precomp(u,nod,hd + 1);
}
}
}
void small(int nod ,int p )
{
int real_target = target + 2 * sum[nod];
mp[nod][sum[nod]] = height[nod];
for ( auto u : adj[nod] )
{
if ( u != p )
{
small(u,nod);
}
}
for ( auto u : adj[nod] )
{
if( u != p )
{
if( mp[u].size() > mp[nod].size())
swap(mp[nod],mp[u]);
for ( auto u : mp[u] )
{
long long real = target - u.second ;
if ( mp[nod][real] != 0 )
{
ans = min ( ans , u.first + mp[nod][real] );
}
if( mp[nod][u.first] == 0 )
mp[nod][u.first] = u .second;
else
mp[nod][u.first] = min(mp[nod][u.first],u.second);
}
}
}
}
int best_path(int n , int k , long long h[][2] , long long costs [] )
{
target =k ;
for ( int i = 1; i < n ; i ++ )
{
adj[h[i][1]].push_back(h[i][0]);
adj[h[i][0]].push_back(h[i][1]);
}
for ( int i = 1; i <= n ; i ++)
v[i] =costs[i];
precomp(1,0,1);
small(1,0);
if( ans == 1e9)
return -1;
else
return ans ;
}