#include <bits/stdc++.h>
#include "closing.h"
#define ar array
#define ll long long
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using namespace std;
template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
const ll INF = 1e18;
int max_score(int n, int X, int Y, long long K,
std::vector<int> U, std::vector<int> V, std::vector<int> W) {
vector<vector<ar<int, 2>>> adj(n+1);
for (int i = 0; i < n-1; i++) {
adj[U[i]].push_back({V[i], W[i]});
adj[V[i]].push_back({U[i], W[i]});
}
vector<vector<ll>> d(2, vector<ll>(n, INF));
vector<int> p(n, -1);
auto dfs = [&](auto&& s, int v, vector<ll>& dist) -> void {
for (auto [u, w] : adj[v]) if (u != p[v]) {
p[u] = v, dist[u] = dist[v] + w;
s(s, u, dist);
}
};
d[0][X] = 0, d[1][Y] = 0;
dfs(dfs, X, d[0]);
fill(all(p), -1);
dfs(dfs, Y, d[1]);
vector<vector<ar<ll, 4>>> dp(n, vector<ar<ll, 4>>(2*n+1, {INF, INF, INF, INF}));
vector<int> tin(n), tout(n), sub(n);
int timer = 0;
auto dfs3 = [&](auto&& s, int v, int px) -> void {
tin[v] = timer++, sub[v] = 1;
for (auto [u, w] : adj[v]) if (u != px) {
s(s, u, v);
sub[v] += sub[u];
}
tout[v] = timer-1;
};
dfs3(dfs3, X, -1);
auto anc = [&](int x, int y) {
return tin[x] <= tin[y] && tout[y] <= tout[x];
};
auto dfs2 = [&](auto&& s, int v, int px) -> void {
dp[v][0][0] = 0;
dp[v][1][1] = d[0][v];
dp[v][1][2] = d[1][v];
dp[v][2][3] = max(d[0][v], d[1][v]);
int tot = 2;
for (auto [u, w] : adj[v]) if (u != px) {
s(s, u, v);
assert(tot <= 2*n);
for (int i = tot; i >= 0; i--) {
for (int j = 2*sub[u]; j >= 1; j--) {
assert(i+j<=2*n);
ckmin(dp[v][i+j][0], dp[v][i][0] + dp[u][j][0]);
if (anc(u, Y)) ckmin(dp[v][i+j][0], dp[v][i][0] + dp[u][j][2]);
ckmin(dp[v][i+j][1], dp[v][i][1] + dp[u][j][0]);
ckmin(dp[v][i+j][1], dp[v][i][1] + dp[u][j][1]);
if (anc(v, Y)) {
ckmin(dp[v][i+j][1], dp[v][i][1] + dp[u][j][2]);
ckmin(dp[v][i+j][1], dp[v][i][1] + dp[u][j][3]);
}
if (anc(u, Y)) {
ckmin(dp[v][i+j][2], dp[v][i][2] + dp[u][j][2]);
}
else {
ckmin(dp[v][i+j][2], dp[v][i][2] + dp[u][j][0]);
ckmin(dp[v][i+j][2], dp[v][i][2] + dp[u][j][2]);
ckmin(dp[v][i+j][2], dp[v][i][2] + dp[u][j][3]);
}
if (anc(u, Y)) {
ckmin(dp[v][i+j][3], dp[v][i][3] + dp[u][j][2]);
ckmin(dp[v][i+j][3], dp[v][i][3] + dp[u][j][3]);
}
else {
ckmin(dp[v][i+j][3], dp[v][i][3] + dp[u][j][0]);
ckmin(dp[v][i+j][3], dp[v][i][3] + dp[u][j][1]);
ckmin(dp[v][i+j][3], dp[v][i][3] + dp[u][j][2]);
ckmin(dp[v][i+j][3], dp[v][i][3] + dp[u][j][3]);
}
}
}
tot += 2*sub[u];
}
};
dfs2(dfs2, X, -1);
int ans = 0;
for (int i = 0; i < n; i++) for (int j = 1; j <= 2*n; j++) for (int k = 0; k < 4; k++) if (dp[i][j][k] <= K) ckmax(ans, j);
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1124 ms |
2097152 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
576 KB |
Output is correct |
7 |
Correct |
0 ms |
352 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Incorrect |
0 ms |
608 KB |
1st lines differ - on the 1st token, expected: '44', found: '52' |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
576 KB |
Output is correct |
7 |
Correct |
0 ms |
352 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Incorrect |
0 ms |
608 KB |
1st lines differ - on the 1st token, expected: '44', found: '52' |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
576 KB |
Output is correct |
7 |
Correct |
0 ms |
352 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Incorrect |
0 ms |
608 KB |
1st lines differ - on the 1st token, expected: '44', found: '52' |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
356 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
352 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '11', found: '12' |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
576 KB |
Output is correct |
8 |
Correct |
0 ms |
352 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Incorrect |
0 ms |
608 KB |
1st lines differ - on the 1st token, expected: '44', found: '52' |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
576 KB |
Output is correct |
8 |
Correct |
0 ms |
352 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Incorrect |
0 ms |
608 KB |
1st lines differ - on the 1st token, expected: '44', found: '52' |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
576 KB |
Output is correct |
8 |
Correct |
0 ms |
352 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Incorrect |
0 ms |
608 KB |
1st lines differ - on the 1st token, expected: '44', found: '52' |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
576 KB |
Output is correct |
8 |
Correct |
0 ms |
352 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Incorrect |
0 ms |
608 KB |
1st lines differ - on the 1st token, expected: '44', found: '52' |
12 |
Halted |
0 ms |
0 KB |
- |