Submission #873841

# Submission time Handle Problem Language Result Execution time Memory
873841 2023-11-15T22:00:18 Z ArashJafariyan Tug of War (BOI15_tug) C++17
100 / 100
851 ms 10588 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef array<int, 2> pii;

#define pb push_back
#define sz(a) (int) (a).size()
#define all(a) (a).begin(), (a).end()

const int N = 3e4 + 4;

int n, k;
bool vis[N << 1];
multiset<pii> g[N << 1];
bitset<N * 20> ks;

bool ok(int v) {
    pii u = *g[v].begin();
    pii w = *g[v].rbegin();
    if (u[0] == w[0]) { 
        int x = u[1];
        int y = w[1];

        int xv = x * (v < n) + y * (u[0] < n);
        int xu = x * (u[0] < n) + y * (v < n);

        ks = (ks << xv) | (ks << xu);

        vis[v] = vis[u[0]] = 1;
        return 0;
    }
    return 1;
}

int dfs(int v, int p, int r, int tp) {
    vis[v] = 1;
    pii u = *g[v].begin();
    
    if (u[0] != p && u[0] != r) {
        int res = dfs(u[0], v, r, tp);
        if (tp == 0 && v < n)
            res += u[1];
        else if (tp == 1 && u[0] < n)
            res += u[1];
        return res;
    }
    else if (u[0] != p && u[0] == r) {
        int res = 0;
        if (tp == 0 && v < n)
            res += u[1];
        else if (tp == 1 && u[0] < n)
            res += u[1];
        return res;
    }
    else { 
        u = *g[v].rbegin();
        if (u[0] != r) {
            int res = dfs(u[0], v, r, tp);
            if (tp == 0 && v < n)
                res += u[1];
            else if (tp == 1 && u[0] < n)
                res += u[1];
            return res;
        }
        else {
            int res = 0;
            if (tp == 0 && v < n)
                res += u[1];
            else if (tp == 1 && u[0] < n)
                res += u[1];
            return res;
        }
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int sum = 0;
    cin >> n >> k;
    for (int i = 0; i < (n << 1); ++i) {
        int l, r, s;
        cin >> l >> r >> s;
        sum += s;
        --l, --r;
        g[l].insert({r + n, s});
        g[r + n].insert({l, s});
    }

    queue<int> ones;
    for (int i = 0; i < (n << 1); ++i) {
        if (!g[i].size()) {
            cout << "NO";
            return 0;
        }
        if (g[i].size() == 1) {
            ones.push(i);
            // cout << i << ' ';
        }
    }

    ks[0] = 1;
    while (!ones.empty()) {
        int v = ones.front();
        pii e = *g[v].begin();
        g[v].clear();
        ones.pop();

        if (v < n)
            ks <<= e[1];
        g[e[0]].erase(g[e[0]].find({v, e[1]}));
        if (g[e[0]].size() == 1) 
            ones.push(e[0]);
        else if (!g[e[0]].size()) {
            cout << "NO";
            return 0;
        }
    }

    for (int i = 0; i < (n << 1); ++i) {
        if (g[i].size() != 0 && g[i].size() != 2) {
            cout << "NO";
            return 0;
        }
    }
    
    for (int i = 0; i < (n << 1); ++i) {
        if (!vis[i] && g[i].size() == 2) 
            ok(i);
        if (!vis[i] && g[i].size() == 2) {
            int res[2] = {dfs(i, i, i, 0), dfs(i, i, i, 1)};
            ks = (ks << res[0]) | (ks << res[1]);
        }
    }

    int mn = N * 20;
    for (int i = 0; i < N * 20; ++i) {
        if (ks[i]) {
            mn = min(mn, abs(i - (sum - i)));
            // cout << i << ' ';
        }
    }

    cout << (mn <= k ? "YES" : "NO");
    // cout << ' ' << mn << ' ' << sum;

    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 2 ms 3676 KB Output is correct
2 Correct 2 ms 3672 KB Output is correct
3 Correct 2 ms 3672 KB Output is correct
4 Correct 2 ms 3676 KB Output is correct
5 Correct 2 ms 3676 KB Output is correct
6 Correct 2 ms 3676 KB Output is correct
7 Correct 2 ms 3676 KB Output is correct
8 Correct 3 ms 3676 KB Output is correct
9 Correct 2 ms 3676 KB Output is correct
10 Correct 3 ms 3676 KB Output is correct
11 Correct 2 ms 3676 KB Output is correct
12 Correct 2 ms 3716 KB Output is correct
13 Correct 2 ms 3676 KB Output is correct
14 Correct 2 ms 3676 KB Output is correct
15 Correct 2 ms 3676 KB Output is correct
16 Correct 2 ms 3672 KB Output is correct
17 Correct 2 ms 3676 KB Output is correct
18 Correct 2 ms 3676 KB Output is correct
19 Correct 2 ms 3676 KB Output is correct
20 Correct 2 ms 3676 KB Output is correct
21 Correct 1 ms 3420 KB Output is correct
22 Correct 2 ms 3676 KB Output is correct
23 Correct 2 ms 3760 KB Output is correct
24 Correct 2 ms 3676 KB Output is correct
25 Correct 2 ms 3672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3676 KB Output is correct
2 Correct 2 ms 3672 KB Output is correct
3 Correct 2 ms 3672 KB Output is correct
4 Correct 2 ms 3676 KB Output is correct
5 Correct 2 ms 3676 KB Output is correct
6 Correct 2 ms 3676 KB Output is correct
7 Correct 2 ms 3676 KB Output is correct
8 Correct 3 ms 3676 KB Output is correct
9 Correct 2 ms 3676 KB Output is correct
10 Correct 3 ms 3676 KB Output is correct
11 Correct 2 ms 3676 KB Output is correct
12 Correct 2 ms 3716 KB Output is correct
13 Correct 2 ms 3676 KB Output is correct
14 Correct 2 ms 3676 KB Output is correct
15 Correct 2 ms 3676 KB Output is correct
16 Correct 2 ms 3672 KB Output is correct
17 Correct 2 ms 3676 KB Output is correct
18 Correct 2 ms 3676 KB Output is correct
19 Correct 2 ms 3676 KB Output is correct
20 Correct 2 ms 3676 KB Output is correct
21 Correct 1 ms 3420 KB Output is correct
22 Correct 2 ms 3676 KB Output is correct
23 Correct 2 ms 3760 KB Output is correct
24 Correct 2 ms 3676 KB Output is correct
25 Correct 2 ms 3672 KB Output is correct
26 Correct 65 ms 4200 KB Output is correct
27 Correct 10 ms 4184 KB Output is correct
28 Correct 60 ms 4204 KB Output is correct
29 Correct 11 ms 4188 KB Output is correct
30 Correct 58 ms 4188 KB Output is correct
31 Correct 11 ms 4188 KB Output is correct
32 Correct 56 ms 4192 KB Output is correct
33 Correct 10 ms 4216 KB Output is correct
34 Correct 7 ms 4384 KB Output is correct
35 Correct 11 ms 4432 KB Output is correct
36 Correct 56 ms 4020 KB Output is correct
37 Correct 11 ms 4188 KB Output is correct
38 Correct 58 ms 4220 KB Output is correct
39 Correct 11 ms 4188 KB Output is correct
40 Correct 57 ms 4188 KB Output is correct
41 Correct 11 ms 4188 KB Output is correct
42 Correct 56 ms 4188 KB Output is correct
43 Correct 11 ms 4188 KB Output is correct
44 Correct 59 ms 4192 KB Output is correct
45 Correct 10 ms 4120 KB Output is correct
46 Correct 57 ms 4188 KB Output is correct
47 Correct 2 ms 3932 KB Output is correct
48 Correct 36 ms 4188 KB Output is correct
49 Correct 36 ms 4188 KB Output is correct
50 Correct 56 ms 4188 KB Output is correct
51 Correct 57 ms 4192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 282 ms 5464 KB Output is correct
2 Correct 9 ms 5720 KB Output is correct
3 Correct 272 ms 5724 KB Output is correct
4 Correct 10 ms 5724 KB Output is correct
5 Correct 275 ms 5912 KB Output is correct
6 Correct 7 ms 5464 KB Output is correct
7 Correct 278 ms 5892 KB Output is correct
8 Correct 7 ms 5468 KB Output is correct
9 Correct 272 ms 5892 KB Output is correct
10 Correct 7 ms 5464 KB Output is correct
11 Correct 277 ms 5912 KB Output is correct
12 Correct 7 ms 5468 KB Output is correct
13 Correct 279 ms 5892 KB Output is correct
14 Correct 277 ms 5720 KB Output is correct
15 Correct 7 ms 5464 KB Output is correct
16 Correct 281 ms 5908 KB Output is correct
17 Correct 7 ms 5464 KB Output is correct
18 Correct 276 ms 5900 KB Output is correct
19 Correct 7 ms 5384 KB Output is correct
20 Correct 278 ms 5724 KB Output is correct
21 Correct 7 ms 5464 KB Output is correct
22 Correct 173 ms 5724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3676 KB Output is correct
2 Correct 2 ms 3672 KB Output is correct
3 Correct 2 ms 3672 KB Output is correct
4 Correct 2 ms 3676 KB Output is correct
5 Correct 2 ms 3676 KB Output is correct
6 Correct 2 ms 3676 KB Output is correct
7 Correct 2 ms 3676 KB Output is correct
8 Correct 3 ms 3676 KB Output is correct
9 Correct 2 ms 3676 KB Output is correct
10 Correct 3 ms 3676 KB Output is correct
11 Correct 2 ms 3676 KB Output is correct
12 Correct 2 ms 3716 KB Output is correct
13 Correct 2 ms 3676 KB Output is correct
14 Correct 2 ms 3676 KB Output is correct
15 Correct 2 ms 3676 KB Output is correct
16 Correct 2 ms 3672 KB Output is correct
17 Correct 2 ms 3676 KB Output is correct
18 Correct 2 ms 3676 KB Output is correct
19 Correct 2 ms 3676 KB Output is correct
20 Correct 2 ms 3676 KB Output is correct
21 Correct 1 ms 3420 KB Output is correct
22 Correct 2 ms 3676 KB Output is correct
23 Correct 2 ms 3760 KB Output is correct
24 Correct 2 ms 3676 KB Output is correct
25 Correct 2 ms 3672 KB Output is correct
26 Correct 65 ms 4200 KB Output is correct
27 Correct 10 ms 4184 KB Output is correct
28 Correct 60 ms 4204 KB Output is correct
29 Correct 11 ms 4188 KB Output is correct
30 Correct 58 ms 4188 KB Output is correct
31 Correct 11 ms 4188 KB Output is correct
32 Correct 56 ms 4192 KB Output is correct
33 Correct 10 ms 4216 KB Output is correct
34 Correct 7 ms 4384 KB Output is correct
35 Correct 11 ms 4432 KB Output is correct
36 Correct 56 ms 4020 KB Output is correct
37 Correct 11 ms 4188 KB Output is correct
38 Correct 58 ms 4220 KB Output is correct
39 Correct 11 ms 4188 KB Output is correct
40 Correct 57 ms 4188 KB Output is correct
41 Correct 11 ms 4188 KB Output is correct
42 Correct 56 ms 4188 KB Output is correct
43 Correct 11 ms 4188 KB Output is correct
44 Correct 59 ms 4192 KB Output is correct
45 Correct 10 ms 4120 KB Output is correct
46 Correct 57 ms 4188 KB Output is correct
47 Correct 2 ms 3932 KB Output is correct
48 Correct 36 ms 4188 KB Output is correct
49 Correct 36 ms 4188 KB Output is correct
50 Correct 56 ms 4188 KB Output is correct
51 Correct 57 ms 4192 KB Output is correct
52 Correct 282 ms 5464 KB Output is correct
53 Correct 9 ms 5720 KB Output is correct
54 Correct 272 ms 5724 KB Output is correct
55 Correct 10 ms 5724 KB Output is correct
56 Correct 275 ms 5912 KB Output is correct
57 Correct 7 ms 5464 KB Output is correct
58 Correct 278 ms 5892 KB Output is correct
59 Correct 7 ms 5468 KB Output is correct
60 Correct 272 ms 5892 KB Output is correct
61 Correct 7 ms 5464 KB Output is correct
62 Correct 277 ms 5912 KB Output is correct
63 Correct 7 ms 5468 KB Output is correct
64 Correct 279 ms 5892 KB Output is correct
65 Correct 277 ms 5720 KB Output is correct
66 Correct 7 ms 5464 KB Output is correct
67 Correct 281 ms 5908 KB Output is correct
68 Correct 7 ms 5464 KB Output is correct
69 Correct 276 ms 5900 KB Output is correct
70 Correct 7 ms 5384 KB Output is correct
71 Correct 278 ms 5724 KB Output is correct
72 Correct 7 ms 5464 KB Output is correct
73 Correct 173 ms 5724 KB Output is correct
74 Correct 837 ms 10432 KB Output is correct
75 Correct 71 ms 10588 KB Output is correct
76 Correct 851 ms 10320 KB Output is correct
77 Correct 56 ms 10264 KB Output is correct
78 Correct 841 ms 10320 KB Output is correct
79 Correct 824 ms 10500 KB Output is correct
80 Correct 56 ms 10120 KB Output is correct
81 Correct 59 ms 10236 KB Output is correct
82 Correct 820 ms 10320 KB Output is correct
83 Correct 818 ms 10068 KB Output is correct
84 Correct 55 ms 10264 KB Output is correct
85 Correct 821 ms 10320 KB Output is correct
86 Correct 55 ms 10076 KB Output is correct
87 Correct 822 ms 10248 KB Output is correct
88 Correct 63 ms 10076 KB Output is correct
89 Correct 819 ms 10148 KB Output is correct
90 Correct 63 ms 10348 KB Output is correct
91 Correct 829 ms 10324 KB Output is correct
92 Correct 55 ms 10064 KB Output is correct
93 Correct 825 ms 10328 KB Output is correct
94 Correct 22 ms 9860 KB Output is correct
95 Correct 524 ms 10576 KB Output is correct
96 Correct 516 ms 10344 KB Output is correct
97 Correct 814 ms 10244 KB Output is correct
98 Correct 845 ms 10344 KB Output is correct