#include "closing.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
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<pair<int, int>>> adj(N);
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>> dist(2, vector<ll>(N));
auto dfs = [&](auto& dfs, int v, int p, int i) -> void {
for (auto& [u, w] : adj[v]) {
if (u == p) continue;
dist[i][u] = dist[i][v] + w;
dfs(dfs, u, v, i);
}
};
dfs(dfs, X, -1, 0);
dfs(dfs, Y, -1, 1);
vector<array<ll, 2>> cost;
for (int i = 0; i < N; ++i) {
ll c1 = min(dist[0][i], dist[1][i]);
ll c2 = max(dist[0][i], dist[1][i]);
cost.push_back({c1, i});
cost.push_back({c2, i});
}
ll tot = 0;
int res = 0;
vector<bool> vis(N);
sort(begin(cost), end(cost));
for (int i = 0; i < (int)cost.size(); ++i) {
ll c = cost[i][0];
int j = cost[i][1];
if (vis[j]) {
c -= min(dist[0][j], dist[1][j]);
}
if (tot + c <= K) {
tot += c;
res += 1;
vis[j] = true;
} else {
break;
}
}
// cerr << "res = " <<res << "\n";
// cerr << "Now dp:\n";
const ll INF = 1e18;
vector<vector<ll>> dp(N + 1, vector<ll>(2 * N + 5, INF));
dp[0][0] = 0;
// res = 0;
for (int i = 0; i < N; ++i) {
for (int j = 0; j <= 2 * N; ++j) {
if (dist[0][i] + dist[1][i] == dist[0][Y]) {
dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + min(dist[0][i], dist[1][i]));
dp[i + 1][j + 2] = min(dp[i + 1][j + 2], dp[i][j] + max(dist[0][i], dist[1][i]));
continue;
}
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + min(dist[0][i], dist[1][i]));
dp[i + 1][j + 2] = min(dp[i + 1][j + 2], dp[i][j] + max(dist[0][i], dist[1][i]));
}
// for (int j = 0; j <= 2 * N; ++j) {
// cerr << setw(4) << left;
// if (dp[i][j] > K) cerr << "- ";
// else cerr << dp[i][j] << " ";
// }
// cerr << "\n";
}
// for (int j = 0; j <= 2 * N; ++j) {
// cerr << setw(4) << left;
// if (dp[N][j] > K) cerr << "- ";
// else cerr << dp[N][j] << " ";
// }
// cerr << "\n";
for (int i = 0; i <= 2 * N; ++i) {
if (dp[N][i] <= K) {
res = max(res, i);
}
}
return res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |