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 <iostream>
#include <vector>
#include <map>
using namespace std;
int n, k, ans = 1e9;
vector<vector<pair<int, int>>> g;
map<int, int> dfs(int u, int par, int s, int d) {
map<int, int> ret;
ret[s] = d;
for (auto [v, w] : g[u]) {
if (v == par) continue;
map<int, int> res = dfs(v, u, s + w, d + 1);
if (ret.size() > res.size()) swap(ret, res);
for (auto [sv, dv] : res) {
int su = k - (sv - 2 * s);
if (ret.find(su) != ret.end()) ans = min(ans, dv + ret[su] - 2 * d);
}
for (auto [sv, dv] : res) {
if (ret.find(sv) != ret.end()) ret[sv] = min(ret[sv], dv);
else ret[sv] = dv;
}
}
return ret;
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N;
k = K;
g.resize(n);
for (int i = 0; i < n - 1; i++) {
g[H[i][0]].push_back({H[i][1], L[i]});
g[H[i][1]].push_back({H[i][0], L[i]});
}
dfs(0, 0, 0, 0);
return (ans > n ? -1 : ans);
}
# | 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... |