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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 7;
const int K = 1e6 + 7;
const int LG = 20;
const int inf = 1e9 + 7;
int n, k;
int sz[N];
bool used[N];
vector <pair <int, int> > g[N];
vector <int> kek(K, inf);
vector <int> go;
int ans = inf;
void calc(int v, int pr) {
sz[v] = 1;
for (auto to : g[v]) {
if (to.first != pr && !used[to.first]) {
calc(to.first, v);
sz[v] += sz[to.first];
}
}
}
int centroid(int v, int pr, int need) {
for (auto to : g[v]) {
if (to.first != pr && !used[to.first] && sz[to.first] > need / 2) {
return centroid(to.first, v, need);
}
}
return v;
}
void dfs(int v, int pr, int h, int cur) {
if (cur > k) {
return;
}
go.push_back(cur);
kek[cur] = min(kek[cur], h);
for (auto to : g[v]) {
if (to.first != pr && !used[to.first]) {
dfs(to.first, v, h + 1, cur + to.second);
}
}
}
void build(int v, int lg) {
calc(v, -1);
v = centroid(v, -1, sz[v]);
vector <int> dp(k + 1, inf);
dp[0] = 0;
kek[0] = 0;
for (auto to : g[v]) {
if (!used[to.first]) {
dfs(to.first, v, 1, to.second);
go.push_back(0);
for (int x : go) {
ans = min(ans, kek[x] + dp[k - x]);
}
for (int x : go) {
dp[x] = min(dp[x], kek[x]);
kek[x] = inf;
}
go = {};
}
}
used[v] = true;
for (auto to : g[v]) {
if (!used[to.first]) {
build(to.first, lg + 1);
}
}
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N, k = K;
for (int i = 0; i < n - 1; i++) {
int u, v, l;
u = H[i][0];
v = H[i][1];
l = L[i];
g[u].push_back({v, l});
g[v].push_back({u, l});
}
build(0, 0);
return (ans == inf ? -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... |