Submission #389167

# Submission time Handle Problem Language Result Execution time Memory
389167 2021-04-13T19:05:43 Z Hegdahl Joker (BOI20_joker) C++17
71 / 100
2000 ms 18656 KB
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define ar array

using namespace std;

const int mxN = 2e5;
const int mxM = 2e5;

const int mxR = ceil(sqrt(mxM));

ar<int, 2> edges[mxM];

int boss[mxN], under[mxN], toggle[mxN];

int tmp_boss[mxN], tmp_under[mxN], tmp_toggle[mxN];

vector<int> tmp_changes;

ar<int, 2> find(int i, bool tmp) {
  if (tmp_boss[i]!=-1 || i != boss[i]) {
    int b, bp;

    if (tmp_boss[i] != -1) {
      auto p = find(tmp_boss[i], tmp);
      b = p[0], bp = p[1];
    } else {
      auto p = find(boss[i], tmp);
      b = p[0], bp = p[1];
    }

    if (tmp) {
      tmp_boss[i] = b;

      int was_toggle = tmp_toggle[i];
      if (was_toggle == -1) was_toggle = toggle[i];
      tmp_toggle[i] = was_toggle ^ bp;

      tmp_changes.push_back(i);
    } else {
      boss[i] = b;
      toggle[i] ^= bp;
    }
  }

  int rb = tmp_boss[i], rp = tmp_toggle[i];
  if (rb == -1) rb = boss[i];
  if (rp == -1) rp = toggle[i];

  //if (tmp) cerr << "() ";
  //cerr << "find " << i << ' ' << rb << ' ' << rp << '\n';
  return {rb, rp};
}

bool unite(int _i, int _j, bool tmp) {
  // returns true as long as bipartite

  //cerr << '\n';
  //if (tmp) cerr << "() ";
  //cerr << _i << ' ' << _j << ' ' << tmp << '\n';

  auto [i, hi] = find(_i, tmp);
  auto [j, hj] = find(_j, tmp);

  //cerr << i << ' ' << j << ' ' << '\n';

  if (i == j) {
    return hi != hj;
  }

  //cerr << "h: " << hi << ' ' << hj << '\n';

  int under_i = tmp_under[i], under_j = tmp_under[j];
  if (under_i == -1) under_i = under[i];
  if (under_j == -1) under_j = under[j];

  if (under_i > under_j) swap(i, j);

  if (tmp) {
    tmp_boss[i] = j;
    tmp_under[j] = under_j + under_i;
    tmp_toggle[i] = toggle[i] ^ hi ^ hj ^ 1;

    tmp_changes.push_back(i);
    tmp_changes.push_back(j);
  } else {
    boss[i] = j;
    under[j] = under_j + under_i;

    toggle[i] ^= hi ^ hj ^ 1;
  }

  //cerr << "new boss: " << j << ' ' << toggle[j] << '\n';
  //cerr << "toggle: " << toggle[_i] << ' ' << toggle[_j] << '\n';

  return true;
}

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

  int n, m, q;
  cin >> n >> m >> q;

  fill(tmp_boss, tmp_boss+n, -1);
  fill(tmp_under, tmp_under+n, -1);
  fill(tmp_toggle, tmp_toggle+n, -1);

  for (int mm = 0; mm < m; ++mm)
    for (int i : {0, 1})
      cin >> edges[mm][i], --edges[mm][i];

  iota(boss, boss+n, 0);
  fill(under, under+n, 1);
  fill(toggle, toggle+n, 0);

  bool all_bipartite = true;
  for (int mm = 0; all_bipartite && mm < m; ++mm)
    all_bipartite &= unite(edges[mm][0], edges[mm][1], false);

  if (all_bipartite) {
    for (int qq = 0; qq < q; ++qq)
      cout << "NO\n";
    return 0;
  }

  vector<int> buckets[mxM];


  int mxL = 0;
  vector<ar<int,2>> qs(q);
  for (int qq = 0; qq < q; ++qq) {
    for (int i : {0, 1})
      cin >> qs[qq][i], --qs[qq][i];
    mxL = max(mxL, qs[qq][0]);
  }

  int root = (int)ceil(sqrt((double)n*n/q));
  //cerr << "root " << root << '\n';

  for (int qq = 0; qq < q; ++qq)
    buckets[qs[qq][0]/root].push_back(qq);

  vector<bool> ans(q);

  for (int b = 0; b <= (m-1)/root; ++b) {
    if (buckets[b].empty()) continue;
    sort(buckets[b].begin(), buckets[b].end(), [&](int i, int j){
        return qs[i][1] > qs[j][1];
        });

    //cerr << "RESET\n";

    iota(boss, boss+n, 0);
    fill(under, under+n, 1);
    fill(toggle, toggle+n, 0);

    bool bipartite = true;

    for (int mm = 0; bipartite && mm < m; ++mm) {
      if (mm/root >= b) break;
      bipartite &= unite(edges[mm][0], edges[mm][1], false);
    }

    int R = m-1;

    for (int qq : buckets[b]) {
      //cerr << "qry: " << qs[qq][0] << ' ' << qs[qq][1] << '\n';

      while (bipartite && R > qs[qq][1]) {
        bipartite &= unite(edges[R][0], edges[R][1], false);
        --R;
      }

      bool tmp_bipartite = bipartite;
      int L = b*root;
      while (tmp_bipartite && L < qs[qq][0]) {
        tmp_bipartite &= unite(edges[L][0], edges[L][1], true);
        ++L;
      }

      ans[qq] = tmp_bipartite;

      //cerr << tmp_bipartite << '\n';

      //cerr << "tmp_reset\n";
      while (tmp_changes.size()) {
        int i = tmp_changes.back();
        tmp_changes.pop_back();
        tmp_boss[i] = -1;
        tmp_under[i] = -1;
        tmp_toggle[i] = -1;
      }
    }
  }

  for (bool a : ans)
    cout << (a?"NO\n":"YES\n");

}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 5 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 4 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 4 ms 4940 KB Output is correct
16 Correct 3 ms 4944 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 4 ms 4940 KB Output is correct
19 Correct 3 ms 4940 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 5 ms 4940 KB Output is correct
22 Correct 3 ms 4940 KB Output is correct
23 Correct 3 ms 4936 KB Output is correct
24 Correct 3 ms 4940 KB Output is correct
25 Correct 3 ms 4944 KB Output is correct
26 Correct 3 ms 4940 KB Output is correct
27 Correct 4 ms 4940 KB Output is correct
28 Correct 3 ms 4940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 5 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 4 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 4 ms 4940 KB Output is correct
16 Correct 3 ms 4944 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 4 ms 4940 KB Output is correct
19 Correct 3 ms 4940 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 5 ms 4940 KB Output is correct
22 Correct 3 ms 4940 KB Output is correct
23 Correct 3 ms 4936 KB Output is correct
24 Correct 3 ms 4940 KB Output is correct
25 Correct 3 ms 4944 KB Output is correct
26 Correct 3 ms 4940 KB Output is correct
27 Correct 4 ms 4940 KB Output is correct
28 Correct 3 ms 4940 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 8 ms 5068 KB Output is correct
31 Correct 6 ms 5068 KB Output is correct
32 Correct 8 ms 5144 KB Output is correct
33 Correct 5 ms 5068 KB Output is correct
34 Correct 5 ms 5068 KB Output is correct
35 Correct 6 ms 5068 KB Output is correct
36 Correct 6 ms 5068 KB Output is correct
37 Correct 8 ms 5068 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 6 ms 5068 KB Output is correct
40 Correct 8 ms 5068 KB Output is correct
41 Correct 6 ms 5068 KB Output is correct
42 Correct 6 ms 5068 KB Output is correct
43 Correct 8 ms 5120 KB Output is correct
44 Correct 6 ms 5068 KB Output is correct
45 Correct 5 ms 5068 KB Output is correct
46 Correct 5 ms 5068 KB Output is correct
47 Correct 7 ms 5068 KB Output is correct
48 Correct 7 ms 5112 KB Output is correct
49 Correct 6 ms 5072 KB Output is correct
50 Correct 6 ms 5132 KB Output is correct
51 Correct 8 ms 5132 KB Output is correct
52 Correct 7 ms 5132 KB Output is correct
53 Correct 6 ms 5068 KB Output is correct
54 Correct 8 ms 5072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 109 ms 12140 KB Output is correct
4 Correct 75 ms 7140 KB Output is correct
5 Correct 113 ms 13924 KB Output is correct
6 Correct 114 ms 14652 KB Output is correct
7 Correct 125 ms 14684 KB Output is correct
8 Correct 106 ms 13896 KB Output is correct
9 Correct 109 ms 14820 KB Output is correct
10 Correct 118 ms 17080 KB Output is correct
11 Correct 112 ms 14640 KB Output is correct
12 Correct 115 ms 16576 KB Output is correct
13 Correct 104 ms 12788 KB Output is correct
14 Correct 115 ms 13696 KB Output is correct
15 Correct 119 ms 15612 KB Output is correct
16 Correct 121 ms 16960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 5 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 4 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 4 ms 4940 KB Output is correct
16 Correct 3 ms 4944 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 4 ms 4940 KB Output is correct
19 Correct 3 ms 4940 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 5 ms 4940 KB Output is correct
22 Correct 3 ms 4940 KB Output is correct
23 Correct 3 ms 4936 KB Output is correct
24 Correct 3 ms 4940 KB Output is correct
25 Correct 3 ms 4944 KB Output is correct
26 Correct 3 ms 4940 KB Output is correct
27 Correct 4 ms 4940 KB Output is correct
28 Correct 3 ms 4940 KB Output is correct
29 Correct 109 ms 12140 KB Output is correct
30 Correct 75 ms 7140 KB Output is correct
31 Correct 113 ms 13924 KB Output is correct
32 Correct 114 ms 14652 KB Output is correct
33 Correct 125 ms 14684 KB Output is correct
34 Correct 106 ms 13896 KB Output is correct
35 Correct 109 ms 14820 KB Output is correct
36 Correct 118 ms 17080 KB Output is correct
37 Correct 112 ms 14640 KB Output is correct
38 Correct 115 ms 16576 KB Output is correct
39 Correct 104 ms 12788 KB Output is correct
40 Correct 115 ms 13696 KB Output is correct
41 Correct 119 ms 15612 KB Output is correct
42 Correct 121 ms 16960 KB Output is correct
43 Correct 987 ms 16348 KB Output is correct
44 Correct 61 ms 9732 KB Output is correct
45 Correct 1056 ms 18656 KB Output is correct
46 Correct 855 ms 16400 KB Output is correct
47 Correct 883 ms 16448 KB Output is correct
48 Correct 216 ms 16036 KB Output is correct
49 Correct 300 ms 18624 KB Output is correct
50 Correct 936 ms 15756 KB Output is correct
51 Correct 1151 ms 17308 KB Output is correct
52 Correct 1037 ms 18640 KB Output is correct
53 Correct 271 ms 14420 KB Output is correct
54 Correct 963 ms 15812 KB Output is correct
55 Correct 1240 ms 17448 KB Output is correct
56 Correct 1262 ms 18656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 5 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 4 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 4 ms 4940 KB Output is correct
16 Correct 3 ms 4944 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 4 ms 4940 KB Output is correct
19 Correct 3 ms 4940 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 5 ms 4940 KB Output is correct
22 Correct 3 ms 4940 KB Output is correct
23 Correct 3 ms 4936 KB Output is correct
24 Correct 3 ms 4940 KB Output is correct
25 Correct 3 ms 4944 KB Output is correct
26 Correct 3 ms 4940 KB Output is correct
27 Correct 4 ms 4940 KB Output is correct
28 Correct 3 ms 4940 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 8 ms 5068 KB Output is correct
31 Correct 6 ms 5068 KB Output is correct
32 Correct 8 ms 5144 KB Output is correct
33 Correct 5 ms 5068 KB Output is correct
34 Correct 5 ms 5068 KB Output is correct
35 Correct 6 ms 5068 KB Output is correct
36 Correct 6 ms 5068 KB Output is correct
37 Correct 8 ms 5068 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 6 ms 5068 KB Output is correct
40 Correct 8 ms 5068 KB Output is correct
41 Correct 6 ms 5068 KB Output is correct
42 Correct 6 ms 5068 KB Output is correct
43 Correct 8 ms 5120 KB Output is correct
44 Correct 6 ms 5068 KB Output is correct
45 Correct 5 ms 5068 KB Output is correct
46 Correct 5 ms 5068 KB Output is correct
47 Correct 7 ms 5068 KB Output is correct
48 Correct 7 ms 5112 KB Output is correct
49 Correct 6 ms 5072 KB Output is correct
50 Correct 6 ms 5132 KB Output is correct
51 Correct 8 ms 5132 KB Output is correct
52 Correct 7 ms 5132 KB Output is correct
53 Correct 6 ms 5068 KB Output is correct
54 Correct 8 ms 5072 KB Output is correct
55 Correct 541 ms 9160 KB Output is correct
56 Correct 55 ms 9028 KB Output is correct
57 Correct 490 ms 13488 KB Output is correct
58 Correct 436 ms 11460 KB Output is correct
59 Correct 581 ms 11368 KB Output is correct
60 Correct 386 ms 12996 KB Output is correct
61 Correct 488 ms 11384 KB Output is correct
62 Correct 425 ms 13508 KB Output is correct
63 Correct 954 ms 9876 KB Output is correct
64 Correct 587 ms 11964 KB Output is correct
65 Correct 532 ms 13604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 5 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 4 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 4 ms 4940 KB Output is correct
16 Correct 3 ms 4944 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 4 ms 4940 KB Output is correct
19 Correct 3 ms 4940 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 5 ms 4940 KB Output is correct
22 Correct 3 ms 4940 KB Output is correct
23 Correct 3 ms 4936 KB Output is correct
24 Correct 3 ms 4940 KB Output is correct
25 Correct 3 ms 4944 KB Output is correct
26 Correct 3 ms 4940 KB Output is correct
27 Correct 4 ms 4940 KB Output is correct
28 Correct 3 ms 4940 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 8 ms 5068 KB Output is correct
31 Correct 6 ms 5068 KB Output is correct
32 Correct 8 ms 5144 KB Output is correct
33 Correct 5 ms 5068 KB Output is correct
34 Correct 5 ms 5068 KB Output is correct
35 Correct 6 ms 5068 KB Output is correct
36 Correct 6 ms 5068 KB Output is correct
37 Correct 8 ms 5068 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 6 ms 5068 KB Output is correct
40 Correct 8 ms 5068 KB Output is correct
41 Correct 6 ms 5068 KB Output is correct
42 Correct 6 ms 5068 KB Output is correct
43 Correct 8 ms 5120 KB Output is correct
44 Correct 6 ms 5068 KB Output is correct
45 Correct 5 ms 5068 KB Output is correct
46 Correct 5 ms 5068 KB Output is correct
47 Correct 7 ms 5068 KB Output is correct
48 Correct 7 ms 5112 KB Output is correct
49 Correct 6 ms 5072 KB Output is correct
50 Correct 6 ms 5132 KB Output is correct
51 Correct 8 ms 5132 KB Output is correct
52 Correct 7 ms 5132 KB Output is correct
53 Correct 6 ms 5068 KB Output is correct
54 Correct 8 ms 5072 KB Output is correct
55 Correct 109 ms 12140 KB Output is correct
56 Correct 75 ms 7140 KB Output is correct
57 Correct 113 ms 13924 KB Output is correct
58 Correct 114 ms 14652 KB Output is correct
59 Correct 125 ms 14684 KB Output is correct
60 Correct 106 ms 13896 KB Output is correct
61 Correct 109 ms 14820 KB Output is correct
62 Correct 118 ms 17080 KB Output is correct
63 Correct 112 ms 14640 KB Output is correct
64 Correct 115 ms 16576 KB Output is correct
65 Correct 104 ms 12788 KB Output is correct
66 Correct 115 ms 13696 KB Output is correct
67 Correct 119 ms 15612 KB Output is correct
68 Correct 121 ms 16960 KB Output is correct
69 Correct 987 ms 16348 KB Output is correct
70 Correct 61 ms 9732 KB Output is correct
71 Correct 1056 ms 18656 KB Output is correct
72 Correct 855 ms 16400 KB Output is correct
73 Correct 883 ms 16448 KB Output is correct
74 Correct 216 ms 16036 KB Output is correct
75 Correct 300 ms 18624 KB Output is correct
76 Correct 936 ms 15756 KB Output is correct
77 Correct 1151 ms 17308 KB Output is correct
78 Correct 1037 ms 18640 KB Output is correct
79 Correct 271 ms 14420 KB Output is correct
80 Correct 963 ms 15812 KB Output is correct
81 Correct 1240 ms 17448 KB Output is correct
82 Correct 1262 ms 18656 KB Output is correct
83 Correct 541 ms 9160 KB Output is correct
84 Correct 55 ms 9028 KB Output is correct
85 Correct 490 ms 13488 KB Output is correct
86 Correct 436 ms 11460 KB Output is correct
87 Correct 581 ms 11368 KB Output is correct
88 Correct 386 ms 12996 KB Output is correct
89 Correct 488 ms 11384 KB Output is correct
90 Correct 425 ms 13508 KB Output is correct
91 Correct 954 ms 9876 KB Output is correct
92 Correct 587 ms 11964 KB Output is correct
93 Correct 532 ms 13604 KB Output is correct
94 Execution timed out 2011 ms 16444 KB Time limit exceeded
95 Halted 0 ms 0 KB -