Submission #873834

# Submission time Handle Problem Language Result Execution time Memory
873834 2023-11-15T21:48:25 Z ArashJafariyan Tug of War (BOI15_tug) C++17
0 / 100
75 ms 4572 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];
set<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);
    }

    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(e);
        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) {
        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 1 ms 3420 KB Output is correct
2 Correct 2 ms 3676 KB Output is correct
3 Correct 2 ms 3676 KB Output is correct
4 Correct 2 ms 3676 KB Output is correct
5 Correct 2 ms 3676 KB Output is correct
6 Correct 1 ms 3420 KB Output is correct
7 Correct 2 ms 3788 KB Output is correct
8 Incorrect 1 ms 3420 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3420 KB Output is correct
2 Correct 2 ms 3676 KB Output is correct
3 Correct 2 ms 3676 KB Output is correct
4 Correct 2 ms 3676 KB Output is correct
5 Correct 2 ms 3676 KB Output is correct
6 Correct 1 ms 3420 KB Output is correct
7 Correct 2 ms 3788 KB Output is correct
8 Incorrect 1 ms 3420 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 75 ms 4572 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3420 KB Output is correct
2 Correct 2 ms 3676 KB Output is correct
3 Correct 2 ms 3676 KB Output is correct
4 Correct 2 ms 3676 KB Output is correct
5 Correct 2 ms 3676 KB Output is correct
6 Correct 1 ms 3420 KB Output is correct
7 Correct 2 ms 3788 KB Output is correct
8 Incorrect 1 ms 3420 KB Output isn't correct
9 Halted 0 ms 0 KB -