#include "closing.h"
#include <bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
const ll N = 2e5 + 5;
vector<pair<ll, ll>> g[N];
void dfs(ll v, ll par, vector<ll> &depth)
{
for (auto [to, w] : g[v])
if (to != par)
{
depth[to] = depth[v] + w;
dfs(to, v, depth);
}
}
vector<ll> getpath(ll x, ll y, vector<ll> &d)
{
vector<ll> path;
while (x != y and path.size() <= d.size())
{
path.push_back(x);
for (auto [i, w] : g[x])
if (d[i] < d[x])
{
x = i;
break;
}
}
path.push_back(y);
return path;
}
ll n;
int max_score(int N, int X, int Y, long long k,
std::vector<int> U, std::vector<int> V, std::vector<int> W)
{
n = N;
for (ll i = 0; i < n; i++)
g[i].clear();
for (ll i = 0; i + 1 < n; i++)
g[U[i]].push_back(make_pair(V[i], W[i])), g[V[i]].push_back(make_pair(U[i], W[i]));
vector<ll> a(n), b(n);
dfs(X, -1, a);
dfs(Y, -1, b);
ll ans = 0, sum = 0;
vector<ll> path = getpath(X, Y, b);
bool onpath[n];
for (ll i = 0; i < n; i++)
onpath[i] = 0;
for (ll i : path)
onpath[i] = 1, k -= min(a[i], b[i]);
ll tot = path.size();
vector<ll> cost;
for (ll i = 0; i < n; i++)
if (onpath[i])
cost.push_back(abs(a[i] - b[i]));
else
cost.push_back(a[i]), cost.push_back(b[i]);
sort(cost.begin(), cost.end());
if (k >= 0)
{
ll res = 0;
for (ll i : cost)
{
sum += i;
if (sum > k)
break;
res++;
}
sum = 0;
ans = res + tot;
}
for (ll i : path)
k += min(a[i], b[i]);
cost.clear();
for (ll i : a)
cost.push_back(i);
for (ll i : b)
cost.push_back(i);
sort(cost.begin(), cost.end());
ll res = 0;
for (ll i : cost)
{
sum += i;
if (sum > k)
break;
res++;
}
return ans = max(ans, 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... |