#ifndef Nhoksocqt1
#include "closing.h"
#endif // Nhoksocqt1
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
const bool isMultiTest = 1;
const int MAXN = 200005;
struct State {
ll dis;
int u, t;
State(ll _dis = 0, int _u = 0, int _t = 0) : dis(_dis), u(_u), t(_t) {};
bool operator < (const State &s) const {
return (dis != s.dis) ? dis > s.dis : u > s.u;
}
};
vector<ii> adj[MAXN];
ll lastDist[MAXN], dp[MAXN][2], dist[MAXN][2], sumDepth[MAXN], maxSum;
int mask[MAXN], depth[MAXN], P[MAXN][18], numNode, cityX, cityY;
bool ok[MAXN][2];
void preDfs(int u) {
for (int it = 0; it < sz(adj[u]); ++it) {
int v(adj[u][it].fi), w(adj[u][it].se);
if(v != P[u][0]) {
sumDepth[v] = sumDepth[u] + w;
depth[v] = depth[u] + 1;
P[v][0] = u;
preDfs(v);
}
}
}
int lca(int u, int v) {
if(depth[u] < depth[v])
swap(u, v);
for (int i1 = depth[u] - depth[v]; i1 > 0; i1 ^= i1 & -i1) {
int i = __builtin_ctz(i1);
u = P[u][i];
}
if(u == v)
return u;
for (int i = 31 - __builtin_clz(depth[u]); i >= 0; --i) {
if(P[u][i] != P[v][i])
u = P[u][i], v = P[v][i];
}
return P[u][0];
}
ll distance(int u, int v) {
return sumDepth[u] + sumDepth[v] - 2LL * sumDepth[lca(u, v)];
}
int max_score(int _N, int _X, int _Y, ll _K, int eu[], int ev[], int ew[]) {
numNode = _N, cityX = _X, cityY = _Y, maxSum = _K;
for (int i = 0; i < numNode; ++i)
adj[i].clear();
for (int i = 0; i + 1 < numNode; ++i) {
int u(eu[i]), v(ev[i]), w(ew[i]);
adj[u].push_back(ii(v, w));
adj[v].push_back(ii(u, w));
}
depth[0] = 1, P[0][0] = -1;
preDfs(0);
for (int j = 1; (1 << j) <= numNode; ++j) {
for (int i = 0; i < numNode; ++i) {
if(P[i][j - 1] != -1) {
P[i][j] = P[P[i][j - 1]][j - 1];
} else {
P[i][j] = -1;
}
}
}
for (int i = 0; i < numNode; ++i) {
ll distx = distance(cityX, i);
ll disty = distance(cityY, i);
dist[i][0] = distx, dist[i][1] = disty;
mask[i] = lastDist[i] = 0;
dp[i][0] = dp[i][1] = 1e18;
}
priority_queue<State> heap;
heap.push({0, cityX, 0});
heap.push({0, cityY, 1});
dp[cityX][0] = dp[cityY][1] = 0;
ll sumUsed(0);
int res(0);
while(sz(heap)) {
ll dis(heap.top().dis);
int u(heap.top().u), t(heap.top().t);
heap.pop();
if((mask[u] >> t & 1) || dis != dp[u][t])
continue;
if(sumUsed + dis > maxSum)
break;
++res;
sumUsed += dis;
lastDist[u] += dis;
mask[u] |= (1 << t);
if(ok[u][!t] && !(mask[u] >> (!t) & 1) && dp[u][!t] > max(0LL, dist[u][!t] - lastDist[u])) {
dp[u][!t] = max(0LL, dist[u][!t] - lastDist[u]);
heap.push(State(dp[u][!t], u, !t));
}
for (int it = 0; it < sz(adj[u]); ++it) {
int v(adj[u][it].fi);
ok[v][t] = 1;
if(!(mask[v] >> t & 1) && dp[v][t] > max(0LL, dist[v][t] - lastDist[v])) {
dp[v][t] = max(0LL, dist[v][t] - lastDist[v]);
heap.push(State(dp[v][t], v, t));
}
}
}
return res;
}
#ifdef Nhoksocqt1
int main(void) {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define TASK "closing"
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
ll k;
int n, x, y, *u, *v, *w;
cin >> n >> x >> y >> k;
u = new int[n];
v = new int[n];
w = new int[n];
for (int i = 0; i + 1 < n; ++i)
cin >> u[i] >> v[i] >> w[i];
int maxScore = max_score(n, x, y, k, u, v, w);
cout << maxScore << '\n';
return 0;
}
#endif // Nhoksocqt1
Compilation message
/usr/bin/ld: /tmp/cclkrvQl.o: in function `main':
grader.cpp:(.text.startup+0x6a1): undefined reference to `max_score(int, int, int, long long, std::vector<int, std::allocator<int> >, std::vector<int, std::allocator<int> >, std::vector<int, std::allocator<int> >)'
collect2: error: ld returned 1 exit status