Submission #946928

#TimeUsernameProblemLanguageResultExecution timeMemory
946928EsgeerRace (IOI11_race)C++17
100 / 100
386 ms105356 KiB
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <unistd.h>
using namespace __gnu_pbds;
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#ifndef Local
    #pragma GCC optimize("O3,unroll-loops")
    #include "race.h"
#endif
#define int long long
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vpi vector<pii>
#define vvpi vector<vpi>
#define vb vector<bool>
#define vvb vector<vb>
#define endl '\n'
#define sp << " " <<
#define F(i, s, n) for(int i = s; i < n; i++)
#define pb push_back
#define fi first
#define se second

 
int inf = 1e18;
const int N = 2e5+5;
int32_t l[N];
int32_t h[N][2];

vpi adj[N];
map<int, int> mp[N];
int k;

int ans = inf;
void dfs(int node, int par, int depth, int depl) {
    mp[node][depl] = depth;

    int totK = depl * 2 + k;
    for(pii go : adj[node]) if(go.fi != par) {
        dfs(go.fi, node, depth+1, depl+go.se);
        if(mp[go.fi].size() > mp[node].size()) swap(mp[go.fi], mp[node]);
        for(pii el : mp[go.fi]) {
            if(mp[node].find(totK - el.fi) != mp[node].end()) {
                ans = min(ans, mp[node][totK - el.fi] + el.se - depth*2);
            }
        }
        for(pii el : mp[go.fi]) {
            if(mp[node].find(el.fi) != mp[node].end()) mp[node][el.fi] = min(mp[node][el.fi], el.se);
            else mp[node][el.fi] = el.se;
        }
    }
}

int32_t best_path(int32_t n, int32_t k, int32_t h[][2], int32_t l[]) {
    F(i, 0, n-1) {
        int u = h[i][0], v = h[i][1];
        adj[u].pb({v, l[i]});
        adj[v].pb({u, l[i]});
    }

    ::k = k;
    dfs(0, -1, 0, 0);
    if(ans < 1e16) return ans;
    else return -1;
}

#ifdef Local
void solve() {
    int32_t n, k;
    cin >> n >> k;

    F(i, 0, n-1) cin >> h[i][0] >> h[i][1];
    F(i, 0, n-1) cin >> l[i];

    cout << best_path(n, k, h, l) << endl;
}
 
void setIO() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    #ifdef Local
        freopen("local.in", "r", stdin);
        freopen("local.out", "w", stdout);
    #else 
        // freopen("disrupt.in","r",stdin);
        // freopen("disrupt.out","w",stdout);
    #endif
}
signed main() {
    setIO();
    int t = 1;
    //cin >> t;
    while(t--) solve();
}
#endif
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...