Submission #1139988

#TimeUsernameProblemLanguageResultExecution timeMemory
1139988AmirMakaMRace (IOI11_race)C++20
100 / 100
320 ms104244 KiB
#include <bits/stdc++.h>
#include "race.h"
using namespace std;
#define ull unsigned long long
#define ll long long
#define ld long double
#define pb push_back
#define f first
#define s second 
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define pii pair<int,int> 
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define pdd pair<double,double>
#define mp make_pair   
#define AmirMakaM ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
// solve it
const ull SEED = chrono::steady_clock::now().time_since_epoch().count();
mt19937_64 mrand(SEED);
ull rnd(ull x = ~(0ull)) {return mrand() % x;} 
const ll MOD = 998244353;
const ll INF = 1e16+20;
const int inf = 1e9 + 7;
const ll N = 2e5+5;
const ll M = 2e3+1;
const double pi = 2*acos(0.0);
const int dx[] = {1,-1,0,0}, dy[] = {0,0,1,-1};

int n, k;
vector<pair<int,ll>> g[N];
map<int,ll> t[N];
ll d[N], s[N], ans;

void calc(int v, int pr = -1, int h = 1, ll c = 0) {
    d[v] = h, s[v] = c, t[v][c] = h;
    for(auto it:g[v]) {
        int to = it.f;
        ll w = it.s;
        if(to == pr) continue;
        calc(to,v,h+1,c+w);
    }
}

void dfs(int v, int pr = -1) {
    ll need = k + 2*s[v];
    for(auto it:g[v]) {
        int to = it.f;
        ll w = it.s;
        if(to == pr) continue;
        dfs(to,v);
        if(sz(t[to]) > sz(t[v])) swap(t[to],t[v]);

        for(auto jt:t[to]) {
            if(t[v].find(need-jt.f) != t[v].end()) {
                ans = min(ans,jt.s+t[v][need-jt.f]-2*d[v]);
            } 
        }

        for(auto jt:t[to]) {
            if(t[v].find(jt.f) == t[v].end()) {
                t[v].insert(jt);
            } else {
                t[v][jt.f] = min(t[v][jt.f],jt.s);
            }
        }
    }
}

int best_path(int N, int K, int h[][2], int l[]) {
    n = N, k = K;
    for(int i=0; i<n-1; i++) {
        g[h[i][0]+1].pb({h[i][1]+1,l[i]});
        g[h[i][1]+1].pb({h[i][0]+1,l[i]});
    }

    calc(1);
    ans = n+1;
    dfs(1);
    return (ans == n+1 ? -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...