답안 #539084

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
539084 2022-03-18T11:29:59 Z huB1erTi2 Tug of War (BOI15_tug) C++17
23 / 100
14 ms 1660 KB
//   _|                                                _|
//   _|  _|    _|    _|    _|_|    _|_|_|      _|_|_|  _|  _|      _|_|      _|_|
//   _|_|      _|    _|  _|_|_|_|  _|    _|  _|_|      _|_|      _|_|_|_|  _|_|_|_|
//   _|  _|    _|    _|  _|        _|    _|      _|_|  _|  _|    _|        _|
//   _|    _|    _|_|_|    _|_|_|  _|_|_|    _|_|_|    _|    _|    _|_|_|    _|_|_|
//                   _|            _|
//               _|_|              _|
#include <iostream>
#include <cassert>
#include <vector>
#include <algorithm>

using namespace std;

// #define DEBUG
#ifdef DEBUG
#define dassert(x) assert(x);
#define df(...) printf(__VA_ARGS__)
#else
#define dassert(x)
#define df(...)
#endif

#define x first
#define y second
#define mp make_pair
#define pb push_back
#define ir(x, a, b) ((a) <= (x) && (x) <= (b))
#define vec vector
#define sz(x) (ll)x.size()
#define foru(i, n) for (int i = 0; (i) < (n); ++(i))
#define all(x) (x).begin(), (x).end()

typedef int64_t ll;

int read() {
    int n = 0; bool b = 0; char c = getchar();
    for (; !ir(c, '0', '9'); c = getchar()) b = (c == '-');
    for (; ir(c, '0', '9'); c = getchar()) n = 10*n + (c-'0');
    if (b) return -n;
    return n;
}

vec<vec<pair<int, int>>> g;
vec<int> ct;
vec<char> used;

// edges, vertices
pair<int, int> count(int v) {
    used[v] = 1;
    int se = ct[v], sv = 1;
    for (auto [c, _] : g[v]) {
        if (used[c]) continue;
        auto [ce, cv] = count(c);
        se += ce, sv += cv;
    }
    return {se, sv};
}

int cycle(int v, int p) {
    // df("visiting %d\n", v);
    used[v] = 1;
    int ret = -1;
    for (auto [c, _] : g[v]) {
        if (c == p) continue;
        if (used[c]) { ret = v; continue; }
        int res = cycle(c, v);
        if (res != -1) ret = res;
    }
    return ret;
}

pair<int, int> dfs(int v, int p, int val, int costdown) {
    used[v] = 1;
    int sum = 0, cycle = 1e9;
    bool covered = 0;
    // df("costdown: %d\n", costdown);
    for (auto [c, cost] : g[v]) {
        if (c == p && cost == -costdown && !covered) {
            covered = 1;
            continue;
        }
        if (used[c]) {
            if (v == p) continue;
            // cout << "set cycle " << v <<  " " << c << " " << cost << endl;
            cycle = val + cost;
            sum += cost;
        } else {
            auto [ss, cyc] = dfs(c, v, val + cost, cost);
            if (cyc != 1e9) cycle = cyc;
            sum += ss + cost;
        }
    }
    return {sum, cycle};
}

void fail() {
    cout << "NO\n";
    exit(0);
}

void succeed() {
    cout << "YES\n";
    exit(0);
}

int main() {
    df("debug mode\n");
#ifndef DEBUG
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif
    int n, k;
    cin >> n >> k;
    g.resize(2*n);
    ct.resize(2*n);
    used.resize(2*n);
    foru (i, 2*n) {
        int v, u, s;
        cin >> v >> u >> s;
        u += n;
        --v, --u;
        df("%d--%d\n", v, u);
        g[v].pb({u, s});
        g[u].pb({v, -s});
        ++ct[u], ++ct[v];
    }
    foru (v, 2*n) {
        if (used[v]) continue;
        auto [ce, cv] = count(v);
        ce /= 2; // in and out counted twice
        if (ce != cv) fail();
    }
    
    used.assign(2*n, 0);
    vec<int> roots;
    foru (v, 2*n) {
        if (used[v]) continue;
        roots.pb(cycle(v, v));
        df("added root %d\n", roots.back());
        dassert(roots.back() != -1);
    }
    
    used.assign(2*n, 0);
    int zero = 0;
    vec<int> ns;
    for (auto root : roots) {
        auto [sum, cycle] = dfs(root, root, 0, 0);
        
        int a = sum, b = sum - 2*cycle;
        if (b < a) swap(a, b);
        
        zero -= a, b -= a, a -= a; // moving everything by -a
        if (b) ns.pb(b);
    }
    df("zero: %d\n", zero);
    // we want a number between zero - k, zero + k
    if (zero - k <= 0 && zero + k >= 0) succeed();
    if (zero < 0) fail();
    vec<char> dp(zero + k + 1);
    dp[0] = 1;
    int hi = 0;
    cout << "here" << endl;
    sort(all(ns));
    cout << zero+k << endl;
    for (auto x : ns) {
        // cout << x << endl;
        for (int i = hi; i >= 0; --i) {
            if (i+x <= zero+k && dp[i]) {
                dp[i+x] = 1;
                if (ir(i+x, zero-k, zero+k)) succeed();
            }
        }
        hi += x;
    }
    for (int i = zero - k; i <= zero + k; ++i) {
        if (dp[i]) succeed();
    }
    fail();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 1620 KB Output is correct
2 Correct 7 ms 1620 KB Output is correct
3 Correct 8 ms 1552 KB Output is correct
4 Correct 7 ms 1632 KB Output is correct
5 Correct 8 ms 1620 KB Output is correct
6 Correct 7 ms 1620 KB Output is correct
7 Correct 8 ms 1620 KB Output is correct
8 Correct 8 ms 1652 KB Output is correct
9 Correct 14 ms 1536 KB Output is correct
10 Correct 8 ms 1620 KB Output is correct
11 Correct 8 ms 1620 KB Output is correct
12 Correct 8 ms 1620 KB Output is correct
13 Correct 8 ms 1620 KB Output is correct
14 Correct 7 ms 1592 KB Output is correct
15 Correct 7 ms 1620 KB Output is correct
16 Correct 7 ms 1620 KB Output is correct
17 Correct 7 ms 1620 KB Output is correct
18 Correct 7 ms 1620 KB Output is correct
19 Correct 7 ms 1620 KB Output is correct
20 Correct 7 ms 1632 KB Output is correct
21 Correct 8 ms 1660 KB Output is correct
22 Correct 13 ms 1620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -