#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll N = 2e5 + 10;
vector<array<ll, 2>> ad[N];
ll mark[N], sz[N], ans = 1e18, k;
array<ll, 2> nodes[N];
int t, tin[N], tout[N], lv[N];
vector<ll> we(N, 1e18);
ll size(ll node, ll par) {
sz[node] = 1;
for (auto [v, w] : ad[node]) {
if (v != par and !mark[v]) {
sz[node] += size(v, node);
}
}
return sz[node];
}
ll centroid(ll node, ll par, ll s) {
for (auto [v, w] : ad[node]) {
if (v != par and !mark[v]) {
if (sz[v] > s / 2) return centroid(v, node, s);
}
}
return node;
}
void dfs(ll node, ll par, ll wt, int l = 0) {
nodes[t] = {node, wt};
tin[node] = t++;
lv[node] = l;
for (auto [v, w] : ad[node]) {
if (v != par and !mark[v]) {
dfs(v, node, wt + w, l + 1);
}
}
tout[node] = t - 1;
}
void go(ll node) {
ll s = size(node, node);
ll c = centroid(node, node, s);
mark[c] = 1;
t = 0;
dfs(c, c, 0);
we[0] = 0;
for (auto [v, w] : ad[c]) {
if (!mark[v]) {
for (ll i = tin[v]; i <= tout[v]; i++) {
auto [a, b] = nodes[i];
if (k - b >= 0)
ans = min(ans, we[k - b] + lv[a]);
}
for (ll i = tin[v]; i <= tout[v]; i++) {
auto [a, b] = nodes[i];
we[b] = min(we[b], 1ll * lv[a]);
}
}
}
for (int i = 0; i < t; i++) {
auto [a, b] = nodes[i];
we[b] = 1e18;
}
for (auto [v, w] : ad[c]) {
if (!mark[v]) {
go(v);
}
}
}
int best_path(ll n, ll d, ll H[][2], ll dis[]) {
k = d;
for (int i = 0; i + 1 < n; i++) {
ad[H[i][0]].push_back({H[i][1], dis[i]});
ad[H[i][1]].push_back({H[i][0], dis[i]});
}
go(0);
if (ans == 1e18) ans = -1;
return ans;
}
Compilation message
/usr/bin/ld: /tmp/ccCdEXwq.o: in function `main':
grader.cpp:(.text.startup+0x28): undefined reference to `best_path(int, int, int (*) [2], int*)'
collect2: error: ld returned 1 exit status