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<long long, long long>>> adj(N);
for (long long i = 0; i < N - 1; i++) {
adj[H[i][0]].push_back(std::make_pair(H[i][1], 1ll * L[i]));
adj[H[i][1]].push_back(std::make_pair(H[i][0], 1ll * L[i]));
}
std::vector<long long> sum(N, 0);
std::vector<long long> dep(N);
dep[0] = 1;
auto Dfs = [&](long long u, long long 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);
long long mn = N + 1;
auto small_to_large = [&](long long u, long long p, auto&& small_to_large) -> std::map<long long, long long> {
long long need = K + (2 * sum[u]);
std::map<long long, long long> here;
here[sum[u]] = dep[u];
for (auto& [v, w] : adj[u]) {
if (v == p) continue;
std::map<long long, long long> 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) {
if (here[s] == 0) here[s] = depth;
else 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 (int) 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... |