Submission #900599

#TimeUsernameProblemLanguageResultExecution timeMemory
900599mayankfrostRace (IOI11_race)C++17
100 / 100
283 ms39076 KiB
#include <bits/stdc++.h>

using namespace std;

typedef int ll;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef map<ll, ll> mll;

#define N 200'005
#define L 1'000'005
#define MOD 1000000007
#define FOR(i, n) for (i = 0; i < n; i++)
#define FORR(i, a, b) for (i = a; i <= b; i++)
#define FASTIO                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);
#define FREOPEN                        \
    freopen("yinyang.in", "r", stdin); \
    freopen("yinyang.out", "w", stdout);
#define gohome      \
    cout << "NO\n"; \
    return;
#define arprt(x)           \
    for (auto it : x)      \
        cout << it << " "; \
    cout << "\n";

void solve();

vpll edges[N];
vll torem;
ll sz[N], maxd, l, ans, cnt[L];
bool rem[N], calc;

const int BUF = 100'001;

ll find_sz(ll v, ll p)
{
    sz[v] = 1;
    for (auto [it, x] : edges[v])
        if (it != p && !rem[it])
            sz[v] += find_sz(it, v);
    return sz[v];
}

ll get_centroid(ll v, ll p, ll trsz)
{
    for (auto [it, x] : edges[v])
        if (it != p && !rem[it] && sz[it] * 2 > trsz)
            return get_centroid(it, v, trsz);
    return v;
}

void process(ll v, ll p, ll s, ll d)
{
    if (s > l)
        return;
    torem.push_back(s);
    if (calc)
    {
        if (cnt[l - s] < INT_MAX)
            ans = min(ans, d + cnt[l - s]);
    }
    else
        cnt[s] = min(cnt[s], d);
    for (auto [u, c] : edges[v])
    {
        if (u == p || rem[u])
            continue;
        process(u, v, s + c, d + 1);
    }
}

void centroid_decomp(ll v)
{
    ll centroid = get_centroid(v, -1, find_sz(v, -1));
    rem[centroid] = true;
    for (auto [u, c] : edges[centroid])
    {
        if (rem[u])
            continue;
        calc = true;
        process(u, -1, c, 1);
        calc = false;
        process(u, -1, c, 1);
    }
    for (auto it : torem)
        cnt[it] = INT_MAX;
    torem.clear();
    for (auto [it, x] : edges[centroid])
        if (!rem[it])
            centroid_decomp(it);
}

int best_path(int n, int K, int H[][2], int *cost)
{
    l = K;
    int u, v, c;
    for (int i = 0; i < n - 1; i++)
    {
        u = H[i][0], v = H[i][1], c = cost[i];
        edges[u].push_back({v, c});
        edges[v].push_back({u, c});
    }
    for (int i = 1; i < L; i++)
        cnt[i] = INT_MAX;
    ans = INT_MAX;
    centroid_decomp(0);
    if (ans == INT_MAX)
        ans = -1;
    return ans;
}

// int main()
// {
//     int n = 4, k = 3;
//     int H[3][2] = {{0, 1}, {1, 2}, {1, 3}};
//     int *cost = new int[3];
//     cost[0] = 1;
//     cost[1] = 2;
//     cost[2] = 4;
//     cout << best_path(n, k, H, cost);
// }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...