Submission #765272

# Submission time Handle Problem Language Result Execution time Memory
765272 2023-06-24T10:06:56 Z Hegdahl Joker (BOI20_joker) C++17
39 / 100
362 ms 7728 KB
#include <bits/stdc++.h>

using namespace std;

struct UnionFind {
  vector<int> parent_or_neg_size, edge_parity;

  UnionFind(int n) : parent_or_neg_size(n, -1), // hver komponent har til å begynne med størrelse 1
  edge_parity(n, 0) {}

  pair<int, int> find_root_and_parity(int i) {
    int parity = 0;
    while (parent_or_neg_size[i] >= 0) {
      parity ^= edge_parity[i];
      i = parent_or_neg_size[i];
    }
    return {i, parity};
  }

  enum UniteResult { NO_CYCLE, EVEN_CYCLE, ODD_CYCLE };
  UniteResult unite(int i, int j, bool new_edge_parity = true) {
    auto [root_i, parity_i] = find_root_and_parity(i);
    auto [root_j, parity_j] = find_root_and_parity(j);
    if (root_i == root_j) {
      if (parity_i ^ parity_j ^ new_edge_parity) {
        return ODD_CYCLE;
      } else {
        return EVEN_CYCLE;
      }
    }
    if (parent_or_neg_size[root_i] > parent_or_neg_size[root_j]) {
      swap(root_i, root_j);
      swap(parity_i, parity_j);
    }
    parent_or_neg_size[root_i] += parent_or_neg_size[root_j];
    parent_or_neg_size[root_j] = root_i;
    edge_parity[root_j] = parity_i ^ parity_j ^ new_edge_parity;
    return NO_CYCLE;
  }
};

int main() {
  int n, m, q;
  cin >> n >> m >> q;

  vector<array<int, 2>> edges(m);
  for (auto &[u, v] : edges) {
    cin >> u >> v;
    --u;
    --v;
  }

  if ((long long)n * q <= 1e7) {
    while (q--) {
      int l, r;
      cin >> l >> r;
      --l;

      UnionFind uf(n);
      bool odd = false;
      for (int j = 0; j < l; ++j) {
        auto [u, v] = edges[j];
        if (uf.unite(u, v) == UnionFind::ODD_CYCLE) {
          odd = true;
          break;
        }
      }

      for (int j = r; j < m; ++j) {
        auto [u, v] = edges[j];
        if (uf.unite(u, v) == UnionFind::ODD_CYCLE) {
          odd = true;
          break;
        }
      }

      cout << (odd ? "YES" : "NO") << '\n';
    }
  } else {

    int ans = m;
    UnionFind uf(n);
    while (ans) {
      auto [u, v] = edges[ans - 1];
      if (uf.unite(u, v) == UnionFind::ODD_CYCLE) {
        break;
      }
      --ans;
    }

    while (q--) {
      int l, r;
      cin >> l >> r;
      --l;
      assert(l == 0);

      if (r < ans) {
        cout << "YES\n";
      } else {
        cout << "NO\n";
      }
    }
  }
}

# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 300 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 304 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 296 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 296 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 300 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 304 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 296 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 296 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 11 ms 340 KB Output is correct
30 Correct 14 ms 308 KB Output is correct
31 Correct 13 ms 340 KB Output is correct
32 Correct 12 ms 468 KB Output is correct
33 Correct 7 ms 308 KB Output is correct
34 Correct 12 ms 312 KB Output is correct
35 Correct 16 ms 368 KB Output is correct
36 Correct 10 ms 312 KB Output is correct
37 Correct 15 ms 340 KB Output is correct
38 Correct 17 ms 344 KB Output is correct
39 Correct 12 ms 312 KB Output is correct
40 Correct 10 ms 368 KB Output is correct
41 Correct 10 ms 364 KB Output is correct
42 Correct 10 ms 340 KB Output is correct
43 Correct 14 ms 356 KB Output is correct
44 Correct 14 ms 316 KB Output is correct
45 Correct 13 ms 312 KB Output is correct
46 Correct 13 ms 340 KB Output is correct
47 Correct 11 ms 340 KB Output is correct
48 Correct 12 ms 340 KB Output is correct
49 Correct 13 ms 340 KB Output is correct
50 Correct 13 ms 372 KB Output is correct
51 Correct 12 ms 340 KB Output is correct
52 Correct 12 ms 308 KB Output is correct
53 Correct 13 ms 340 KB Output is correct
54 Correct 15 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 362 ms 3216 KB Output is correct
4 Correct 337 ms 4172 KB Output is correct
5 Correct 345 ms 4168 KB Output is correct
6 Correct 354 ms 3536 KB Output is correct
7 Correct 345 ms 3532 KB Output is correct
8 Correct 340 ms 3260 KB Output is correct
9 Correct 351 ms 3660 KB Output is correct
10 Correct 338 ms 4360 KB Output is correct
11 Correct 342 ms 3420 KB Output is correct
12 Correct 352 ms 4144 KB Output is correct
13 Correct 341 ms 2876 KB Output is correct
14 Correct 350 ms 3264 KB Output is correct
15 Correct 346 ms 3772 KB Output is correct
16 Correct 350 ms 4284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 300 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 304 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 296 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 296 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 362 ms 3216 KB Output is correct
30 Correct 337 ms 4172 KB Output is correct
31 Correct 345 ms 4168 KB Output is correct
32 Correct 354 ms 3536 KB Output is correct
33 Correct 345 ms 3532 KB Output is correct
34 Correct 340 ms 3260 KB Output is correct
35 Correct 351 ms 3660 KB Output is correct
36 Correct 338 ms 4360 KB Output is correct
37 Correct 342 ms 3420 KB Output is correct
38 Correct 352 ms 4144 KB Output is correct
39 Correct 341 ms 2876 KB Output is correct
40 Correct 350 ms 3264 KB Output is correct
41 Correct 346 ms 3772 KB Output is correct
42 Correct 350 ms 4284 KB Output is correct
43 Runtime error 82 ms 7728 KB Execution killed with signal 6
44 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 300 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 304 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 296 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 296 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 11 ms 340 KB Output is correct
30 Correct 14 ms 308 KB Output is correct
31 Correct 13 ms 340 KB Output is correct
32 Correct 12 ms 468 KB Output is correct
33 Correct 7 ms 308 KB Output is correct
34 Correct 12 ms 312 KB Output is correct
35 Correct 16 ms 368 KB Output is correct
36 Correct 10 ms 312 KB Output is correct
37 Correct 15 ms 340 KB Output is correct
38 Correct 17 ms 344 KB Output is correct
39 Correct 12 ms 312 KB Output is correct
40 Correct 10 ms 368 KB Output is correct
41 Correct 10 ms 364 KB Output is correct
42 Correct 10 ms 340 KB Output is correct
43 Correct 14 ms 356 KB Output is correct
44 Correct 14 ms 316 KB Output is correct
45 Correct 13 ms 312 KB Output is correct
46 Correct 13 ms 340 KB Output is correct
47 Correct 11 ms 340 KB Output is correct
48 Correct 12 ms 340 KB Output is correct
49 Correct 13 ms 340 KB Output is correct
50 Correct 13 ms 372 KB Output is correct
51 Correct 12 ms 340 KB Output is correct
52 Correct 12 ms 308 KB Output is correct
53 Correct 13 ms 340 KB Output is correct
54 Correct 15 ms 344 KB Output is correct
55 Runtime error 85 ms 5444 KB Execution killed with signal 6
56 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 300 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 304 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 296 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 296 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 11 ms 340 KB Output is correct
30 Correct 14 ms 308 KB Output is correct
31 Correct 13 ms 340 KB Output is correct
32 Correct 12 ms 468 KB Output is correct
33 Correct 7 ms 308 KB Output is correct
34 Correct 12 ms 312 KB Output is correct
35 Correct 16 ms 368 KB Output is correct
36 Correct 10 ms 312 KB Output is correct
37 Correct 15 ms 340 KB Output is correct
38 Correct 17 ms 344 KB Output is correct
39 Correct 12 ms 312 KB Output is correct
40 Correct 10 ms 368 KB Output is correct
41 Correct 10 ms 364 KB Output is correct
42 Correct 10 ms 340 KB Output is correct
43 Correct 14 ms 356 KB Output is correct
44 Correct 14 ms 316 KB Output is correct
45 Correct 13 ms 312 KB Output is correct
46 Correct 13 ms 340 KB Output is correct
47 Correct 11 ms 340 KB Output is correct
48 Correct 12 ms 340 KB Output is correct
49 Correct 13 ms 340 KB Output is correct
50 Correct 13 ms 372 KB Output is correct
51 Correct 12 ms 340 KB Output is correct
52 Correct 12 ms 308 KB Output is correct
53 Correct 13 ms 340 KB Output is correct
54 Correct 15 ms 344 KB Output is correct
55 Correct 362 ms 3216 KB Output is correct
56 Correct 337 ms 4172 KB Output is correct
57 Correct 345 ms 4168 KB Output is correct
58 Correct 354 ms 3536 KB Output is correct
59 Correct 345 ms 3532 KB Output is correct
60 Correct 340 ms 3260 KB Output is correct
61 Correct 351 ms 3660 KB Output is correct
62 Correct 338 ms 4360 KB Output is correct
63 Correct 342 ms 3420 KB Output is correct
64 Correct 352 ms 4144 KB Output is correct
65 Correct 341 ms 2876 KB Output is correct
66 Correct 350 ms 3264 KB Output is correct
67 Correct 346 ms 3772 KB Output is correct
68 Correct 350 ms 4284 KB Output is correct
69 Runtime error 82 ms 7728 KB Execution killed with signal 6
70 Halted 0 ms 0 KB -