답안 #539088

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
539088 2022-03-18T11:41:44 Z huB1erTi2 Tug of War (BOI15_tug) C++17
71 / 100
3000 ms 4436 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;
    sort(all(ns));
    for (auto x : ns) {
        // cout << x << endl;
        for (int i = min(hi, zero+k-x); i >= 0; --i) { 
            if (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 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 12 ms 468 KB Output is correct
27 Correct 8 ms 596 KB Output is correct
28 Correct 10 ms 468 KB Output is correct
29 Correct 9 ms 596 KB Output is correct
30 Correct 18 ms 468 KB Output is correct
31 Correct 9 ms 600 KB Output is correct
32 Correct 9 ms 600 KB Output is correct
33 Correct 8 ms 596 KB Output is correct
34 Correct 4 ms 468 KB Output is correct
35 Correct 9 ms 532 KB Output is correct
36 Correct 11 ms 524 KB Output is correct
37 Correct 9 ms 596 KB Output is correct
38 Correct 20 ms 596 KB Output is correct
39 Correct 8 ms 596 KB Output is correct
40 Correct 11 ms 596 KB Output is correct
41 Correct 8 ms 596 KB Output is correct
42 Correct 20 ms 596 KB Output is correct
43 Correct 8 ms 596 KB Output is correct
44 Correct 10 ms 596 KB Output is correct
45 Correct 8 ms 604 KB Output is correct
46 Correct 10 ms 572 KB Output is correct
47 Correct 2 ms 596 KB Output is correct
48 Correct 3 ms 468 KB Output is correct
49 Correct 3 ms 468 KB Output is correct
50 Correct 25 ms 536 KB Output is correct
51 Correct 69 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 1620 KB Output is correct
2 Correct 8 ms 1588 KB Output is correct
3 Correct 13 ms 1632 KB Output is correct
4 Correct 8 ms 1620 KB Output is correct
5 Correct 11 ms 1568 KB Output is correct
6 Correct 9 ms 1620 KB Output is correct
7 Correct 8 ms 1620 KB Output is correct
8 Correct 7 ms 1620 KB Output is correct
9 Correct 8 ms 1620 KB Output is correct
10 Correct 8 ms 1620 KB Output is correct
11 Correct 8 ms 1620 KB Output is correct
12 Correct 9 ms 1616 KB Output is correct
13 Correct 8 ms 1620 KB Output is correct
14 Correct 10 ms 1524 KB Output is correct
15 Correct 8 ms 1620 KB Output is correct
16 Correct 9 ms 1620 KB Output is correct
17 Correct 7 ms 1620 KB Output is correct
18 Correct 8 ms 1620 KB Output is correct
19 Correct 8 ms 1620 KB Output is correct
20 Correct 8 ms 1528 KB Output is correct
21 Correct 7 ms 1620 KB Output is correct
22 Correct 10 ms 1620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 12 ms 468 KB Output is correct
27 Correct 8 ms 596 KB Output is correct
28 Correct 10 ms 468 KB Output is correct
29 Correct 9 ms 596 KB Output is correct
30 Correct 18 ms 468 KB Output is correct
31 Correct 9 ms 600 KB Output is correct
32 Correct 9 ms 600 KB Output is correct
33 Correct 8 ms 596 KB Output is correct
34 Correct 4 ms 468 KB Output is correct
35 Correct 9 ms 532 KB Output is correct
36 Correct 11 ms 524 KB Output is correct
37 Correct 9 ms 596 KB Output is correct
38 Correct 20 ms 596 KB Output is correct
39 Correct 8 ms 596 KB Output is correct
40 Correct 11 ms 596 KB Output is correct
41 Correct 8 ms 596 KB Output is correct
42 Correct 20 ms 596 KB Output is correct
43 Correct 8 ms 596 KB Output is correct
44 Correct 10 ms 596 KB Output is correct
45 Correct 8 ms 604 KB Output is correct
46 Correct 10 ms 572 KB Output is correct
47 Correct 2 ms 596 KB Output is correct
48 Correct 3 ms 468 KB Output is correct
49 Correct 3 ms 468 KB Output is correct
50 Correct 25 ms 536 KB Output is correct
51 Correct 69 ms 600 KB Output is correct
52 Correct 7 ms 1620 KB Output is correct
53 Correct 8 ms 1588 KB Output is correct
54 Correct 13 ms 1632 KB Output is correct
55 Correct 8 ms 1620 KB Output is correct
56 Correct 11 ms 1568 KB Output is correct
57 Correct 9 ms 1620 KB Output is correct
58 Correct 8 ms 1620 KB Output is correct
59 Correct 7 ms 1620 KB Output is correct
60 Correct 8 ms 1620 KB Output is correct
61 Correct 8 ms 1620 KB Output is correct
62 Correct 8 ms 1620 KB Output is correct
63 Correct 9 ms 1616 KB Output is correct
64 Correct 8 ms 1620 KB Output is correct
65 Correct 10 ms 1524 KB Output is correct
66 Correct 8 ms 1620 KB Output is correct
67 Correct 9 ms 1620 KB Output is correct
68 Correct 7 ms 1620 KB Output is correct
69 Correct 8 ms 1620 KB Output is correct
70 Correct 8 ms 1620 KB Output is correct
71 Correct 8 ms 1528 KB Output is correct
72 Correct 7 ms 1620 KB Output is correct
73 Correct 10 ms 1620 KB Output is correct
74 Execution timed out 3069 ms 4436 KB Time limit exceeded
75 Halted 0 ms 0 KB -