Submission #757485

#TimeUsernameProblemLanguageResultExecution timeMemory
757485hafoRace (IOI11_race)C++14
100 / 100
557 ms99788 KiB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;

template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}

const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 2e5 + 7;
const ll oo = 1e9 + 69;

int n, k, w[maxn], u, v, ed[maxn][2], dep[maxn], ans;
ll f[maxn];
vector<pa> g[maxn];
unordered_map<ll, int> mp[maxn];

void dfs(int u, int par) {
    for(auto e:g[u]) {
        int v = e.fi, w = e.se;
        if(v == par) continue;
        f[v] = f[u] + w;
        dep[v] = dep[u] + 1;
        dfs(v, u);
    }
}

void dfs2(int u, int par) {
    mp[u][f[u]] = dep[u];
    if(f[u] == k) mini(ans, dep[u]);
    for(auto e:g[u]) {
        int v = e.fi;
        if(v == par) continue;
        dfs2(v, u);
        if(Size(mp[u]) < Size(mp[v])) {
            swap(mp[u], mp[v]);
        }

        for(auto e:mp[v]) {
            ll rem = k + 2LL * f[u] - e.fi;
            if(mp[u].count(rem)) mini(ans, e.se + mp[u][rem] - 2 * dep[u]);
        }
        for(auto e:mp[v]) {
            if(!mp[u].count(e.fi)) mp[u][e.fi] = e.se;
            else mini(mp[u][e.fi], e.se);
        }
    }
}

int best_path(int n, int len, int ed[][2], int w[]) {
//    cin>>n>>k;
//    for(int i = 0; i < n - 1; i++) {
//        cin>>ed[i][0]>>ed[i][1];
//    }
    k = len;
    for(int i = 0; i < n - 1; i++) {
//        cin>>w[i];
        u = ed[i][0] + 1, v = ed[i][1] + 1;
        g[u].pb({v, w[i]});
        g[v].pb({u, w[i]});
    }

    ans = oo;
    dfs(1, 0);
    dfs2(1, 0);
    return (ans == oo ? -1: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...