제출 #104117

#제출 시각아이디문제언어결과실행 시간메모리
104117rkocharyan경주 (Race) (IOI11_race)C++14
100 / 100
897 ms39476 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

const int N = 2e5 + 7;
const int K = 1e6 + 7;
const int LG = 20;
const int inf = 1e9 + 7;

int n, k;
int sz[N];
bool used[N];
vector <pair <int, int> > g[N];
vector <int> dp(K, inf);
vector <int> kek(K, inf);
vector <int> go;
int ans = inf;

void calc(int v, int pr) {
    sz[v] = 1;
    for (auto to : g[v]) {
        if (to.first != pr && !used[to.first]) {
            calc(to.first, v);
            sz[v] += sz[to.first];
        }
    }
}

int centroid(int v, int pr, int need) {
    for (auto to : g[v]) {
        if (to.first != pr && !used[to.first] && sz[to.first] > need / 2) {
            return centroid(to.first, v, need);
        }
    }
    return v;
}

void dfs(int v, int pr, int h, int cur) {
    if (cur > k) {
        return;
    }
    go.push_back(cur);
    kek[cur] = min(kek[cur], h);
    for (auto to : g[v]) {
        if (to.first != pr && !used[to.first]) {
            dfs(to.first, v, h + 1, cur + to.second);
        }
    }
}

void build(int v, int lg) {
    calc(v, -1);
    v = centroid(v, -1, sz[v]);
    dp[0] = 0;
    kek[0] = 0;
    vector <int> kekos;
    for (auto to : g[v]) {
        if (!used[to.first]) {
            dfs(to.first, v, 1, to.second);
            go.push_back(0);
            for (int x : go) {
                ans = min(ans, kek[x] + dp[k - x]);
            }
            for (int x : go) {
                dp[x] = min(dp[x], kek[x]);
                kek[x] = inf;
            }
            while (!go.empty()) {
                kekos.push_back(go.back());
                go.pop_back();
            }
        }
    }
    for (int x : kekos) {
        dp[x] = inf;
    }
    used[v] = true;
    for (auto to : g[v]) {
        if (!used[to.first]) {
            build(to.first, lg + 1);
        }
    }
}

int best_path(int N, int K, int H[][2], int L[]) {
    n = N, k = K;
    for (int i = 0; i < n - 1; i++) {
        int u, v, l;
        u = H[i][0];
        v = H[i][1];
        l = L[i];
        g[u].push_back({v, l});
        g[v].push_back({u, l});
    }
    build(0, 0);
    return (ans == inf ? -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...