Submission #539139

# Submission time Handle Problem Language Result Execution time Memory
539139 2022-03-18T13:10:44 Z huB1erTi2 Tug of War (BOI15_tug) C++17
100 / 100
1063 ms 5620 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));
    vec<pair<int, int>> ps = {{ns.front(), 0}};
    for (auto x : ns) {
        if (ps.back().x == x) ++ps.back().y;
        else ps.push_back({x, 1});
    }
    for (auto [x, c] : ps) {
        if (x > zero+k) continue;
        for (int i = zero+k; i >= 0; --i) { 
            for (int t = 1; t <= c && i + t*x <= zero + k; ++t) {
                if (!dp[i] || dp[i + t*x]) break;
                dp[i+t*x] = 1;
            }
        }
    }
    for (int i = zero - k; i <= zero + k; ++i) {
        if (dp[i]) succeed();
    }
    fail();
    return 0;
}

Compilation message

tug.cpp: In function 'int main()':
tug.cpp:161:9: warning: unused variable 'hi' [-Wunused-variable]
  161 |     int hi = 0;
      |         ^~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 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 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 232 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 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 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 232 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 3 ms 468 KB Output is correct
27 Correct 23 ms 604 KB Output is correct
28 Correct 3 ms 596 KB Output is correct
29 Correct 18 ms 596 KB Output is correct
30 Correct 2 ms 468 KB Output is correct
31 Correct 16 ms 604 KB Output is correct
32 Correct 2 ms 596 KB Output is correct
33 Correct 15 ms 596 KB Output is correct
34 Correct 3 ms 468 KB Output is correct
35 Correct 19 ms 600 KB Output is correct
36 Correct 2 ms 468 KB Output is correct
37 Correct 25 ms 604 KB Output is correct
38 Correct 2 ms 596 KB Output is correct
39 Correct 15 ms 544 KB Output is correct
40 Correct 3 ms 468 KB Output is correct
41 Correct 16 ms 604 KB Output is correct
42 Correct 3 ms 596 KB Output is correct
43 Correct 15 ms 608 KB Output is correct
44 Correct 2 ms 596 KB Output is correct
45 Correct 20 ms 596 KB Output is correct
46 Correct 3 ms 596 KB Output is correct
47 Correct 2 ms 596 KB Output is correct
48 Correct 2 ms 468 KB Output is correct
49 Correct 2 ms 596 KB Output is correct
50 Correct 3 ms 596 KB Output is correct
51 Correct 3 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 1536 KB Output is correct
2 Correct 8 ms 1620 KB Output is correct
3 Correct 9 ms 1568 KB Output is correct
4 Correct 12 ms 1748 KB Output is correct
5 Correct 9 ms 1608 KB Output is correct
6 Correct 7 ms 1620 KB Output is correct
7 Correct 8 ms 1540 KB Output is correct
8 Correct 7 ms 1568 KB Output is correct
9 Correct 9 ms 1536 KB Output is correct
10 Correct 8 ms 1544 KB Output is correct
11 Correct 9 ms 1620 KB Output is correct
12 Correct 7 ms 1620 KB Output is correct
13 Correct 8 ms 1620 KB Output is correct
14 Correct 9 ms 1632 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 1532 KB Output is correct
20 Correct 7 ms 1620 KB Output is correct
21 Correct 7 ms 1756 KB Output is correct
22 Correct 8 ms 1588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 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 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 232 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 3 ms 468 KB Output is correct
27 Correct 23 ms 604 KB Output is correct
28 Correct 3 ms 596 KB Output is correct
29 Correct 18 ms 596 KB Output is correct
30 Correct 2 ms 468 KB Output is correct
31 Correct 16 ms 604 KB Output is correct
32 Correct 2 ms 596 KB Output is correct
33 Correct 15 ms 596 KB Output is correct
34 Correct 3 ms 468 KB Output is correct
35 Correct 19 ms 600 KB Output is correct
36 Correct 2 ms 468 KB Output is correct
37 Correct 25 ms 604 KB Output is correct
38 Correct 2 ms 596 KB Output is correct
39 Correct 15 ms 544 KB Output is correct
40 Correct 3 ms 468 KB Output is correct
41 Correct 16 ms 604 KB Output is correct
42 Correct 3 ms 596 KB Output is correct
43 Correct 15 ms 608 KB Output is correct
44 Correct 2 ms 596 KB Output is correct
45 Correct 20 ms 596 KB Output is correct
46 Correct 3 ms 596 KB Output is correct
47 Correct 2 ms 596 KB Output is correct
48 Correct 2 ms 468 KB Output is correct
49 Correct 2 ms 596 KB Output is correct
50 Correct 3 ms 596 KB Output is correct
51 Correct 3 ms 596 KB Output is correct
52 Correct 8 ms 1536 KB Output is correct
53 Correct 8 ms 1620 KB Output is correct
54 Correct 9 ms 1568 KB Output is correct
55 Correct 12 ms 1748 KB Output is correct
56 Correct 9 ms 1608 KB Output is correct
57 Correct 7 ms 1620 KB Output is correct
58 Correct 8 ms 1540 KB Output is correct
59 Correct 7 ms 1568 KB Output is correct
60 Correct 9 ms 1536 KB Output is correct
61 Correct 8 ms 1544 KB Output is correct
62 Correct 9 ms 1620 KB Output is correct
63 Correct 7 ms 1620 KB Output is correct
64 Correct 8 ms 1620 KB Output is correct
65 Correct 9 ms 1632 KB Output is correct
66 Correct 7 ms 1620 KB Output is correct
67 Correct 7 ms 1620 KB Output is correct
68 Correct 7 ms 1620 KB Output is correct
69 Correct 7 ms 1620 KB Output is correct
70 Correct 7 ms 1532 KB Output is correct
71 Correct 7 ms 1620 KB Output is correct
72 Correct 7 ms 1756 KB Output is correct
73 Correct 8 ms 1588 KB Output is correct
74 Correct 34 ms 4456 KB Output is correct
75 Correct 852 ms 5308 KB Output is correct
76 Correct 35 ms 5196 KB Output is correct
77 Correct 1002 ms 5308 KB Output is correct
78 Correct 46 ms 5272 KB Output is correct
79 Correct 40 ms 5184 KB Output is correct
80 Correct 845 ms 5308 KB Output is correct
81 Correct 45 ms 5320 KB Output is correct
82 Correct 41 ms 5476 KB Output is correct
83 Correct 47 ms 5208 KB Output is correct
84 Correct 997 ms 5320 KB Output is correct
85 Correct 49 ms 5212 KB Output is correct
86 Correct 798 ms 5288 KB Output is correct
87 Correct 40 ms 5196 KB Output is correct
88 Correct 795 ms 5316 KB Output is correct
89 Correct 31 ms 5152 KB Output is correct
90 Correct 761 ms 5308 KB Output is correct
91 Correct 58 ms 5228 KB Output is correct
92 Correct 1063 ms 5304 KB Output is correct
93 Correct 42 ms 5168 KB Output is correct
94 Correct 24 ms 4852 KB Output is correct
95 Correct 36 ms 5240 KB Output is correct
96 Correct 45 ms 5108 KB Output is correct
97 Correct 62 ms 5620 KB Output is correct
98 Correct 61 ms 5588 KB Output is correct