제출 #492833

#제출 시각아이디문제언어결과실행 시간메모리
492833yt1234Race (IOI11_race)C++17
100 / 100
542 ms103112 KiB
#include<bits/stdc++.h>
using namespace std;

vector<vector<pair<int, int>>> adj(200000);
vector<map<int64_t, int>> mp(200000);
int offsetw[200000];
int offsetl[200000];
int k;
int ans = INT_MAX;

void dfs(int c, int p, int w = 0) {
    mp[c][0] = 0;
    offsetl[c] = 0;
    offsetw[c] = 0;
    for (auto &i : adj[c]) {
        if (i.first == p) continue;
        int x = i.first;
        dfs(x, c, i.second);
        if (mp[c].size() < mp[x].size()) {
            swap(mp[x], mp[c]);
            swap(offsetl[c], offsetl[x]);
            swap(offsetw[c], offsetw[x]);
        }
        for (auto &j : mp[x]) {
            auto it = mp[c].find(k - (j.first + offsetw[x]) - offsetw[c]);
            if (it != mp[c].end()) ans = min(ans, j.second + it->second + 1 + offsetl[c] + offsetl[x]);
        }
        for (auto &j : mp[x]) {
            auto it = mp[c].find(j.first + offsetw[x] - offsetw[c]);
            if (it != mp[c].end()) {
                it->second = min(it->second, j.second + offsetl[x] - offsetl[c]);
            }
            else {
                mp[c][j.first + offsetw[x] - offsetw[c]] = j.second + offsetl[x] - offsetl[c];
            }
        }
    }
    offsetl[c]++;
    offsetw[c] += w;
}

int best_path(int N, int K, int H[][2], int L[]) {
    for (int i = 0; i < N - 1; i++) {
        adj[H[i][0]].push_back({H[i][1], L[i]});
        adj[H[i][1]].push_back({H[i][0], L[i]});
    }
    k = K;
    dfs(0, -1);
    return (ans == INT_MAX ? -1 : (ans - 1));
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...