#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[2][2 * MAXN], 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) {
dist[i][0] = distance(cityX, i);
dist[i][1] = distance(cityY, i);
}
for (int i = 0; i <= 2 * numNode; ++i)
dp[0][i] = dp[1][i] = maxSum + 1;
dp[0][0] = 0;
for (int i = 0; i < numNode; ++i) {
int pre(i & 1), cur(!pre);
for (int j = 0; j <= 2 * i; ++j) {
if(dp[pre][j] > maxSum)
continue;
//cout << i << ' ' << j << ' ' << dp[pre][j] << '\n';
dp[cur][j] = min(dp[cur][j], dp[pre][j]);
dp[cur][j + 1] = min(dp[cur][j + 1], dp[pre][j] + min(dist[i][0], dist[i][1]));
dp[cur][j + 2] = min(dp[cur][j + 2], dp[pre][j] + max(dist[i][0], dist[i][1]));
dp[pre][j] = maxSum + 1;
}
}
int res(0);
for (int j = 1; j <= 2 * numNode; ++j) {
if(dp[numNode & 1][j] <= maxSum)
res = j;
}
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 |
19032 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 |
Execution timed out |
1044 ms |
50608 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19036 KB |
Output is correct |
2 |
Correct |
4 ms |
19132 KB |
Output is correct |
3 |
Correct |
3 ms |
19036 KB |
Output is correct |
4 |
Correct |
3 ms |
19036 KB |
Output is correct |
5 |
Incorrect |
3 ms |
19036 KB |
1st lines differ - on the 1st token, expected: '3', found: '4' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19036 KB |
Output is correct |
2 |
Correct |
4 ms |
19132 KB |
Output is correct |
3 |
Correct |
3 ms |
19036 KB |
Output is correct |
4 |
Correct |
3 ms |
19036 KB |
Output is correct |
5 |
Incorrect |
3 ms |
19036 KB |
1st lines differ - on the 1st token, expected: '3', found: '4' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19036 KB |
Output is correct |
2 |
Correct |
4 ms |
19132 KB |
Output is correct |
3 |
Correct |
3 ms |
19036 KB |
Output is correct |
4 |
Correct |
3 ms |
19036 KB |
Output is correct |
5 |
Incorrect |
3 ms |
19036 KB |
1st lines differ - on the 1st token, expected: '3', found: '4' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
19032 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 |
19032 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 |
19032 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 |
19032 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 |
19032 KB |
2nd lines differ - on the 1st token, expected: '3', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |