Submission #978661

#TimeUsernameProblemLanguageResultExecution timeMemory
978661LOLOLORace (IOI11_race)C++17
100 / 100
410 ms165512 KiB
#include <bits/stdc++.h>
#define ll long long
using namespace std;
 
#define           f     first
#define           s     second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (int)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())
 
const int N = 1e6 + 10;
vector <pair <int, int>> ed[N];
ll d1[N], d2[N], k;
ll ans = 1e9;
map <ll, int> mp[N];

// x + y - 2 * v = k
// x + y = k + 2 * v

void dfs(int u, int v) {
    mp[u][d1[u]] = d2[u];

    ll tot = k + 2 * d1[u];
    for (auto x : ed[u]) {
        if (x.f == v)
            continue;

        d2[x.f] = d2[u] + 1;
        d1[x.f] = d1[u] + x.s;
        dfs(x.f, u);

        if (sz(mp[u]) < sz(mp[x.f])) {
            swap(mp[u], mp[x.f]);
        }

        for (auto el : mp[x.f]) {
            auto it = mp[u].find(tot - el.f);
            if (it != mp[u].end()) {
                ans = min(ans, it -> s + el.s - d2[u] * 2);
            }
        }

        for (auto el : mp[x.f]) {
            if (mp[u].find(el.f) != mp[u].end()) {
                mp[u][el.f] = min(mp[u][el.f], el.s);
            } else {
                mp[u][el.f] = el.s;
            }
        }
    }
}

int best_path(int n, int len, int H[][2], int L[]) {
    k = len;
    for (int i = 0; i < n - 1; i++) {
        ed[H[i][0]].pb({H[i][1], L[i]});
        ed[H[i][1]].pb({H[i][0], L[i]});
    }

    dfs(0, 0);
    if (ans > n)
        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...