Submission #890893

# Submission time Handle Problem Language Result Execution time Memory
890893 2023-12-22T04:58:11 Z nahco314 Tug of War (BOI15_tug) C++14
0 / 100
3000 ms 4272 KB
#include <bits/stdc++.h>
using namespace std;

#pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

void ng() {
    cout << "NO" << endl;
    exit(0);
}

int remove(int v, vector<set<tuple<int, int, int>>>& g) {
    if (g[v].size() == 1) {
        tuple<int, int, int> elt = *g[v].begin();
        int l = get<0>(elt);
        int r = get<1>(elt);
        int s = get<2>(elt);
        g[l].erase({l, r, s});
        g[r].erase({r, l, s});
        return s + -remove(r, g);
    }
    return 0;
}

int dfs(int v, int used_e, int sign, vector<set<tuple<int, int, int>>>& g, vector<int>& seen) {
    seen[v] = sign;
    int res = 0;
    sign *= -1;
    for (auto [nxt, s, e] : g[v]) {
        if (e == used_e) continue;
        if (seen[nxt]) {
            if (seen[nxt] == sign) {
                continue;
            } else {
                ng();
            }
        }
        res += sign * s;
        res += dfs(nxt, e, sign, g, seen);
    }
    // cerr << v << " " << used_e << " " << sign << " " << res << endl;
    return res;
}

int main() {
    int n, k;
    cin >> n >> k;
    vector<set<tuple<int, int, int>>> g(2 * n);
    for (int i = 0; i < 2 * n; i++) {
        int l, r, s;
        cin >> l >> r >> s;
        l--;
        r--;
        r += n;
        g[l].emplace(r, s, i);
        g[r].emplace(l, s, i);
    }

    int cur_sum = 0;
    for (int i = 0; i < 2 * n; i++) {
        int sign = i < n ? 1 : -1;
        cur_sum += remove(i, g) * sign;
    }

    vector<int> nums;
    vector<int> seen(2 * n, 0);
    for (int i = 0; i < 2 * n; i++) {
        if (seen[i]) continue;
        if (g[i].size() == 0) continue;
        int l = i;
        int r = get<0>(*g[i].begin());
        int s = get<1>(*g[i].begin());
        int used_e = get<2>(*g[i].begin());

        int num = s + dfs(l, used_e, 1, g, seen);
        nums.push_back(num);
    }

    for (auto a : nums) {
        // cerr << "" << a << endl;
    }

    int m = nums.size();
    int mid = n * 20 / 2;
    bitset<600001> cur(0);
    cur.set(cur_sum + mid);

    for (int i = 0; i < m; i++) {
        int num = abs(nums[i]);
        cur <<= num;
        cur |= cur >> (2 * num);

        /*
        cerr << i << endl;
        for (int i = 0; i < 600001; i++) {
            if (cur.test(i)) {
                cerr << i - mid << " ";
            }
        }
        cerr << endl;
        */
    }

    for (int i = -k; i <= k; i++) {
        int num = mid + i;
        if (cur.test(num)) {
            cout << "YES" << endl;
            exit(0);
        }
    }

    cout << "NO" << endl;
}

Compilation message

tug.cpp: In function 'int dfs(int, int, int, std::vector<std::set<std::tuple<int, int, int> > >&, std::vector<int>&)':
tug.cpp:30:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   30 |     for (auto [nxt, s, e] : g[v]) {
      |               ^
tug.cpp: In function 'int main()':
tug.cpp:72:13: warning: unused variable 'r' [-Wunused-variable]
   72 |         int r = get<0>(*g[i].begin());
      |             ^
tug.cpp:80:15: warning: unused variable 'a' [-Wunused-variable]
   80 |     for (auto a : nums) {
      |               ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 0 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 0 ms 604 KB Output is correct
17 Correct 0 ms 604 KB Output is correct
18 Correct 0 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Execution timed out 3031 ms 600 KB Time limit exceeded
22 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 0 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 0 ms 604 KB Output is correct
17 Correct 0 ms 604 KB Output is correct
18 Correct 0 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Execution timed out 3031 ms 600 KB Time limit exceeded
22 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 52 ms 4188 KB Output is correct
2 Correct 14 ms 4184 KB Output is correct
3 Correct 56 ms 4272 KB Output is correct
4 Correct 14 ms 4184 KB Output is correct
5 Correct 53 ms 4188 KB Output is correct
6 Correct 14 ms 4188 KB Output is correct
7 Correct 53 ms 4188 KB Output is correct
8 Correct 14 ms 4188 KB Output is correct
9 Correct 55 ms 4048 KB Output is correct
10 Correct 14 ms 4184 KB Output is correct
11 Correct 54 ms 4188 KB Output is correct
12 Correct 14 ms 4032 KB Output is correct
13 Correct 53 ms 4184 KB Output is correct
14 Correct 53 ms 4184 KB Output is correct
15 Correct 14 ms 4188 KB Output is correct
16 Correct 53 ms 4188 KB Output is correct
17 Correct 14 ms 4040 KB Output is correct
18 Correct 53 ms 4184 KB Output is correct
19 Execution timed out 3075 ms 3932 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 0 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 0 ms 604 KB Output is correct
17 Correct 0 ms 604 KB Output is correct
18 Correct 0 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Execution timed out 3031 ms 600 KB Time limit exceeded
22 Halted 0 ms 0 KB -