Submission #1046263

#TimeUsernameProblemLanguageResultExecution timeMemory
1046263I_FloPPed21Race (IOI11_race)C++14
100 / 100
301 ms119124 KiB
#include <bits/stdc++.h>

using namespace std;
map<int,long long> mp[300005] ;
vector<pair<int,long long>> adj[300005];
long long target = 0,sum[300005],height[300005],ans = 1e9,n,k;

void precomp(int nod, int p, int hd)
{
    height[nod] = hd ;
    for ( auto u : adj[nod] )
    {
        if ( u.first != p )
        {
            sum[u.first] += sum[nod]  + u.second;
            precomp(u.first,nod,hd + 1);
        }
    }
}

void small(int nod,int p )
{
    mp[nod][sum[nod]] = height[nod];
    long long real_target = target + 2 * sum[nod];
    for ( auto u : adj[nod] )
    {
        if ( u.first != p )
        {
            small(u.first,nod);
        }
    }

    for ( auto u : adj[nod] )
    {
        if( u.first != p )
        {
            if( mp[u.first].size() > mp[nod].size())
                swap(mp[nod],mp[u.first]);

            for ( auto k : mp[u.first] )
            {
                long long real = real_target - k.first ;

                if ( mp[nod].find(real) != mp[nod].end())
                {
                    //cout << nod << " " << u.first << " " << k.first << " " << k.second << '\n';
                    ans = min ( ans, k.second + mp[nod][real] - 2 * height[nod]);
                }


            }

            for ( auto k : mp[u.first])
            {
                if( mp[nod].find(k.first) == mp[nod].end() )
                    mp[nod][k.first] = k .second;
                else
                    mp[nod][k.first] = min(mp[nod][k.first],k.second);

            }
        }
    }

    /*if( nod == 6)
    {
        cout << mp[nod][real_target]<< '\n';
    }*/
}
int best_path(int N, int K, int H[][2], int L [] )
{
    if( K == 1 )
    {
        return 0 ;
    }
    target =K ;

    for ( int i = 0; i < N - 1  ; i ++ )
    {
        adj[H[i][1]].push_back({H[i][0],L[i]});
        adj[H[i][0]].push_back({H[i][1],L[i]});
    }


    precomp(0,-1,1);
    small(0,-1);
    if ( ans == 1e9 )
        return -1;
    else
        return ans;
}
//int v[200005];
/*int main()
{
   cin >> n >> k;
   for ( int i = 1; i < n ; i ++ )
   {
       cin >> h[i][0] >> h[i][1] >> v[i] ;
   }

   cout << best_path(n,k,h,v);

   return 0;
}*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...