Submission #1111782

# Submission time Handle Problem Language Result Execution time Memory
1111782 2024-11-12T20:40:36 Z Kirill22 Tug of War (BOI15_tug) C++17
100 / 100
418 ms 15952 KB
#include "bits/stdc++.h"
 
using namespace std;
 
const int MX = 2 * (int) 1e5;
bitset<MX> dp;
 
void solve() {
    int n, k;
    cin >> n >> k;
    vector<set<array<int, 3>>> g(2 * n);
    vector<pair<int, int>> e(2 * n);
    vector<int> W(2 * n);
    int L = 0, R = 0;
    for (int i = 0; i < 2 * n; i++) {
        int x, y, w;
        cin >> x >> y >> w;
        x--, y--;
        e[i] = {x, y + n};
        W[i] = w;
        g[x].insert({i, y + n, w});
        g[y + n].insert({i, x, w});
    }
    vector<int> deg(2 * n);
    set<pair<int, int>> tmp;
    for (int i = 0; i < 2 * n; i++) {
        deg[i] = (int) g[i].size();
        tmp.insert({deg[i], i});
    }
    while (!tmp.empty()) {
        auto [d, v] = *tmp.begin();
        if (d == 0) {
            cout << "NO\n";
            return;
        }
        tmp.erase(tmp.begin());
        if (d == 1) {
            auto [i, u, w] = *g[v].begin();
            g[v].clear();
            deg[v] = 0;
            (v < n ? L : R) += w;
            tmp.erase({deg[u], u});
            deg[u]--;
            g[u].erase({i, v, w});
            tmp.insert({deg[u], u});
        } else {
            break;
        }
    }
    for (int i = 0; i < 2 * n; i++) {
        assert(deg[i] == 0 || deg[i] == 2);
    }
    dp[MX / 2] = 1;
    vector<int> used(2 * n);
    for (int i = 0; i < 2 * n; i++) {
        if (!used[i] && !g[i].empty()) {
            vector<int> comp = {(*g[i].begin())[0]};
            if (e[comp[0]].first != i) swap(e[comp[0]].first, e[comp[0]].second);
            used[e[comp[0]].second] = 1;
            for (int j = 0; ; j++) {
                bool fnd = false;
                int v = e[comp[j]].second;
                for (auto [id, u, w] : g[v]) {
                    if (id != comp[j] && !used[u]) {
                        fnd = true;
                        comp.push_back(id);
                        used[u] = true;
                        if (e[id].first != v) {
                            swap(e[id].first, e[id].second);
                        }
                        break;
                    }
                }
                if (!fnd) {
                    break;
                }
            }
            int sum = 0;
            for (int i = 0; i < (int) comp.size(); i++) {
                sum += i % 2 == 0 ? W[comp[i]] : -W[comp[i]];
            }
            sum = abs(sum);
//            cout << sum << endl;
            dp = (dp << sum) | (dp >> sum);
        }
    }
//    cout << L << " " << R << endl
    for (int j = 0; j < MX; j++) {
        if (dp[j] && abs(j - MX / 2 + L - R) <= k) {
            cout << "YES\n";
            return;
        }
    }
    cout << "NO\n";
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
//    cin >> t;
    while (t--) {
        solve();
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 2 ms 336 KB Output is correct
7 Correct 1 ms 700 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 504 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 2 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 560 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 532 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 2 ms 336 KB Output is correct
7 Correct 1 ms 700 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 504 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 2 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 560 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 532 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 22 ms 1360 KB Output is correct
27 Correct 6 ms 1548 KB Output is correct
28 Correct 23 ms 1368 KB Output is correct
29 Correct 6 ms 1528 KB Output is correct
30 Correct 24 ms 1360 KB Output is correct
31 Correct 6 ms 1360 KB Output is correct
32 Correct 24 ms 1360 KB Output is correct
33 Correct 6 ms 1360 KB Output is correct
34 Correct 5 ms 1360 KB Output is correct
35 Correct 6 ms 1360 KB Output is correct
36 Correct 24 ms 1360 KB Output is correct
37 Correct 6 ms 1360 KB Output is correct
38 Correct 23 ms 1316 KB Output is correct
39 Correct 9 ms 1360 KB Output is correct
40 Correct 22 ms 1352 KB Output is correct
41 Correct 6 ms 1360 KB Output is correct
42 Correct 24 ms 1368 KB Output is correct
43 Correct 7 ms 1360 KB Output is correct
44 Correct 28 ms 1360 KB Output is correct
45 Correct 6 ms 1360 KB Output is correct
46 Correct 21 ms 1364 KB Output is correct
47 Correct 3 ms 1528 KB Output is correct
48 Correct 13 ms 1360 KB Output is correct
49 Correct 14 ms 1360 KB Output is correct
50 Correct 25 ms 1528 KB Output is correct
51 Correct 23 ms 1360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 70 ms 5456 KB Output is correct
2 Correct 13 ms 5456 KB Output is correct
3 Correct 92 ms 5456 KB Output is correct
4 Correct 12 ms 5456 KB Output is correct
5 Correct 68 ms 5468 KB Output is correct
6 Correct 15 ms 5456 KB Output is correct
7 Correct 73 ms 5456 KB Output is correct
8 Correct 13 ms 5456 KB Output is correct
9 Correct 96 ms 5556 KB Output is correct
10 Correct 12 ms 5456 KB Output is correct
11 Correct 65 ms 5624 KB Output is correct
12 Correct 15 ms 5456 KB Output is correct
13 Correct 68 ms 5624 KB Output is correct
14 Correct 67 ms 5456 KB Output is correct
15 Correct 11 ms 5456 KB Output is correct
16 Correct 81 ms 5456 KB Output is correct
17 Correct 12 ms 5456 KB Output is correct
18 Correct 67 ms 5456 KB Output is correct
19 Correct 12 ms 5200 KB Output is correct
20 Correct 66 ms 5456 KB Output is correct
21 Correct 15 ms 5456 KB Output is correct
22 Correct 44 ms 5456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 2 ms 336 KB Output is correct
7 Correct 1 ms 700 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 504 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 2 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 560 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 532 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 22 ms 1360 KB Output is correct
27 Correct 6 ms 1548 KB Output is correct
28 Correct 23 ms 1368 KB Output is correct
29 Correct 6 ms 1528 KB Output is correct
30 Correct 24 ms 1360 KB Output is correct
31 Correct 6 ms 1360 KB Output is correct
32 Correct 24 ms 1360 KB Output is correct
33 Correct 6 ms 1360 KB Output is correct
34 Correct 5 ms 1360 KB Output is correct
35 Correct 6 ms 1360 KB Output is correct
36 Correct 24 ms 1360 KB Output is correct
37 Correct 6 ms 1360 KB Output is correct
38 Correct 23 ms 1316 KB Output is correct
39 Correct 9 ms 1360 KB Output is correct
40 Correct 22 ms 1352 KB Output is correct
41 Correct 6 ms 1360 KB Output is correct
42 Correct 24 ms 1368 KB Output is correct
43 Correct 7 ms 1360 KB Output is correct
44 Correct 28 ms 1360 KB Output is correct
45 Correct 6 ms 1360 KB Output is correct
46 Correct 21 ms 1364 KB Output is correct
47 Correct 3 ms 1528 KB Output is correct
48 Correct 13 ms 1360 KB Output is correct
49 Correct 14 ms 1360 KB Output is correct
50 Correct 25 ms 1528 KB Output is correct
51 Correct 23 ms 1360 KB Output is correct
52 Correct 70 ms 5456 KB Output is correct
53 Correct 13 ms 5456 KB Output is correct
54 Correct 92 ms 5456 KB Output is correct
55 Correct 12 ms 5456 KB Output is correct
56 Correct 68 ms 5468 KB Output is correct
57 Correct 15 ms 5456 KB Output is correct
58 Correct 73 ms 5456 KB Output is correct
59 Correct 13 ms 5456 KB Output is correct
60 Correct 96 ms 5556 KB Output is correct
61 Correct 12 ms 5456 KB Output is correct
62 Correct 65 ms 5624 KB Output is correct
63 Correct 15 ms 5456 KB Output is correct
64 Correct 68 ms 5624 KB Output is correct
65 Correct 67 ms 5456 KB Output is correct
66 Correct 11 ms 5456 KB Output is correct
67 Correct 81 ms 5456 KB Output is correct
68 Correct 12 ms 5456 KB Output is correct
69 Correct 67 ms 5456 KB Output is correct
70 Correct 12 ms 5200 KB Output is correct
71 Correct 66 ms 5456 KB Output is correct
72 Correct 15 ms 5456 KB Output is correct
73 Correct 44 ms 5456 KB Output is correct
74 Correct 381 ms 15696 KB Output is correct
75 Correct 65 ms 15432 KB Output is correct
76 Correct 326 ms 15704 KB Output is correct
77 Correct 54 ms 15440 KB Output is correct
78 Correct 329 ms 15452 KB Output is correct
79 Correct 337 ms 15436 KB Output is correct
80 Correct 58 ms 15696 KB Output is correct
81 Correct 65 ms 15432 KB Output is correct
82 Correct 321 ms 15440 KB Output is correct
83 Correct 383 ms 15696 KB Output is correct
84 Correct 59 ms 15432 KB Output is correct
85 Correct 360 ms 15600 KB Output is correct
86 Correct 63 ms 15432 KB Output is correct
87 Correct 349 ms 15696 KB Output is correct
88 Correct 61 ms 15696 KB Output is correct
89 Correct 418 ms 15688 KB Output is correct
90 Correct 61 ms 15432 KB Output is correct
91 Correct 381 ms 15952 KB Output is correct
92 Correct 71 ms 15432 KB Output is correct
93 Correct 364 ms 15700 KB Output is correct
94 Correct 37 ms 15440 KB Output is correct
95 Correct 233 ms 15564 KB Output is correct
96 Correct 232 ms 15524 KB Output is correct
97 Correct 355 ms 15688 KB Output is correct
98 Correct 384 ms 15440 KB Output is correct