This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "race.h"
#include "bits/stdc++.h"
int best_path(int N, int K, int H[][2], int L[]) {
std::vector<std::vector<std::pair<int, int>>> adj(N);
for (int i = 0; i < N - 1; i++) {
adj[H[i][0]].push_back(std::make_pair(H[i][1], L[i]));
adj[H[i][1]].push_back(std::make_pair(H[i][0], L[i]));
}
std::vector<long long> sum(N);
std::vector<int> dep(N);
dep[0] = 1;
auto Dfs = [&](int u, int p, auto&& Dfs) -> void {
for (auto& [v, w] : adj[u]) {
if (v == p) continue;
sum[v] = sum[u] + w;
dep[v] = dep[u] + 1;
Dfs(v, u, Dfs);
}
};
Dfs(0, -1, Dfs);
int mn = N + 1;
auto small_to_large = [&](int u, int p, auto&& small_to_large) -> std::map<long long, int> {
long long need = K + (2 * sum[u]);
std::map<long long, int> here;
here[sum[u]] = dep[u];
for (auto& [v, w] : adj[u]) {
if (v == p) continue;
std::map<long long, int> child = small_to_large(v, u, small_to_large);
if (here.size() < child.size()) std::swap(here, child);
for (auto& [s, depth] : child) {
if (here[need - s] != 0) {
mn = std::min(mn, here[need - s] + depth - (2 * dep[u]));
}
}
for (auto& [s, depth] : child) {
here[s] = std::min(here[s], depth);
}
}
return here;
};
small_to_large(0, -1, small_to_large);
if (mn == N + 1) return -1;
else return mn;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |