Submission #1208763

#TimeUsernameProblemLanguageResultExecution timeMemory
1208763NeltClosing Time (IOI23_closing)C++20
8 / 100
133 ms37492 KiB
#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, ptr = n - 1, sum = 0;
    vector<ll> path = getpath(X, Y, b);
    a.insert(a.begin(), 0);
    b.insert(b.begin(), 0);
    bool onpath[n + 1];
    for (ll i = 1; i <= n; i++) onpath[i] = 0;
    for (ll i : path) i++, onpath[i] = 1, k -= min(a[i], b[i]);
    ll tot = path.size();
    vector<ll> cost;
    for (ll i = 1; 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;
    }
    a.erase(a.begin());
    b.erase(b.begin());
    for (ll i = 0; i < n; i++) sum += b[i];
    for (ll i : path) k += min(a[i], b[i]);
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    if (sum <= k) ans = max(ans, n);
    for (ll i = 0; i < n; i++)
    {
        sum += a[i];
        while (ptr >= 0 and sum > k) sum -= b[ptr--];
        if (sum <= k) ans = max(ans, ptr + 1 + i + 1);
    }
    return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...