Submission #1369418

#TimeUsernameProblemLanguageResultExecution timeMemory
1369418pandaa73Tug of War (BOI15_tug)C++20
Compilation error
0 ms0 KiB
#include <bits/stdc++.h>
using namespace std;

#define ff endl
#define lf "\n"
#define fi first
#define se second
#define _ << ' ' <<
#define all(x) begin(x),end(x)
#define rall(x) rbegin(x),rend(x)

#ifdef debug

constexpr bool is_debug = 1;

#define infor(fmt, ...) do { print(stderr, fmt, ##__va_args__); } while(0)
#define infof(fmt, ...) do { println(stderr, fmt, ##__va_args__); } while(0)

#else

constexpr bool is_debug = 0;

#define infor(fmt, ...)
#define infof(fmt, ...)

#endif

using ll = long long;

using pll = pair<ll, ll>;
using pii = pair<int, int>;

template<typename... args>
using vec = vector<args...>;

mt19937 timmy_loves_gambling(73);

constexpr int maxs = 6e5+7;

int main(void) {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n, k; cin >> n >> k;

    vec<int> dg(2 * n);
    vec<vec<pii>> adj(2 * n);
    for(int i = 0; i < 2 * n; ++i) {
        int l, r, x; cin >> l >> r >> x;

        l -= 1, r += n - 1;

        adj[l].emplace_back(r, x);
        adj[r].emplace_back(l, x);

        dg[l] += 1, dg[r] += 1;
    }

    vec<int> w;
    queue<int> q;
    for(int i = 0; i < 2 * n; ++i) {
        if(dg[i] != 1) continue;

        auto [j, x] = adj[i][0];

        if(dg[j] == 1) {
            w.emplace_back(x);
        } else {
            q.emplace(i);
        }
    }

    int l = 0, r = 0, x = 0, y = 0;
    while(!q.empty()) {
        auto u = q.front(); q.pop();

        for(auto [v, x]: adj[u]) {
            if(dg[v] == 1) continue;
            dg[v] -= 1;
            if(dg[v] != 1) continue;

            q.emplace(v);

            (v < n ? l : r) += x;
            (v < n ? x : y) += 1;
        }
    }

    if(x != y) {
        cout << "no" << lf;
        return 0;
    }

    for(int i = 0; i < 2 * n; ++i) {
        if(dg[i] > 2) {
            cout << "no" << lf;
            return 0;
        }
    }

    vec<bool> vis(2 * n);
    for(int i = 0; i < 2 * n; ++i) {
        vis[i] = dg[i] < 2;
    }

    auto dfs = [&](int u, auto &&dfs) -> int {
        vis[u] = 1;

        for(auto [v, x]: adj[u]) {
            if(vis[v]) continue;

            int y = dfs(v, dfs);

            return x - y;
        }

        return 0;
    };

    for(int i = 0; i < 2 * n; ++i) {
        if(vis[i]) continue;

        int x = 0, f = -1;
        for(auto [j, y]: adj[i]) {
            if(vis[j] == 0) {
                x = y;
                f = j;
            }
        }

        int y = dfs(i, dfs);

        w.emplace_back(abs(x - y));
    }

    int s = accumulate(all(w), 0);

    bitset<maxs> kp;
    kp[0] = 1;

    for(auto w: w) {
        kp |= kp << w;
    }

    int mn = int_max;
    for(int i = 0; i <= s; ++i) {
        if(kp[i] == 0) continue;

        mn = min(mn, abs((l + i) - (r + s - i)));
    }

    cout << (mn <= k ? "yes" : "no") << lf;
}

Compilation message (stderr)

tug.cpp: In function 'int main()':
tug.cpp:145:14: error: 'int_max' was not declared in this scope
  145 |     int mn = int_max;
      |              ^~~~~~~