Submission #471163

#TimeUsernameProblemLanguageResultExecution timeMemory
471163jalsolRace (IOI11_race)C++17
9 / 100
465 ms262148 KiB
// looking to shine brighter in this world...

#define TASK "race"
#include "race.h"

#ifdef LOCAL
#include "local.h"
#else
#include <bits/stdc++.h>
#define debug(...)
#define DB(...)
#endif

using namespace std;

using ll = long long;
using ld = long double;

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fi first
#define se second

#define For(i, l, r) for (int i = (l); i <= (r); ++i)
#define Ford(i, r, l) for (int i = (r); i >= (l); --i)
#define Rep(i, n) For (i, 0, (n) - 1)
#define Repd(i, n) Ford (i, (n) - 1, 0)
#define Fe(...) for (auto __VA_ARGS__)

template <class C> inline int isz(const C& c) { return static_cast<int>(c.size()); }
template <class T> inline bool chmin(T& a, const T& b) { return (a > b) ? a = b, true : false; }
template <class T> inline bool chmax(T& a, const T& b) { return (a < b) ? a = b, true : false; }

const bool __initialization = []() {
    cin.tie(nullptr)->sync_with_stdio(false);
    if (fopen(TASK".inp", "r")) {
        (void)(!freopen(TASK".inp", "r", stdin));
        (void)(!freopen(TASK".out", "w", stdout)); }
    cout << setprecision(12) << fixed;
    return true;
}();

constexpr ld eps = 1e-9;
constexpr int inf = 0x3f3f3f3f; // for memset
constexpr ll linf = 1e18;

// ================================================================================

constexpr int maxn = 2e5;
constexpr int maxk = 100 + 1; // subtask 3

struct Info { int v, c; };
vector<Info> g[maxn];

int dp1[maxn][maxk];

void DfsDown(int u, int p, int k) {
    dp1[u][0] = 0;

    Fe (&[v, c] : g[u]) {
        if (v == p) continue;

        DfsDown(v, u, k);

        For (i, 0, k - c) {
            chmin(dp1[u][i + c], dp1[v][i] + 1);
        }
    }
}

int dp2[maxn][maxk];

void DfsUp(int u, int p, int k) {
    vector<int> fi(k + 1, inf);
    vector<int> se(k + 1, inf);

    Fe (&[v, c] : g[u]) {
        if (v == p) continue;

        For (i, 0, k - c) {
            if (fi[i + c] > dp1[v][i] + 1) {
                se[i + c] = fi[i + c];
                fi[i + c] = dp1[v][i] + 1;
            }
            else {
                chmin(se[i + c], dp1[v][i] + 1);
            }
        }
    }

    Fe (&[v, c] : g[u]) {
        if (v == p) continue;

        dp2[v][0] = 0;

        For (i, 0, k - c) {
            chmin(dp2[v][i + c], dp2[u][i] + 1);
        }

        // TODO: optimize this
        // Fe (&[w, cw] : g[u]) {
        //     if (w == p || w == v) continue;

        //     For (i, 0, k - c - cw) {
        //         chmin(dp2[v][i + c + cw], dp1[w][i] + 2);
        //     }
        // }

        For (i, c, k - c) {
            int temp = (dp1[v][i - c] + 1 == fi[i]
                        ? se[i]
                        : fi[i]);

            chmin(dp2[v][i + c], temp + 1);
        }

        DfsUp(v, u, k);
    }
}

int best_path(int n, int k, int h[][2], int l[]) {
    Rep (i, n - 1) {
        int u = h[i][0];
        int v = h[i][1];
        int c = l[i];

        g[u].push_back({ v, c });
        g[v].push_back({ u, c });
    }

    memset(dp1, 0x3f, sizeof(dp1));
    DfsDown(0, -1, k);

    memset(dp2, 0x3f, sizeof(dp2));
    dp2[0][0] = 0;

    DfsUp(0, -1, k);

    int ans = inf;

    Rep (u, n) {
        For (i, 0, k) {
            chmin(ans, dp1[u][i] + dp2[u][k - i]);
        }
    }

    if (ans == inf) ans = -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...