Submission #821801

# Submission time Handle Problem Language Result Execution time Memory
821801 2023-08-11T17:16:42 Z tch1cherin Tug of War (BOI15_tug) C++17
100 / 100
486 ms 17604 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 30000 * 20 + 1;

void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> l(2 * n), r(2 * n), s(2 * n);
  vector idx(2, vector<set<int>>(n));
  vector cnt(2, vector<int>(n));
  for (int i = 0; i < 2 * n; i++) {
    cin >> l[i] >> r[i] >> s[i];
    l[i]--, r[i]--;
    idx[0][l[i]].insert(i);
    idx[1][r[i]].insert(i);
    cnt[0][l[i]]++, cnt[1][r[i]]++;
  }
  queue<pair<int, int>> q;
  for (int i = 0; i < n; i++) {
    if (cnt[0][i] <= 1) {
      q.push({i, 0});
    }
    if (cnt[1][i] <= 1) {
      q.push({i, 1});
    }
  }
  vector<bool> deleted(2 * n, false);
  vector used(2, vector<bool>(n, false));
  vector g(2, vector<vector<int>>(2 * n));
  int diff = 0;
  while (!q.empty()) {
    auto [i, t] = q.front();
    q.pop();
    if (cnt[t][i] == 0) {
      cout << "NO\n";
      return;
    }
    int j = *idx[t][i].begin();
    if (t == 0) {
      diff += s[j];
    } else {
      diff -= s[j]; 
    }
    used[t][i] = true;
    deleted[j] = true;
    cnt[0][l[j]]--;
    cnt[1][r[j]]--;
    idx[0][l[j]].erase(j);
    idx[1][r[j]].erase(j);
    if (cnt[0][l[j]] == 1) {
      q.push({l[j], 0});
    }
    if (cnt[1][r[j]] == 1) {
      q.push({r[j], 1});
    }
  }
  for (int i = 0; i < 2 * n; i++) {
    if (!deleted[i]) {
      if (!used[0][l[i]]) {
        g[0][i].push_back(l[i]);
        g[1][l[i]].push_back(i);
      }
      if (!used[1][r[i]]) {
        g[0][i].push_back(r[i] + n);
        g[1][r[i] + n].push_back(i);
      }
    }
  }
  int cost0 = 0;
  vector<int> d;
  int sum = 0;
  for (int i = 0; i < 2 * n; i++) {
    if (!deleted[i]) {
      sum += s[i];
    }
  }
  for (int i = 0; i < 2 * n; i++) {
    if (deleted[i]) {
      continue;
    }
    int j = i, par = -1, step = 0, ss = 0, c0 = 0, c1 = 0;
    bool first = true;
    while (first || j != i || step != 0) {
      if (step == 0) {
        deleted[j] = true;
        if (ss == 0) {
          c0 += s[j];
        } else {
          c1 += s[j];
        }
        ss ^= 1;
      }
      first = false;
      if (g[step][j][0] == par) {
        par = j;
        j = g[step][j][1];
      } else {
        par = j;
        j = g[step][j][0];
      }
      step ^= 1;
    }
    cost0 += c0;
    d.push_back(c1 - c0);
  }
  bitset<N> b;
  b[cost0] = 1;
  for (int v : d) {
    if (v < 0) {
      b |= b >> (-v);
    } else {
      b |= b << v;
    }
  }
  for (int i = 0; i <= sum; i++) {
    int j = sum - i;
    if (b[i] && abs(i - j + diff) <= k) {
      cout << "YES\n";
      return;
    }
  }
  cout << "NO\n";
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  solve();
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 536 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 536 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 540 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 536 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 536 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 540 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 31 ms 1648 KB Output is correct
27 Correct 6 ms 1620 KB Output is correct
28 Correct 31 ms 1620 KB Output is correct
29 Correct 6 ms 1592 KB Output is correct
30 Correct 38 ms 1648 KB Output is correct
31 Correct 7 ms 1620 KB Output is correct
32 Correct 30 ms 1676 KB Output is correct
33 Correct 8 ms 1576 KB Output is correct
34 Correct 5 ms 1548 KB Output is correct
35 Correct 8 ms 1580 KB Output is correct
36 Correct 30 ms 1664 KB Output is correct
37 Correct 6 ms 1620 KB Output is correct
38 Correct 31 ms 1620 KB Output is correct
39 Correct 6 ms 1620 KB Output is correct
40 Correct 31 ms 1572 KB Output is correct
41 Correct 6 ms 1656 KB Output is correct
42 Correct 31 ms 1620 KB Output is correct
43 Correct 6 ms 1576 KB Output is correct
44 Correct 30 ms 1620 KB Output is correct
45 Correct 6 ms 1620 KB Output is correct
46 Correct 30 ms 1620 KB Output is correct
47 Correct 3 ms 1492 KB Output is correct
48 Correct 17 ms 1432 KB Output is correct
49 Correct 16 ms 1540 KB Output is correct
50 Correct 31 ms 1660 KB Output is correct
51 Correct 31 ms 1620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 99 ms 6220 KB Output is correct
2 Correct 9 ms 5388 KB Output is correct
3 Correct 102 ms 6140 KB Output is correct
4 Correct 9 ms 5332 KB Output is correct
5 Correct 110 ms 6148 KB Output is correct
6 Correct 8 ms 5372 KB Output is correct
7 Correct 95 ms 6140 KB Output is correct
8 Correct 8 ms 5372 KB Output is correct
9 Correct 98 ms 6188 KB Output is correct
10 Correct 9 ms 5324 KB Output is correct
11 Correct 97 ms 6140 KB Output is correct
12 Correct 8 ms 5316 KB Output is correct
13 Correct 115 ms 6204 KB Output is correct
14 Correct 96 ms 6212 KB Output is correct
15 Correct 9 ms 5328 KB Output is correct
16 Correct 99 ms 6212 KB Output is correct
17 Correct 8 ms 5332 KB Output is correct
18 Correct 96 ms 6220 KB Output is correct
19 Correct 10 ms 5300 KB Output is correct
20 Correct 123 ms 6224 KB Output is correct
21 Correct 9 ms 5372 KB Output is correct
22 Correct 53 ms 5528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 536 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 536 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 540 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 31 ms 1648 KB Output is correct
27 Correct 6 ms 1620 KB Output is correct
28 Correct 31 ms 1620 KB Output is correct
29 Correct 6 ms 1592 KB Output is correct
30 Correct 38 ms 1648 KB Output is correct
31 Correct 7 ms 1620 KB Output is correct
32 Correct 30 ms 1676 KB Output is correct
33 Correct 8 ms 1576 KB Output is correct
34 Correct 5 ms 1548 KB Output is correct
35 Correct 8 ms 1580 KB Output is correct
36 Correct 30 ms 1664 KB Output is correct
37 Correct 6 ms 1620 KB Output is correct
38 Correct 31 ms 1620 KB Output is correct
39 Correct 6 ms 1620 KB Output is correct
40 Correct 31 ms 1572 KB Output is correct
41 Correct 6 ms 1656 KB Output is correct
42 Correct 31 ms 1620 KB Output is correct
43 Correct 6 ms 1576 KB Output is correct
44 Correct 30 ms 1620 KB Output is correct
45 Correct 6 ms 1620 KB Output is correct
46 Correct 30 ms 1620 KB Output is correct
47 Correct 3 ms 1492 KB Output is correct
48 Correct 17 ms 1432 KB Output is correct
49 Correct 16 ms 1540 KB Output is correct
50 Correct 31 ms 1660 KB Output is correct
51 Correct 31 ms 1620 KB Output is correct
52 Correct 99 ms 6220 KB Output is correct
53 Correct 9 ms 5388 KB Output is correct
54 Correct 102 ms 6140 KB Output is correct
55 Correct 9 ms 5332 KB Output is correct
56 Correct 110 ms 6148 KB Output is correct
57 Correct 8 ms 5372 KB Output is correct
58 Correct 95 ms 6140 KB Output is correct
59 Correct 8 ms 5372 KB Output is correct
60 Correct 98 ms 6188 KB Output is correct
61 Correct 9 ms 5324 KB Output is correct
62 Correct 97 ms 6140 KB Output is correct
63 Correct 8 ms 5316 KB Output is correct
64 Correct 115 ms 6204 KB Output is correct
65 Correct 96 ms 6212 KB Output is correct
66 Correct 9 ms 5328 KB Output is correct
67 Correct 99 ms 6212 KB Output is correct
68 Correct 8 ms 5332 KB Output is correct
69 Correct 96 ms 6220 KB Output is correct
70 Correct 10 ms 5300 KB Output is correct
71 Correct 123 ms 6224 KB Output is correct
72 Correct 9 ms 5372 KB Output is correct
73 Correct 53 ms 5528 KB Output is correct
74 Correct 457 ms 17596 KB Output is correct
75 Correct 59 ms 17228 KB Output is correct
76 Correct 457 ms 17596 KB Output is correct
77 Correct 58 ms 17228 KB Output is correct
78 Correct 486 ms 17588 KB Output is correct
79 Correct 460 ms 17592 KB Output is correct
80 Correct 61 ms 17248 KB Output is correct
81 Correct 58 ms 17260 KB Output is correct
82 Correct 480 ms 17592 KB Output is correct
83 Correct 460 ms 17600 KB Output is correct
84 Correct 64 ms 17236 KB Output is correct
85 Correct 463 ms 17604 KB Output is correct
86 Correct 77 ms 17300 KB Output is correct
87 Correct 465 ms 17588 KB Output is correct
88 Correct 67 ms 17468 KB Output is correct
89 Correct 454 ms 17584 KB Output is correct
90 Correct 67 ms 17252 KB Output is correct
91 Correct 459 ms 17588 KB Output is correct
92 Correct 56 ms 17236 KB Output is correct
93 Correct 457 ms 17588 KB Output is correct
94 Correct 28 ms 15052 KB Output is correct
95 Correct 250 ms 15428 KB Output is correct
96 Correct 250 ms 15180 KB Output is correct
97 Correct 480 ms 17584 KB Output is correct
98 Correct 461 ms 17484 KB Output is correct