#include <bits/stdc++.h>
using namespace std;
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define LSB(i) ((i) & (-i))
#define ll long long
const int dx[]{-1,1,0,0,-1,-1,1,1};
const int dy[]{0,0,1,-1,-1,1,-1,1};
const int MOD = 1e9+7;
#define int ll
const int N = 2e5+5;
std::vector<array<int,2>> G[N];
map<int, int> vec[N];
int sum[N], dist[N], n, k, ans;
void dfs0(int u, int p, int s = 0, int d = 0){
sum[u] = s;
dist[u] = d;
vec[u][s] = d;
for(auto& nei : G[u]){
int v = nei[0], w = nei[1];
if(v==p) continue;
dfs0(v, u, s+w, d+1);
}
}
void dfs(int u, int p){
int target = k + 2*sum[u];
for(auto& nei : G[u]){
int v = nei[0];
if(v==p) continue;
dfs(v, u);
if(vec[v].size() > vec[u].size()) swap(vec[u], vec[v]);
for(auto& p : vec[v]){
if(vec[u].count(target-p.first)){
ans = min(ans, vec[u][target-p.first]+p.second-2*dist[u]);
}
}
for(auto& p : vec[v]){
if(!vec[u].count(p.first)) vec[u].insert(p);
else vec[u][p.first] = min(vec[u][p.first], p.second);
}
}
}
int best_path(int _n, int _k, int edges[][2], int weights[]) {
if (k == 1) { return 0; }
n = _n;
k = _k;
ans = INT_MAX;
for (int i = 0; i < n - 1; i++) {
int u = edges[i][0];
int v = edges[i][1];
G[u].push_back({v, weights[i]});
G[v].push_back({u, weights[i]});
}
dfs0(0, -1);
dfs(0, -1);
return ans == INT_MAX ? -1 : ans;
}
Compilation message
/usr/bin/ld: /tmp/ccJUb2lk.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