#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, vector<int> eu, vector<int> ev, vector<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);
}
vector<int> u, v, w;
ll k;
int n, x, y;
cin >> n >> x >> y >> k;
u.resize(n - 1);
v.resize(n - 1);
w.resize(n - 1);
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
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
18012 KB |
2nd lines differ - on the 1st token, expected: '3', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
189 ms |
49236 KB |
Output is correct |
2 |
Correct |
177 ms |
52560 KB |
Output is correct |
3 |
Correct |
66 ms |
23184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
18008 KB |
Output is correct |
2 |
Correct |
3 ms |
18012 KB |
Output is correct |
3 |
Correct |
4 ms |
18012 KB |
Output is correct |
4 |
Correct |
4 ms |
18012 KB |
Output is correct |
5 |
Correct |
3 ms |
18008 KB |
Output is correct |
6 |
Correct |
3 ms |
18008 KB |
Output is correct |
7 |
Correct |
3 ms |
18012 KB |
Output is correct |
8 |
Correct |
3 ms |
18008 KB |
Output is correct |
9 |
Correct |
4 ms |
18012 KB |
Output is correct |
10 |
Correct |
3 ms |
18012 KB |
Output is correct |
11 |
Correct |
3 ms |
18264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
18008 KB |
Output is correct |
2 |
Correct |
3 ms |
18012 KB |
Output is correct |
3 |
Correct |
4 ms |
18012 KB |
Output is correct |
4 |
Correct |
4 ms |
18012 KB |
Output is correct |
5 |
Correct |
3 ms |
18008 KB |
Output is correct |
6 |
Correct |
3 ms |
18008 KB |
Output is correct |
7 |
Correct |
3 ms |
18012 KB |
Output is correct |
8 |
Correct |
3 ms |
18008 KB |
Output is correct |
9 |
Correct |
4 ms |
18012 KB |
Output is correct |
10 |
Correct |
3 ms |
18012 KB |
Output is correct |
11 |
Correct |
3 ms |
18264 KB |
Output is correct |
12 |
Correct |
4 ms |
18008 KB |
Output is correct |
13 |
Correct |
3 ms |
18012 KB |
Output is correct |
14 |
Correct |
3 ms |
18100 KB |
Output is correct |
15 |
Correct |
3 ms |
18208 KB |
Output is correct |
16 |
Correct |
4 ms |
18268 KB |
Output is correct |
17 |
Correct |
3 ms |
18012 KB |
Output is correct |
18 |
Incorrect |
3 ms |
18012 KB |
9th lines differ - on the 1st token, expected: '26', found: '24' |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
18008 KB |
Output is correct |
2 |
Correct |
3 ms |
18012 KB |
Output is correct |
3 |
Correct |
4 ms |
18012 KB |
Output is correct |
4 |
Correct |
4 ms |
18012 KB |
Output is correct |
5 |
Correct |
3 ms |
18008 KB |
Output is correct |
6 |
Correct |
3 ms |
18008 KB |
Output is correct |
7 |
Correct |
3 ms |
18012 KB |
Output is correct |
8 |
Correct |
3 ms |
18008 KB |
Output is correct |
9 |
Correct |
4 ms |
18012 KB |
Output is correct |
10 |
Correct |
3 ms |
18012 KB |
Output is correct |
11 |
Correct |
3 ms |
18264 KB |
Output is correct |
12 |
Correct |
4 ms |
18008 KB |
Output is correct |
13 |
Correct |
3 ms |
18012 KB |
Output is correct |
14 |
Correct |
3 ms |
18100 KB |
Output is correct |
15 |
Correct |
3 ms |
18208 KB |
Output is correct |
16 |
Correct |
4 ms |
18268 KB |
Output is correct |
17 |
Correct |
3 ms |
18012 KB |
Output is correct |
18 |
Incorrect |
3 ms |
18012 KB |
9th lines differ - on the 1st token, expected: '26', found: '24' |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
18012 KB |
2nd lines differ - on the 1st token, expected: '3', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
18012 KB |
2nd lines differ - on the 1st token, expected: '3', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
18012 KB |
2nd lines differ - on the 1st token, expected: '3', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
18012 KB |
2nd lines differ - on the 1st token, expected: '3', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
18012 KB |
2nd lines differ - on the 1st token, expected: '3', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |