#include <bits/stdc++.h>
#include "race.h"
using namespace std;
vector<vector<pair<int, int>>> g;
vector<map<int, int>> dp;
int k, res;
void dfs(int u, int p) {
dp[u][0] = 0;
for (auto [v, w] : g[u]) {
if (v == p) continue;
dp[u][w] = 1;
dfs(v, u);
if (dp[v].size() > dp[u].size()) {
swap(dp[v], dp[u]);
}
map<int, int> new_dp = dp[u];
for (auto [x, c] : dp[u]) {
for (auto [y, d] : dp[v]) {
if (x == y) {
new_dp[x] = min(c, d);
}
if (!new_dp.count(y)) {
new_dp[y] = d;
}
else {
new_dp[y] = min(new_dp[y], d);
}
if (!new_dp.count(x + y)) {
new_dp[x + y] = c + d;
}
else {
new_dp[x + y] = min(new_dp[x + y], c + d);
}
}
}
swap(dp[u], new_dp);
}
if (dp[u].count(k)) {
res = min(res, dp[u][k]);
}
}
int best_path(int N, int K, int H[][2], int L[]) {
k = K;
g.resize(N);
dp.resize(N);
for (int i = 0; i < N - 1; i++) {
int u = H[i][0], v = H[i][1], w = L[i];
g[u].push_back({v, w});
g[v].push_back({u, w});
}
res = N + 1;
dfs(0, -1);
return (res == N + 1 ? -1 : res);
}
# | 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... |