Submission #890869

# Submission time Handle Problem Language Result Execution time Memory
890869 2023-12-22T04:33:11 Z shiomusubi496 Tug of War (BOI15_tug) C++17
100 / 100
998 ms 5592 KB
#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define all(v) begin(v), end(v)

struct edge {
    int to, cost;
    edge(int t, int c) : to(t), cost(c) {}
};

int main() {
    int n, k; cin >> n >> k;
    vector<vector<edge>> g(2 * n);
    rep (i, 2 * n) {
        int l, r, s; cin >> l >> r >> s;
        g[--l].emplace_back(--r + n, s);
        g[r + n].emplace_back(l, s);
    }
    vector<bool> used(2 * n, false);
    int sm = 0;
    {
        vector<int> deg(2 * n);
        queue<int> que;
        rep (i, 2 * n) {
            deg[i] = g[i].size();
            if (deg[i] == 1) que.push(i);
            if (deg[i] == 0) {
                puts("NO");
                return 0;
            }
        }
        while (!que.empty()) {
            int v = que.front(); que.pop();
            if (deg[v] != 1) {
                puts("NO");
                return 0;
            }
            used[v] = true;
            for (auto e : g[v]) {
                if (used[e.to]) continue;
                if (v < n) sm += e.cost;
                else sm -= e.cost;
                --deg[e.to];
                if (deg[e.to] == 1) que.push(e.to);
            }
        }
    }
    vector<int> a;
    rep (i, 2 * n) {
        if (used[i]) continue;
        used[i] = true;
        int tmp = 0, sgn = 1;
        int v = -1;
        for (auto e : g[i]) {
            if (!used[e.to]) {
                if (v == -1) {
                    v = e.to;
                    tmp += e.cost;
                }
                else {
                    tmp -= e.cost;
                }
            }
        }
        sgn = -sgn;
        while (v != i) {
            used[v] = true;
            bool flag = true;
            for (auto e : g[v]) {
                if (!used[e.to]) {
                    v = e.to;
                    tmp += e.cost * sgn;
                    sgn = -sgn;
                    flag = false;
                    break;
                }
            }
            if (flag) break;
        }
        tmp = abs(tmp);
        sm -= tmp;
        a.push_back(tmp * 2);
    }
    bitset<1200001> dp(1);
    for (auto x : a) dp |= dp << x;
    bool f = false;
    rep2 (i, max(0, -k - sm), k - sm + 1) {
        if (dp[i]) f = true;
    }
    puts(f ? "YES" : "NO");
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 856 KB Output is correct
2 Correct 1 ms 856 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 1 ms 856 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 1 ms 860 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 1 ms 860 KB Output is correct
19 Correct 1 ms 860 KB Output is correct
20 Correct 1 ms 860 KB Output is correct
21 Correct 1 ms 860 KB Output is correct
22 Correct 1 ms 860 KB Output is correct
23 Correct 1 ms 860 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 856 KB Output is correct
2 Correct 1 ms 856 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 1 ms 856 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 1 ms 860 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 1 ms 860 KB Output is correct
19 Correct 1 ms 860 KB Output is correct
20 Correct 1 ms 860 KB Output is correct
21 Correct 1 ms 860 KB Output is correct
22 Correct 1 ms 860 KB Output is correct
23 Correct 1 ms 860 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 64 ms 1116 KB Output is correct
27 Correct 11 ms 1112 KB Output is correct
28 Correct 68 ms 1136 KB Output is correct
29 Correct 11 ms 1116 KB Output is correct
30 Correct 64 ms 1132 KB Output is correct
31 Correct 11 ms 1112 KB Output is correct
32 Correct 64 ms 1116 KB Output is correct
33 Correct 14 ms 1112 KB Output is correct
34 Correct 7 ms 1116 KB Output is correct
35 Correct 12 ms 1116 KB Output is correct
36 Correct 65 ms 1116 KB Output is correct
37 Correct 12 ms 1112 KB Output is correct
38 Correct 65 ms 1116 KB Output is correct
39 Correct 11 ms 1116 KB Output is correct
40 Correct 65 ms 1116 KB Output is correct
41 Correct 16 ms 1148 KB Output is correct
42 Correct 65 ms 1116 KB Output is correct
43 Correct 11 ms 1116 KB Output is correct
44 Correct 66 ms 1128 KB Output is correct
45 Correct 11 ms 1116 KB Output is correct
46 Correct 75 ms 1116 KB Output is correct
47 Correct 3 ms 1132 KB Output is correct
48 Correct 34 ms 1132 KB Output is correct
49 Correct 36 ms 1368 KB Output is correct
50 Correct 75 ms 1116 KB Output is correct
51 Correct 75 ms 1128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 166 ms 2140 KB Output is correct
2 Correct 12 ms 2400 KB Output is correct
3 Correct 162 ms 2312 KB Output is correct
4 Correct 12 ms 2316 KB Output is correct
5 Correct 165 ms 2308 KB Output is correct
6 Correct 12 ms 2396 KB Output is correct
7 Correct 171 ms 2316 KB Output is correct
8 Correct 12 ms 2396 KB Output is correct
9 Correct 164 ms 2316 KB Output is correct
10 Correct 12 ms 2396 KB Output is correct
11 Correct 163 ms 2080 KB Output is correct
12 Correct 12 ms 2392 KB Output is correct
13 Correct 156 ms 2140 KB Output is correct
14 Correct 166 ms 2312 KB Output is correct
15 Correct 13 ms 2392 KB Output is correct
16 Correct 156 ms 2316 KB Output is correct
17 Correct 13 ms 2396 KB Output is correct
18 Correct 158 ms 2140 KB Output is correct
19 Correct 11 ms 2392 KB Output is correct
20 Correct 157 ms 2136 KB Output is correct
21 Correct 11 ms 2140 KB Output is correct
22 Correct 84 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 856 KB Output is correct
2 Correct 1 ms 856 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 1 ms 856 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 1 ms 860 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 1 ms 860 KB Output is correct
19 Correct 1 ms 860 KB Output is correct
20 Correct 1 ms 860 KB Output is correct
21 Correct 1 ms 860 KB Output is correct
22 Correct 1 ms 860 KB Output is correct
23 Correct 1 ms 860 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 64 ms 1116 KB Output is correct
27 Correct 11 ms 1112 KB Output is correct
28 Correct 68 ms 1136 KB Output is correct
29 Correct 11 ms 1116 KB Output is correct
30 Correct 64 ms 1132 KB Output is correct
31 Correct 11 ms 1112 KB Output is correct
32 Correct 64 ms 1116 KB Output is correct
33 Correct 14 ms 1112 KB Output is correct
34 Correct 7 ms 1116 KB Output is correct
35 Correct 12 ms 1116 KB Output is correct
36 Correct 65 ms 1116 KB Output is correct
37 Correct 12 ms 1112 KB Output is correct
38 Correct 65 ms 1116 KB Output is correct
39 Correct 11 ms 1116 KB Output is correct
40 Correct 65 ms 1116 KB Output is correct
41 Correct 16 ms 1148 KB Output is correct
42 Correct 65 ms 1116 KB Output is correct
43 Correct 11 ms 1116 KB Output is correct
44 Correct 66 ms 1128 KB Output is correct
45 Correct 11 ms 1116 KB Output is correct
46 Correct 75 ms 1116 KB Output is correct
47 Correct 3 ms 1132 KB Output is correct
48 Correct 34 ms 1132 KB Output is correct
49 Correct 36 ms 1368 KB Output is correct
50 Correct 75 ms 1116 KB Output is correct
51 Correct 75 ms 1128 KB Output is correct
52 Correct 166 ms 2140 KB Output is correct
53 Correct 12 ms 2400 KB Output is correct
54 Correct 162 ms 2312 KB Output is correct
55 Correct 12 ms 2316 KB Output is correct
56 Correct 165 ms 2308 KB Output is correct
57 Correct 12 ms 2396 KB Output is correct
58 Correct 171 ms 2316 KB Output is correct
59 Correct 12 ms 2396 KB Output is correct
60 Correct 164 ms 2316 KB Output is correct
61 Correct 12 ms 2396 KB Output is correct
62 Correct 163 ms 2080 KB Output is correct
63 Correct 12 ms 2392 KB Output is correct
64 Correct 156 ms 2140 KB Output is correct
65 Correct 166 ms 2312 KB Output is correct
66 Correct 13 ms 2392 KB Output is correct
67 Correct 156 ms 2316 KB Output is correct
68 Correct 13 ms 2396 KB Output is correct
69 Correct 158 ms 2140 KB Output is correct
70 Correct 11 ms 2392 KB Output is correct
71 Correct 157 ms 2136 KB Output is correct
72 Correct 11 ms 2140 KB Output is correct
73 Correct 84 ms 2396 KB Output is correct
74 Correct 967 ms 5028 KB Output is correct
75 Correct 71 ms 5200 KB Output is correct
76 Correct 970 ms 5080 KB Output is correct
77 Correct 76 ms 5200 KB Output is correct
78 Correct 970 ms 5200 KB Output is correct
79 Correct 977 ms 4972 KB Output is correct
80 Correct 74 ms 4968 KB Output is correct
81 Correct 72 ms 4972 KB Output is correct
82 Correct 998 ms 4968 KB Output is correct
83 Correct 961 ms 5228 KB Output is correct
84 Correct 71 ms 4968 KB Output is correct
85 Correct 971 ms 5200 KB Output is correct
86 Correct 71 ms 4964 KB Output is correct
87 Correct 968 ms 4964 KB Output is correct
88 Correct 71 ms 4960 KB Output is correct
89 Correct 961 ms 5200 KB Output is correct
90 Correct 74 ms 4968 KB Output is correct
91 Correct 958 ms 5200 KB Output is correct
92 Correct 73 ms 5212 KB Output is correct
93 Correct 965 ms 4968 KB Output is correct
94 Correct 36 ms 5204 KB Output is correct
95 Correct 507 ms 5592 KB Output is correct
96 Correct 503 ms 5456 KB Output is correct
97 Correct 989 ms 4976 KB Output is correct
98 Correct 994 ms 5232 KB Output is correct