This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define vec vector
typedef long long ll;
void dfs(ll u, ll p, vec<ll> &dist, vec<vec<pair<ll, ll>>> &adj) {
for (auto [v, w] : adj[u]) {
if (v == p) continue;
dist[v] = dist[u] + w;
dfs(v, u, dist, adj);
}
}
ll max_score_ll(ll N, ll X, ll Y, ll K, vec<ll> U, vec<ll> V, vec<ll> W) {
vec<vec<pair<ll, ll>>> adj(N);
for (ll i = 0; i < N - 1; i++) {
adj[U[i]].emplace_back(V[i], W[i]);
adj[V[i]].emplace_back(U[i], W[i]);
}
vec<ll> dist_x(N), dist_y(N);
dfs(X, -1, dist_x, adj);
dfs(Y, -1, dist_y, adj);
ll ans = 0;
vec<ll> dist(N * 2);
for (int x: dist_x) dist.emplace_back(x);
for (int y: dist_y) dist.emplace_back(y);
sort(dist.begin(), dist.end(), greater<ll>());
ll sum = 0;
for (int d: dist) {
sum += d;
if (sum > K) break;
ans++;
}
return ans;
}
int max_score(int N, int X, int Y, ll K, vec<int> U, vec<int> V, vec<int> W) {
vec<ll> _U(N - 1), _V(N - 1), _W(N - 1);
for (int i = 0; i < N - 1; i++) {
_U[i] = U[i];
_V[i] = V[i];
_W[i] = W[i];
}
return max_score_ll(N, X, Y, K, _U, _V, _W);
}
# | 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... |