Submission #652408

# Submission time Handle Problem Language Result Execution time Memory
652408 2022-10-22T13:44:58 Z dooompy Tug of War (BOI15_tug) C++17
100 / 100
2304 ms 11560 KB
#include "bits/stdc++.h"

using namespace std;

void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
    cout << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
    while (l != r) cout << *l << " \n"[++l == r];
}
template <typename A, typename B> istream& operator >> (istream& o, pair<A, B> &a) {
    return o >> a.first >> a.second;
}
template <typename A, typename B> ostream& operator << (ostream& o, pair<A, B> a) {
    return o << '(' << a.first << ", " << a.second << ')';
}
template <typename T> ostream& operator << (ostream& o, vector<T> a) {
    bool is = false;
    for (T i : a) {o << (is ? ' ' : '{'), is = true, o << i;}
    return o << '}';
}

#ifdef local
#define test(args...) abc("[" + string(#args) + "]", args)
#else
#define test(args...) void(0)
#endif

using ll = long long;

multiset<pair<int, int>> adj[100005];
bitset<600005*2> possible;

bool done[100005];

int curct;

void dfs(int node, int par = 1) {
    if (adj[node].empty()) return;

    auto cur = *adj[node].begin();
    adj[node].erase(adj[node].begin());
    adj[cur.first].erase(adj[cur.first].find({node, cur.second}));

    curct += par * cur.second;
    dfs(cur.first, -par);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
//    freopen("", "r", stdin);
//    freopen("", "w", stdout);
    int n, k; cin >> n >> k;
    for (int i = 1; i <= 2 * n; i++) {
        int l, r, s; cin >> l >> r >> s;
        r += n;
        adj[l].insert({r, s});
        adj[r].insert({l, s});
    }

    vector<int> must;

    for (int i = 1; i <= 2 * n; i++) {
        if (adj[i].empty()) {
            cout << "NO" << endl;
            return 0;
        }

        if (adj[i].size() == 1) {
            must.push_back(i);
        }
    }

    int ct = 0;

    while (!must.empty()) {
        auto cur = must.back(); must.pop_back();

        auto to = *adj[cur].begin();
        adj[cur].clear();

        if (cur > n) {
            ct += to.second;
        } else ct -= to.second;

        adj[to.first].erase(adj[to.first].find({cur, to.second}));
        if (adj[to.first].size() == 1) must.push_back(to.first);

    }

    int base = 600000 + ct;
    possible.set(base);

    for (int i = 1; i <= 2 * n; i++) {
        if (adj[i].empty()) {
            continue;
        }

        auto cur = *adj[i].begin();
        adj[i].erase(adj[i].begin());
        adj[cur.first].erase(adj[cur.first].find({i, cur.second}));

        curct = -cur.second;
         dfs(i);

         curct = abs(curct);

        possible = (possible << curct) | (possible >> curct);
    }

    for (int i = -k; i <= k; i++) {
        if (0 <= 600000 + i && 600000 + i < 1200001 && possible[600000 + i]) {
            cout << "YES" << endl;
            return 0;
        }
    }

    cout << "NO" << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5588 KB Output is correct
2 Correct 4 ms 5588 KB Output is correct
3 Correct 4 ms 5588 KB Output is correct
4 Correct 4 ms 5588 KB Output is correct
5 Correct 4 ms 5588 KB Output is correct
6 Correct 4 ms 5588 KB Output is correct
7 Correct 4 ms 5588 KB Output is correct
8 Correct 4 ms 5588 KB Output is correct
9 Correct 4 ms 5588 KB Output is correct
10 Correct 3 ms 5588 KB Output is correct
11 Correct 4 ms 5588 KB Output is correct
12 Correct 4 ms 5588 KB Output is correct
13 Correct 4 ms 5588 KB Output is correct
14 Correct 4 ms 5588 KB Output is correct
15 Correct 4 ms 5588 KB Output is correct
16 Correct 5 ms 5588 KB Output is correct
17 Correct 4 ms 5588 KB Output is correct
18 Correct 4 ms 5588 KB Output is correct
19 Correct 3 ms 5588 KB Output is correct
20 Correct 3 ms 5588 KB Output is correct
21 Correct 3 ms 5332 KB Output is correct
22 Correct 3 ms 5588 KB Output is correct
23 Correct 3 ms 5588 KB Output is correct
24 Correct 4 ms 5588 KB Output is correct
25 Correct 3 ms 5588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5588 KB Output is correct
2 Correct 4 ms 5588 KB Output is correct
3 Correct 4 ms 5588 KB Output is correct
4 Correct 4 ms 5588 KB Output is correct
5 Correct 4 ms 5588 KB Output is correct
6 Correct 4 ms 5588 KB Output is correct
7 Correct 4 ms 5588 KB Output is correct
8 Correct 4 ms 5588 KB Output is correct
9 Correct 4 ms 5588 KB Output is correct
10 Correct 3 ms 5588 KB Output is correct
11 Correct 4 ms 5588 KB Output is correct
12 Correct 4 ms 5588 KB Output is correct
13 Correct 4 ms 5588 KB Output is correct
14 Correct 4 ms 5588 KB Output is correct
15 Correct 4 ms 5588 KB Output is correct
16 Correct 5 ms 5588 KB Output is correct
17 Correct 4 ms 5588 KB Output is correct
18 Correct 4 ms 5588 KB Output is correct
19 Correct 3 ms 5588 KB Output is correct
20 Correct 3 ms 5588 KB Output is correct
21 Correct 3 ms 5332 KB Output is correct
22 Correct 3 ms 5588 KB Output is correct
23 Correct 3 ms 5588 KB Output is correct
24 Correct 4 ms 5588 KB Output is correct
25 Correct 3 ms 5588 KB Output is correct
26 Correct 140 ms 5972 KB Output is correct
27 Correct 24 ms 5972 KB Output is correct
28 Correct 145 ms 5968 KB Output is correct
29 Correct 23 ms 5896 KB Output is correct
30 Correct 144 ms 5972 KB Output is correct
31 Correct 26 ms 5972 KB Output is correct
32 Correct 157 ms 5976 KB Output is correct
33 Correct 25 ms 5972 KB Output is correct
34 Correct 14 ms 5980 KB Output is correct
35 Correct 25 ms 5988 KB Output is correct
36 Correct 155 ms 5972 KB Output is correct
37 Correct 24 ms 5992 KB Output is correct
38 Correct 148 ms 5972 KB Output is correct
39 Correct 27 ms 5976 KB Output is correct
40 Correct 145 ms 5972 KB Output is correct
41 Correct 23 ms 5984 KB Output is correct
42 Correct 138 ms 5972 KB Output is correct
43 Correct 23 ms 5972 KB Output is correct
44 Correct 143 ms 5972 KB Output is correct
45 Correct 24 ms 6100 KB Output is correct
46 Correct 171 ms 5976 KB Output is correct
47 Correct 5 ms 5716 KB Output is correct
48 Correct 74 ms 5972 KB Output is correct
49 Correct 75 ms 5972 KB Output is correct
50 Correct 149 ms 6092 KB Output is correct
51 Correct 153 ms 5972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 358 ms 7464 KB Output is correct
2 Correct 11 ms 7252 KB Output is correct
3 Correct 372 ms 7476 KB Output is correct
4 Correct 11 ms 7252 KB Output is correct
5 Correct 385 ms 7476 KB Output is correct
6 Correct 11 ms 7252 KB Output is correct
7 Correct 430 ms 7468 KB Output is correct
8 Correct 11 ms 7252 KB Output is correct
9 Correct 411 ms 7472 KB Output is correct
10 Correct 13 ms 7380 KB Output is correct
11 Correct 415 ms 7468 KB Output is correct
12 Correct 11 ms 7252 KB Output is correct
13 Correct 390 ms 7464 KB Output is correct
14 Correct 370 ms 7380 KB Output is correct
15 Correct 12 ms 7252 KB Output is correct
16 Correct 416 ms 7472 KB Output is correct
17 Correct 11 ms 7252 KB Output is correct
18 Correct 436 ms 7468 KB Output is correct
19 Correct 15 ms 7252 KB Output is correct
20 Correct 404 ms 7468 KB Output is correct
21 Correct 12 ms 7252 KB Output is correct
22 Correct 206 ms 7532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5588 KB Output is correct
2 Correct 4 ms 5588 KB Output is correct
3 Correct 4 ms 5588 KB Output is correct
4 Correct 4 ms 5588 KB Output is correct
5 Correct 4 ms 5588 KB Output is correct
6 Correct 4 ms 5588 KB Output is correct
7 Correct 4 ms 5588 KB Output is correct
8 Correct 4 ms 5588 KB Output is correct
9 Correct 4 ms 5588 KB Output is correct
10 Correct 3 ms 5588 KB Output is correct
11 Correct 4 ms 5588 KB Output is correct
12 Correct 4 ms 5588 KB Output is correct
13 Correct 4 ms 5588 KB Output is correct
14 Correct 4 ms 5588 KB Output is correct
15 Correct 4 ms 5588 KB Output is correct
16 Correct 5 ms 5588 KB Output is correct
17 Correct 4 ms 5588 KB Output is correct
18 Correct 4 ms 5588 KB Output is correct
19 Correct 3 ms 5588 KB Output is correct
20 Correct 3 ms 5588 KB Output is correct
21 Correct 3 ms 5332 KB Output is correct
22 Correct 3 ms 5588 KB Output is correct
23 Correct 3 ms 5588 KB Output is correct
24 Correct 4 ms 5588 KB Output is correct
25 Correct 3 ms 5588 KB Output is correct
26 Correct 140 ms 5972 KB Output is correct
27 Correct 24 ms 5972 KB Output is correct
28 Correct 145 ms 5968 KB Output is correct
29 Correct 23 ms 5896 KB Output is correct
30 Correct 144 ms 5972 KB Output is correct
31 Correct 26 ms 5972 KB Output is correct
32 Correct 157 ms 5976 KB Output is correct
33 Correct 25 ms 5972 KB Output is correct
34 Correct 14 ms 5980 KB Output is correct
35 Correct 25 ms 5988 KB Output is correct
36 Correct 155 ms 5972 KB Output is correct
37 Correct 24 ms 5992 KB Output is correct
38 Correct 148 ms 5972 KB Output is correct
39 Correct 27 ms 5976 KB Output is correct
40 Correct 145 ms 5972 KB Output is correct
41 Correct 23 ms 5984 KB Output is correct
42 Correct 138 ms 5972 KB Output is correct
43 Correct 23 ms 5972 KB Output is correct
44 Correct 143 ms 5972 KB Output is correct
45 Correct 24 ms 6100 KB Output is correct
46 Correct 171 ms 5976 KB Output is correct
47 Correct 5 ms 5716 KB Output is correct
48 Correct 74 ms 5972 KB Output is correct
49 Correct 75 ms 5972 KB Output is correct
50 Correct 149 ms 6092 KB Output is correct
51 Correct 153 ms 5972 KB Output is correct
52 Correct 358 ms 7464 KB Output is correct
53 Correct 11 ms 7252 KB Output is correct
54 Correct 372 ms 7476 KB Output is correct
55 Correct 11 ms 7252 KB Output is correct
56 Correct 385 ms 7476 KB Output is correct
57 Correct 11 ms 7252 KB Output is correct
58 Correct 430 ms 7468 KB Output is correct
59 Correct 11 ms 7252 KB Output is correct
60 Correct 411 ms 7472 KB Output is correct
61 Correct 13 ms 7380 KB Output is correct
62 Correct 415 ms 7468 KB Output is correct
63 Correct 11 ms 7252 KB Output is correct
64 Correct 390 ms 7464 KB Output is correct
65 Correct 370 ms 7380 KB Output is correct
66 Correct 12 ms 7252 KB Output is correct
67 Correct 416 ms 7472 KB Output is correct
68 Correct 11 ms 7252 KB Output is correct
69 Correct 436 ms 7468 KB Output is correct
70 Correct 15 ms 7252 KB Output is correct
71 Correct 404 ms 7468 KB Output is correct
72 Correct 12 ms 7252 KB Output is correct
73 Correct 206 ms 7532 KB Output is correct
74 Correct 2091 ms 11228 KB Output is correct
75 Correct 125 ms 11240 KB Output is correct
76 Correct 2163 ms 11228 KB Output is correct
77 Correct 114 ms 11224 KB Output is correct
78 Correct 2158 ms 11224 KB Output is correct
79 Correct 2110 ms 11340 KB Output is correct
80 Correct 117 ms 11220 KB Output is correct
81 Correct 120 ms 11204 KB Output is correct
82 Correct 2061 ms 11228 KB Output is correct
83 Correct 2047 ms 11220 KB Output is correct
84 Correct 119 ms 11220 KB Output is correct
85 Correct 2088 ms 11236 KB Output is correct
86 Correct 124 ms 11224 KB Output is correct
87 Correct 2116 ms 11220 KB Output is correct
88 Correct 137 ms 11220 KB Output is correct
89 Correct 2158 ms 11224 KB Output is correct
90 Correct 126 ms 11268 KB Output is correct
91 Correct 2089 ms 11224 KB Output is correct
92 Correct 116 ms 11340 KB Output is correct
93 Correct 2080 ms 11228 KB Output is correct
94 Correct 36 ms 11032 KB Output is correct
95 Correct 1038 ms 11560 KB Output is correct
96 Correct 1052 ms 11468 KB Output is correct
97 Correct 2084 ms 11220 KB Output is correct
98 Correct 2304 ms 11168 KB Output is correct