Submission #395992

#TimeUsernameProblemLanguageResultExecution timeMemory
395992Jarif_RahmanRace (IOI11_race)C++17
100 / 100
494 ms76116 KiB
#include "race.h"
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
const ll inf = 1e18;
 
ll k;
vector<set<pair<ll, int>>> s;
vector<vector<pair<int, ll>>> v;
ll ans = 1e18;
 
void dfs(int nd, int ss, ll dis, int depth){
    for(auto [x, w]: v[nd]) if(x != ss) dfs(x, nd, dis+w, depth+1);
    int mx = -1, in = -1;
    for(auto [x, w]: v[nd]) if(x != ss){
        if((int)s[x].size() > mx){
            mx = s[x].size();
            in = x;
        }
    }
    if(in != -1) swap(s[nd], s[in]);
    auto it = s[nd].lower_bound(make_pair(k+dis, 0));
    if(it != s[nd].end() && it->f == k+dis) ans = min(ans, (ll)it->sc - depth);
    for(auto [x, w]: v[nd]) if(x != ss){
        for(auto [d, dpt]: s[x]){
            dpt-=depth;
            d-=dis;
            ll dd = k-d;
            if(dd == 0){
                ans = min(ans, (ll)dpt);
            }
            else{
                auto it = s[nd].lower_bound(make_pair(dd+dis, 0));
                if(it != s[nd].end() && it->f == dd+dis){
                    ans = min(ans, dpt+(ll)(it->sc)-depth);
                }
            }
        }
        for(auto p: s[x]) s[nd].insert(p);
        s[x].clear();
    }
    s[nd].insert({dis, depth});
}
 
int best_path(int n, int k, int edges[][2], int w[]){
    ::k = k;
    v.resize(n);
    s.resize(n);
    for(int i = 0; i < n-1; i++){
        v[edges[i][0]].pb({edges[i][1], w[i]});
        v[edges[i][1]].pb({edges[i][0], w[i]});
    }
    dfs(0, -1, 0LL, 0);
    if(ans == inf) ans = -1;
    return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...