제출 #888402

#제출 시각아이디문제언어결과실행 시간메모리
888402vjudge1경주 (Race) (IOI11_race)C++17
100 / 100
655 ms66896 KiB
#include <bits/stdc++.h>
#include "race.h"
using namespace std;

using LL = long long;
using PII = pair<int, int>;

const int N = 1e6 + 7;
const LL INF = 1e18;

int n, k;
vector<PII> adj[N];
int sz[N];

LL ans = 0;
bool vis[N];
map<LL, LL> best;

int dfs(int u, int p = 0) {
    sz[u] = 1;
    for (auto [v, w] : adj[u])
        if (!vis[v] && v != p) sz[u] += dfs(v, u);
    return sz[u];
}

int centroid(int u, int p, int mx) {
    for (auto [v, w] : adj[u])
        if (!vis[v] && v != p && sz[v] * 2 > mx) return centroid(v, u, mx);
    return u;
}

void go(int u, int p, int depth, int level, bool filling) {
    if (filling) {
        if (best.count(depth)) best[depth] = min(best[depth], 1LL*level);
        else best[depth] = level;
    } else {
        if (best.count(k-depth)) {
            ans = min(ans, best[k - depth] + level);
        }
    }

    for (auto [v, w] : adj[u])
        if (!vis[v] && v != p) go(v, u, depth + w, level + 1 ,filling);
}

void decompose(int u) {
    int mx = dfs(u, u);
    int c = centroid(u, u, mx);
    vis[c] = true;
    best[0] = 0;

    for (auto [v, w] : adj[c]) {
        if (!vis[v]) {
            go(v, c, w, 1, false);
            go(v, c, w, 1, true);
        }
    }

    best.clear();
    for (auto[v,w] : adj[c])
        if (!vis[v]) decompose(v);
}

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 = H[i][0];
        int v = H[i][1];
        int w = L[i];
        adj[u].emplace_back(v,w);
        adj[v].emplace_back(u,w);
    }
    ans = INF;
    decompose(1);
    if(ans > INF/4) 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...