답안 #261668

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
261668 2020-08-12T00:13:10 Z rama_pang Joker (BOI20_joker) C++14
100 / 100
472 ms 18132 KB
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 200005;

int N, M, Q;
pair<int, int> edges[MAXN];

// Disjoint Set with rollbacks
int par[MAXN * 2];
int sz[MAXN * 2];

vector<pair<int, int>> hist;

int Find(int x) {
  return par[x] == x ? x : Find(par[x]);
}

int Unite(int x, int y) {
  x = Find(x), y = Find(y);
  if (x == y) return 0;
  if (sz[x] > sz[y]) swap(x, y);
  hist.emplace_back(x, par[x]);
  hist.emplace_back(y, sz[y]);
  par[x] = y;
  sz[y] += sz[x];
  return 1;
}

void Rollback(int x) {
  while (x--) {
    sz[hist.back().first] = hist.back().second;
    hist.pop_back();
    par[hist.back().first] = hist.back().second;
    hist.pop_back();
  }
}

int last[MAXN]; // last[L] = highest R, such that erasing edges = [L...R), odd cycle still exist

void FindOpt(int ll, int lr, int rl, int rr) {
  if (ll > lr) return;
  int l = (ll + lr) / 2;
  int r = rr;
  int ops = 0;
  int odd_cycle = 0;
  for (int i = ll; i < l; i++) {
    ops += Unite(edges[i].first + MAXN, edges[i].second);
    ops += Unite(edges[i].first, edges[i].second + MAXN);    
    if (Find(edges[i].first) == Find(edges[i].first + MAXN)) {
      odd_cycle = 1;
      Rollback(ops); ops = 0;
      last[l] = r = M + 1;
      break;
    }
  }
  for (int i = rr; odd_cycle == 0 && i >= max(l, rl); i--) {
    ops += Unite(edges[i].first + MAXN, edges[i].second);
    ops += Unite(edges[i].first, edges[i].second + MAXN);
    if (Find(edges[i].first) == Find(edges[i].first + MAXN)) {
      last[l] = r = i;
      Rollback(ops); ops = 0;
      break;
    }
  }

  odd_cycle = 0;
  for (int i = r + 1; i <= rr; i++) {
    ops += Unite(edges[i].first + MAXN, edges[i].second);
    ops += Unite(edges[i].first, edges[i].second + MAXN);
    if (Find(edges[i].first) == Find(edges[i].first + MAXN)) {
      odd_cycle = 1;
      Rollback(ops); ops = 0;
      for (int j = ll; j < l; j++) {
        last[j] = M + 1;
      }
      break;
    }
  }
  if (odd_cycle == 0) {
    FindOpt(ll, l - 1, rl, r);
    Rollback(ops); ops = 0;
  }

  odd_cycle = 0;
  for (int i = ll; i <= l; i++) {
    ops += Unite(edges[i].first + MAXN, edges[i].second);
    ops += Unite(edges[i].first, edges[i].second + MAXN);
    if (Find(edges[i].first) == Find(edges[i].first + MAXN)) {
      odd_cycle = 1;
      Rollback(ops); ops = 0;
      for (int j = l + 1; j <= lr; j++) {
        last[j] = M + 1;
      }
      break;
    } 
  }
  if (odd_cycle == 0) {
    FindOpt(l + 1, lr, r, rr);
    Rollback(ops); ops = 0;
  }
}

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

  iota(par, par + MAXN * 2, 0);
  fill(sz, sz + MAXN * 2, 1);

  cin >> N >> M >> Q;
  for (int i = 1; i <= M; i++) {
    cin >> edges[i].first >> edges[i].second;
  }
  edges[M + 1].first = N + 1, edges[M + 1].second = N + 2;
  FindOpt(1, M, 1, M + 1);
  for (int i = 1; i <= Q; i++) {
    int l, r;
    cin >> l >> r;
    if (r < last[l]) {
      cout << "YES\n";
    } else {
      cout << "NO\n";
    }
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 3456 KB Output is correct
2 Correct 2 ms 3456 KB Output is correct
3 Correct 3 ms 3456 KB Output is correct
4 Correct 3 ms 3456 KB Output is correct
5 Correct 3 ms 3456 KB Output is correct
6 Correct 3 ms 3456 KB Output is correct
7 Correct 3 ms 3456 KB Output is correct
8 Correct 3 ms 3456 KB Output is correct
9 Correct 3 ms 3456 KB Output is correct
10 Correct 3 ms 3456 KB Output is correct
11 Correct 3 ms 3456 KB Output is correct
12 Correct 3 ms 3456 KB Output is correct
13 Correct 3 ms 3456 KB Output is correct
14 Correct 3 ms 3456 KB Output is correct
15 Correct 3 ms 3456 KB Output is correct
16 Correct 4 ms 3456 KB Output is correct
17 Correct 3 ms 3456 KB Output is correct
18 Correct 3 ms 3456 KB Output is correct
19 Correct 3 ms 3456 KB Output is correct
20 Correct 3 ms 3584 KB Output is correct
21 Correct 3 ms 3456 KB Output is correct
22 Correct 3 ms 3456 KB Output is correct
23 Correct 3 ms 3456 KB Output is correct
24 Correct 3 ms 3456 KB Output is correct
25 Correct 3 ms 3456 KB Output is correct
26 Correct 3 ms 3456 KB Output is correct
27 Correct 3 ms 3456 KB Output is correct
28 Correct 3 ms 3456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 3456 KB Output is correct
2 Correct 2 ms 3456 KB Output is correct
3 Correct 3 ms 3456 KB Output is correct
4 Correct 3 ms 3456 KB Output is correct
5 Correct 3 ms 3456 KB Output is correct
6 Correct 3 ms 3456 KB Output is correct
7 Correct 3 ms 3456 KB Output is correct
8 Correct 3 ms 3456 KB Output is correct
9 Correct 3 ms 3456 KB Output is correct
10 Correct 3 ms 3456 KB Output is correct
11 Correct 3 ms 3456 KB Output is correct
12 Correct 3 ms 3456 KB Output is correct
13 Correct 3 ms 3456 KB Output is correct
14 Correct 3 ms 3456 KB Output is correct
15 Correct 3 ms 3456 KB Output is correct
16 Correct 4 ms 3456 KB Output is correct
17 Correct 3 ms 3456 KB Output is correct
18 Correct 3 ms 3456 KB Output is correct
19 Correct 3 ms 3456 KB Output is correct
20 Correct 3 ms 3584 KB Output is correct
21 Correct 3 ms 3456 KB Output is correct
22 Correct 3 ms 3456 KB Output is correct
23 Correct 3 ms 3456 KB Output is correct
24 Correct 3 ms 3456 KB Output is correct
25 Correct 3 ms 3456 KB Output is correct
26 Correct 3 ms 3456 KB Output is correct
27 Correct 3 ms 3456 KB Output is correct
28 Correct 3 ms 3456 KB Output is correct
29 Correct 4 ms 3584 KB Output is correct
30 Correct 4 ms 3712 KB Output is correct
31 Correct 4 ms 3584 KB Output is correct
32 Correct 4 ms 3584 KB Output is correct
33 Correct 4 ms 3584 KB Output is correct
34 Correct 4 ms 3712 KB Output is correct
35 Correct 5 ms 3712 KB Output is correct
36 Correct 5 ms 3584 KB Output is correct
37 Correct 6 ms 3712 KB Output is correct
38 Correct 4 ms 3712 KB Output is correct
39 Correct 5 ms 3712 KB Output is correct
40 Correct 6 ms 3584 KB Output is correct
41 Correct 4 ms 3584 KB Output is correct
42 Correct 4 ms 3584 KB Output is correct
43 Correct 5 ms 3584 KB Output is correct
44 Correct 5 ms 3584 KB Output is correct
45 Correct 5 ms 3584 KB Output is correct
46 Correct 6 ms 3712 KB Output is correct
47 Correct 4 ms 3584 KB Output is correct
48 Correct 4 ms 3584 KB Output is correct
49 Correct 5 ms 3712 KB Output is correct
50 Correct 5 ms 3712 KB Output is correct
51 Correct 4 ms 3584 KB Output is correct
52 Correct 4 ms 3584 KB Output is correct
53 Correct 5 ms 3584 KB Output is correct
54 Correct 5 ms 3712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 3456 KB Output is correct
2 Correct 2 ms 3456 KB Output is correct
3 Correct 309 ms 13920 KB Output is correct
4 Correct 339 ms 16104 KB Output is correct
5 Correct 334 ms 16480 KB Output is correct
6 Correct 248 ms 13792 KB Output is correct
7 Correct 262 ms 13792 KB Output is correct
8 Correct 284 ms 11880 KB Output is correct
9 Correct 337 ms 12820 KB Output is correct
10 Correct 458 ms 13592 KB Output is correct
11 Correct 327 ms 13304 KB Output is correct
12 Correct 346 ms 14052 KB Output is correct
13 Correct 253 ms 11116 KB Output is correct
14 Correct 293 ms 12392 KB Output is correct
15 Correct 411 ms 13564 KB Output is correct
16 Correct 422 ms 13924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 3456 KB Output is correct
2 Correct 2 ms 3456 KB Output is correct
3 Correct 3 ms 3456 KB Output is correct
4 Correct 3 ms 3456 KB Output is correct
5 Correct 3 ms 3456 KB Output is correct
6 Correct 3 ms 3456 KB Output is correct
7 Correct 3 ms 3456 KB Output is correct
8 Correct 3 ms 3456 KB Output is correct
9 Correct 3 ms 3456 KB Output is correct
10 Correct 3 ms 3456 KB Output is correct
11 Correct 3 ms 3456 KB Output is correct
12 Correct 3 ms 3456 KB Output is correct
13 Correct 3 ms 3456 KB Output is correct
14 Correct 3 ms 3456 KB Output is correct
15 Correct 3 ms 3456 KB Output is correct
16 Correct 4 ms 3456 KB Output is correct
17 Correct 3 ms 3456 KB Output is correct
18 Correct 3 ms 3456 KB Output is correct
19 Correct 3 ms 3456 KB Output is correct
20 Correct 3 ms 3584 KB Output is correct
21 Correct 3 ms 3456 KB Output is correct
22 Correct 3 ms 3456 KB Output is correct
23 Correct 3 ms 3456 KB Output is correct
24 Correct 3 ms 3456 KB Output is correct
25 Correct 3 ms 3456 KB Output is correct
26 Correct 3 ms 3456 KB Output is correct
27 Correct 3 ms 3456 KB Output is correct
28 Correct 3 ms 3456 KB Output is correct
29 Correct 309 ms 13920 KB Output is correct
30 Correct 339 ms 16104 KB Output is correct
31 Correct 334 ms 16480 KB Output is correct
32 Correct 248 ms 13792 KB Output is correct
33 Correct 262 ms 13792 KB Output is correct
34 Correct 284 ms 11880 KB Output is correct
35 Correct 337 ms 12820 KB Output is correct
36 Correct 458 ms 13592 KB Output is correct
37 Correct 327 ms 13304 KB Output is correct
38 Correct 346 ms 14052 KB Output is correct
39 Correct 253 ms 11116 KB Output is correct
40 Correct 293 ms 12392 KB Output is correct
41 Correct 411 ms 13564 KB Output is correct
42 Correct 422 ms 13924 KB Output is correct
43 Correct 310 ms 14012 KB Output is correct
44 Correct 314 ms 16108 KB Output is correct
45 Correct 472 ms 17236 KB Output is correct
46 Correct 247 ms 14224 KB Output is correct
47 Correct 253 ms 14044 KB Output is correct
48 Correct 364 ms 13460 KB Output is correct
49 Correct 425 ms 14108 KB Output is correct
50 Correct 310 ms 13280 KB Output is correct
51 Correct 323 ms 14048 KB Output is correct
52 Correct 412 ms 14280 KB Output is correct
53 Correct 266 ms 11376 KB Output is correct
54 Correct 405 ms 13252 KB Output is correct
55 Correct 415 ms 14048 KB Output is correct
56 Correct 454 ms 14304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 3456 KB Output is correct
2 Correct 2 ms 3456 KB Output is correct
3 Correct 3 ms 3456 KB Output is correct
4 Correct 3 ms 3456 KB Output is correct
5 Correct 3 ms 3456 KB Output is correct
6 Correct 3 ms 3456 KB Output is correct
7 Correct 3 ms 3456 KB Output is correct
8 Correct 3 ms 3456 KB Output is correct
9 Correct 3 ms 3456 KB Output is correct
10 Correct 3 ms 3456 KB Output is correct
11 Correct 3 ms 3456 KB Output is correct
12 Correct 3 ms 3456 KB Output is correct
13 Correct 3 ms 3456 KB Output is correct
14 Correct 3 ms 3456 KB Output is correct
15 Correct 3 ms 3456 KB Output is correct
16 Correct 4 ms 3456 KB Output is correct
17 Correct 3 ms 3456 KB Output is correct
18 Correct 3 ms 3456 KB Output is correct
19 Correct 3 ms 3456 KB Output is correct
20 Correct 3 ms 3584 KB Output is correct
21 Correct 3 ms 3456 KB Output is correct
22 Correct 3 ms 3456 KB Output is correct
23 Correct 3 ms 3456 KB Output is correct
24 Correct 3 ms 3456 KB Output is correct
25 Correct 3 ms 3456 KB Output is correct
26 Correct 3 ms 3456 KB Output is correct
27 Correct 3 ms 3456 KB Output is correct
28 Correct 3 ms 3456 KB Output is correct
29 Correct 4 ms 3584 KB Output is correct
30 Correct 4 ms 3712 KB Output is correct
31 Correct 4 ms 3584 KB Output is correct
32 Correct 4 ms 3584 KB Output is correct
33 Correct 4 ms 3584 KB Output is correct
34 Correct 4 ms 3712 KB Output is correct
35 Correct 5 ms 3712 KB Output is correct
36 Correct 5 ms 3584 KB Output is correct
37 Correct 6 ms 3712 KB Output is correct
38 Correct 4 ms 3712 KB Output is correct
39 Correct 5 ms 3712 KB Output is correct
40 Correct 6 ms 3584 KB Output is correct
41 Correct 4 ms 3584 KB Output is correct
42 Correct 4 ms 3584 KB Output is correct
43 Correct 5 ms 3584 KB Output is correct
44 Correct 5 ms 3584 KB Output is correct
45 Correct 5 ms 3584 KB Output is correct
46 Correct 6 ms 3712 KB Output is correct
47 Correct 4 ms 3584 KB Output is correct
48 Correct 4 ms 3584 KB Output is correct
49 Correct 5 ms 3712 KB Output is correct
50 Correct 5 ms 3712 KB Output is correct
51 Correct 4 ms 3584 KB Output is correct
52 Correct 4 ms 3584 KB Output is correct
53 Correct 5 ms 3584 KB Output is correct
54 Correct 5 ms 3712 KB Output is correct
55 Correct 253 ms 11628 KB Output is correct
56 Correct 272 ms 15980 KB Output is correct
57 Correct 252 ms 16356 KB Output is correct
58 Correct 200 ms 11628 KB Output is correct
59 Correct 325 ms 11628 KB Output is correct
60 Correct 396 ms 11756 KB Output is correct
61 Correct 308 ms 11628 KB Output is correct
62 Correct 298 ms 11756 KB Output is correct
63 Correct 225 ms 9456 KB Output is correct
64 Correct 347 ms 11756 KB Output is correct
65 Correct 412 ms 11824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 3456 KB Output is correct
2 Correct 2 ms 3456 KB Output is correct
3 Correct 3 ms 3456 KB Output is correct
4 Correct 3 ms 3456 KB Output is correct
5 Correct 3 ms 3456 KB Output is correct
6 Correct 3 ms 3456 KB Output is correct
7 Correct 3 ms 3456 KB Output is correct
8 Correct 3 ms 3456 KB Output is correct
9 Correct 3 ms 3456 KB Output is correct
10 Correct 3 ms 3456 KB Output is correct
11 Correct 3 ms 3456 KB Output is correct
12 Correct 3 ms 3456 KB Output is correct
13 Correct 3 ms 3456 KB Output is correct
14 Correct 3 ms 3456 KB Output is correct
15 Correct 3 ms 3456 KB Output is correct
16 Correct 4 ms 3456 KB Output is correct
17 Correct 3 ms 3456 KB Output is correct
18 Correct 3 ms 3456 KB Output is correct
19 Correct 3 ms 3456 KB Output is correct
20 Correct 3 ms 3584 KB Output is correct
21 Correct 3 ms 3456 KB Output is correct
22 Correct 3 ms 3456 KB Output is correct
23 Correct 3 ms 3456 KB Output is correct
24 Correct 3 ms 3456 KB Output is correct
25 Correct 3 ms 3456 KB Output is correct
26 Correct 3 ms 3456 KB Output is correct
27 Correct 3 ms 3456 KB Output is correct
28 Correct 3 ms 3456 KB Output is correct
29 Correct 4 ms 3584 KB Output is correct
30 Correct 4 ms 3712 KB Output is correct
31 Correct 4 ms 3584 KB Output is correct
32 Correct 4 ms 3584 KB Output is correct
33 Correct 4 ms 3584 KB Output is correct
34 Correct 4 ms 3712 KB Output is correct
35 Correct 5 ms 3712 KB Output is correct
36 Correct 5 ms 3584 KB Output is correct
37 Correct 6 ms 3712 KB Output is correct
38 Correct 4 ms 3712 KB Output is correct
39 Correct 5 ms 3712 KB Output is correct
40 Correct 6 ms 3584 KB Output is correct
41 Correct 4 ms 3584 KB Output is correct
42 Correct 4 ms 3584 KB Output is correct
43 Correct 5 ms 3584 KB Output is correct
44 Correct 5 ms 3584 KB Output is correct
45 Correct 5 ms 3584 KB Output is correct
46 Correct 6 ms 3712 KB Output is correct
47 Correct 4 ms 3584 KB Output is correct
48 Correct 4 ms 3584 KB Output is correct
49 Correct 5 ms 3712 KB Output is correct
50 Correct 5 ms 3712 KB Output is correct
51 Correct 4 ms 3584 KB Output is correct
52 Correct 4 ms 3584 KB Output is correct
53 Correct 5 ms 3584 KB Output is correct
54 Correct 5 ms 3712 KB Output is correct
55 Correct 309 ms 13920 KB Output is correct
56 Correct 339 ms 16104 KB Output is correct
57 Correct 334 ms 16480 KB Output is correct
58 Correct 248 ms 13792 KB Output is correct
59 Correct 262 ms 13792 KB Output is correct
60 Correct 284 ms 11880 KB Output is correct
61 Correct 337 ms 12820 KB Output is correct
62 Correct 458 ms 13592 KB Output is correct
63 Correct 327 ms 13304 KB Output is correct
64 Correct 346 ms 14052 KB Output is correct
65 Correct 253 ms 11116 KB Output is correct
66 Correct 293 ms 12392 KB Output is correct
67 Correct 411 ms 13564 KB Output is correct
68 Correct 422 ms 13924 KB Output is correct
69 Correct 310 ms 14012 KB Output is correct
70 Correct 314 ms 16108 KB Output is correct
71 Correct 472 ms 17236 KB Output is correct
72 Correct 247 ms 14224 KB Output is correct
73 Correct 253 ms 14044 KB Output is correct
74 Correct 364 ms 13460 KB Output is correct
75 Correct 425 ms 14108 KB Output is correct
76 Correct 310 ms 13280 KB Output is correct
77 Correct 323 ms 14048 KB Output is correct
78 Correct 412 ms 14280 KB Output is correct
79 Correct 266 ms 11376 KB Output is correct
80 Correct 405 ms 13252 KB Output is correct
81 Correct 415 ms 14048 KB Output is correct
82 Correct 454 ms 14304 KB Output is correct
83 Correct 253 ms 11628 KB Output is correct
84 Correct 272 ms 15980 KB Output is correct
85 Correct 252 ms 16356 KB Output is correct
86 Correct 200 ms 11628 KB Output is correct
87 Correct 325 ms 11628 KB Output is correct
88 Correct 396 ms 11756 KB Output is correct
89 Correct 308 ms 11628 KB Output is correct
90 Correct 298 ms 11756 KB Output is correct
91 Correct 225 ms 9456 KB Output is correct
92 Correct 347 ms 11756 KB Output is correct
93 Correct 412 ms 11824 KB Output is correct
94 Correct 322 ms 14888 KB Output is correct
95 Correct 451 ms 18004 KB Output is correct
96 Correct 358 ms 17364 KB Output is correct
97 Correct 284 ms 14688 KB Output is correct
98 Correct 292 ms 14816 KB Output is correct
99 Correct 332 ms 13544 KB Output is correct
100 Correct 456 ms 15200 KB Output is correct
101 Correct 318 ms 14052 KB Output is correct
102 Correct 377 ms 14688 KB Output is correct
103 Correct 430 ms 15584 KB Output is correct
104 Correct 321 ms 12776 KB Output is correct
105 Correct 404 ms 14560 KB Output is correct
106 Correct 454 ms 14916 KB Output is correct
107 Correct 295 ms 18132 KB Output is correct
108 Correct 325 ms 14560 KB Output is correct
109 Correct 317 ms 14560 KB Output is correct
110 Correct 316 ms 14564 KB Output is correct
111 Correct 320 ms 14560 KB Output is correct
112 Correct 323 ms 14564 KB Output is correct
113 Correct 338 ms 14564 KB Output is correct
114 Correct 382 ms 14560 KB Output is correct
115 Correct 320 ms 14560 KB Output is correct
116 Correct 364 ms 14544 KB Output is correct