Submission #877277

# Submission time Handle Problem Language Result Execution time Memory
877277 2023-11-23T05:21:15 Z TranGiaHuy1508 Joker (BOI20_joker) C++17
100 / 100
343 ms 45036 KB
#include <bits/stdc++.h>
using namespace std;

void main_program();

signed main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	main_program();
}

using ii = pair<int, int>;

struct DSU_Rollback_Bipartite{
	using Change = pair<int*, int>;
	using Changes = vector<Change>;

	vector<Changes> changes;
	vector<ii> parent;
	vector<int> size;
	int is_bipartite;

	DSU_Rollback_Bipartite() {}
	DSU_Rollback_Bipartite(int N): parent(N), size(N, 1), is_bipartite(1) {
		for (int i = 0; i < N; i++){
			parent[i] = {i, 0};
		}
	}

	ii get(int x){
		if (x == parent[x].first) return {x, 0};
		auto [p, d] = get(parent[x].first);
		return {p, d ^ parent[x].second};
	}

	void merge(int _a, int _b){
		changes.emplace_back();

		auto [a, da] = get(_a);
		auto [b, db] = get(_b);

		if (a == b){
			if (da == db){
				changes.back().emplace_back(&is_bipartite, is_bipartite);
				is_bipartite = 0;
			}
			return;
		}

		if (size[a] < size[b]) swap(a, b);

		changes.back().emplace_back(&parent[b].first, parent[b].first);
		changes.back().emplace_back(&parent[b].second, parent[b].second);
		changes.back().emplace_back(&size[a], size[a]);

		parent[b] = {a, da ^ db ^ 1};
		size[a] += size[b];
	}

	void rollback(){
		for (auto [pointer, value]: changes.back()){
			*pointer = value;
		}
		changes.pop_back();
	}
};

struct DSU_Queue_Rollback{
	using Query = pair<ii, int>;
	vector<Query> st;
	DSU_Rollback_Bipartite ds;
	int bottom, _n;

	DSU_Queue_Rollback() {}
	DSU_Queue_Rollback(int N): ds(N * 2), bottom(0), _n(N) {}

	void merge(int a, int b){
		ds.merge(a, b);
		st.push_back({{a, b}, 0});
	}

	int is_bipartite(){
		return ds.is_bipartite;
	}

	void advance_bottom(){
		while (bottom < (int)st.size() && st[bottom].second == 0) bottom++;
	}

	void flip_all(){
		for (int i = 0; i < (int)st.size(); i++) ds.rollback();
		reverse(st.begin(), st.end());
		for (int i = 0; i < (int)st.size(); i++){
			ds.merge(st[i].first.first, st[i].first.second);
			st[i].second = 1;
		}
		bottom = 0;
	}

	void fix(){
		if (st.empty() || st.back().second == 1) return;
		vector<Query> qA, qB;

		advance_bottom();
		while ((qA.empty() && qB.empty()) ||
				(qA.size() != qB.size() && (int)st.size() > bottom)){
			if (st.back().second == 1) qA.push_back(st.back());
			else qB.push_back(st.back());

			ds.rollback();
			st.pop_back();
		}

		reverse(qA.begin(), qA.end());
		reverse(qB.begin(), qB.end());

		for (auto query: qB){
			ds.merge(query.first.first, query.first.second);
			st.push_back(query);
		}
		for (auto query: qA){
			ds.merge(query.first.first, query.first.second);
			st.push_back(query);
		}
		
		advance_bottom();
	}

	void queue_rollback(){
		advance_bottom();
		if (bottom == (int)st.size()) flip_all();
		fix();

		ds.rollback();
		st.pop_back();
	}
};

int n, m, q;
vector<ii> edges;
DSU_Queue_Rollback dsu;

vector<int> best;

void main_program(){
	cin >> n >> m >> q;

	edges.resize(m);

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

	best.resize(m);

	dsu = DSU_Queue_Rollback(n);
	for (int i = m-1; i >= 0; i--){
		dsu.merge(edges[i].first, edges[i].second);
	}

	int crr = m;
	for (int i = m-1; i >= 0; i--){
		while (!dsu.is_bipartite()){
			if (crr < 0) break;
			dsu.queue_rollback();
			crr--;
		}
		best[i] = crr;
		dsu.merge(edges[i].first, edges[i].second);
	}

	for (int _ = 0; _ < q; _++){
		int x, y; cin >> x >> y;
		x--; y--;

		if (x <= best[y]) cout << "NO\n";
		else cout << "YES\n";
	}
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 404 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 0 ms 600 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 404 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 0 ms 600 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 2 ms 604 KB Output is correct
32 Correct 2 ms 604 KB Output is correct
33 Correct 1 ms 600 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 3 ms 604 KB Output is correct
38 Correct 1 ms 760 KB Output is correct
39 Correct 2 ms 604 KB Output is correct
40 Correct 2 ms 604 KB Output is correct
41 Correct 2 ms 604 KB Output is correct
42 Correct 2 ms 604 KB Output is correct
43 Correct 2 ms 604 KB Output is correct
44 Correct 2 ms 604 KB Output is correct
45 Correct 2 ms 604 KB Output is correct
46 Correct 2 ms 604 KB Output is correct
47 Correct 2 ms 604 KB Output is correct
48 Correct 2 ms 684 KB Output is correct
49 Correct 2 ms 700 KB Output is correct
50 Correct 2 ms 604 KB Output is correct
51 Correct 2 ms 604 KB Output is correct
52 Correct 2 ms 604 KB Output is correct
53 Correct 2 ms 604 KB Output is correct
54 Correct 2 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 269 ms 25220 KB Output is correct
4 Correct 94 ms 44576 KB Output is correct
5 Correct 176 ms 33348 KB Output is correct
6 Correct 150 ms 26868 KB Output is correct
7 Correct 152 ms 27132 KB Output is correct
8 Correct 151 ms 22168 KB Output is correct
9 Correct 175 ms 26364 KB Output is correct
10 Correct 223 ms 33528 KB Output is correct
11 Correct 190 ms 26192 KB Output is correct
12 Correct 199 ms 31992 KB Output is correct
13 Correct 110 ms 18936 KB Output is correct
14 Correct 150 ms 23940 KB Output is correct
15 Correct 209 ms 29460 KB Output is correct
16 Correct 224 ms 34496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 404 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 0 ms 600 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 269 ms 25220 KB Output is correct
30 Correct 94 ms 44576 KB Output is correct
31 Correct 176 ms 33348 KB Output is correct
32 Correct 150 ms 26868 KB Output is correct
33 Correct 152 ms 27132 KB Output is correct
34 Correct 151 ms 22168 KB Output is correct
35 Correct 175 ms 26364 KB Output is correct
36 Correct 223 ms 33528 KB Output is correct
37 Correct 190 ms 26192 KB Output is correct
38 Correct 199 ms 31992 KB Output is correct
39 Correct 110 ms 18936 KB Output is correct
40 Correct 150 ms 23940 KB Output is correct
41 Correct 209 ms 29460 KB Output is correct
42 Correct 224 ms 34496 KB Output is correct
43 Correct 277 ms 29948 KB Output is correct
44 Correct 94 ms 45036 KB Output is correct
45 Correct 239 ms 40696 KB Output is correct
46 Correct 157 ms 28504 KB Output is correct
47 Correct 157 ms 27132 KB Output is correct
48 Correct 181 ms 25892 KB Output is correct
49 Correct 243 ms 33500 KB Output is correct
50 Correct 168 ms 25976 KB Output is correct
51 Correct 201 ms 29280 KB Output is correct
52 Correct 215 ms 33204 KB Output is correct
53 Correct 112 ms 19532 KB Output is correct
54 Correct 168 ms 26104 KB Output is correct
55 Correct 209 ms 29448 KB Output is correct
56 Correct 224 ms 34816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 404 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 0 ms 600 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 2 ms 604 KB Output is correct
32 Correct 2 ms 604 KB Output is correct
33 Correct 1 ms 600 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 3 ms 604 KB Output is correct
38 Correct 1 ms 760 KB Output is correct
39 Correct 2 ms 604 KB Output is correct
40 Correct 2 ms 604 KB Output is correct
41 Correct 2 ms 604 KB Output is correct
42 Correct 2 ms 604 KB Output is correct
43 Correct 2 ms 604 KB Output is correct
44 Correct 2 ms 604 KB Output is correct
45 Correct 2 ms 604 KB Output is correct
46 Correct 2 ms 604 KB Output is correct
47 Correct 2 ms 604 KB Output is correct
48 Correct 2 ms 684 KB Output is correct
49 Correct 2 ms 700 KB Output is correct
50 Correct 2 ms 604 KB Output is correct
51 Correct 2 ms 604 KB Output is correct
52 Correct 2 ms 604 KB Output is correct
53 Correct 2 ms 604 KB Output is correct
54 Correct 2 ms 604 KB Output is correct
55 Correct 249 ms 23280 KB Output is correct
56 Correct 67 ms 44028 KB Output is correct
57 Correct 143 ms 32508 KB Output is correct
58 Correct 131 ms 24836 KB Output is correct
59 Correct 159 ms 23756 KB Output is correct
60 Correct 200 ms 28900 KB Output is correct
61 Correct 162 ms 24704 KB Output is correct
62 Correct 181 ms 29684 KB Output is correct
63 Correct 103 ms 19204 KB Output is correct
64 Correct 187 ms 25484 KB Output is correct
65 Correct 210 ms 30972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 404 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 0 ms 600 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 2 ms 604 KB Output is correct
32 Correct 2 ms 604 KB Output is correct
33 Correct 1 ms 600 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 3 ms 604 KB Output is correct
38 Correct 1 ms 760 KB Output is correct
39 Correct 2 ms 604 KB Output is correct
40 Correct 2 ms 604 KB Output is correct
41 Correct 2 ms 604 KB Output is correct
42 Correct 2 ms 604 KB Output is correct
43 Correct 2 ms 604 KB Output is correct
44 Correct 2 ms 604 KB Output is correct
45 Correct 2 ms 604 KB Output is correct
46 Correct 2 ms 604 KB Output is correct
47 Correct 2 ms 604 KB Output is correct
48 Correct 2 ms 684 KB Output is correct
49 Correct 2 ms 700 KB Output is correct
50 Correct 2 ms 604 KB Output is correct
51 Correct 2 ms 604 KB Output is correct
52 Correct 2 ms 604 KB Output is correct
53 Correct 2 ms 604 KB Output is correct
54 Correct 2 ms 604 KB Output is correct
55 Correct 269 ms 25220 KB Output is correct
56 Correct 94 ms 44576 KB Output is correct
57 Correct 176 ms 33348 KB Output is correct
58 Correct 150 ms 26868 KB Output is correct
59 Correct 152 ms 27132 KB Output is correct
60 Correct 151 ms 22168 KB Output is correct
61 Correct 175 ms 26364 KB Output is correct
62 Correct 223 ms 33528 KB Output is correct
63 Correct 190 ms 26192 KB Output is correct
64 Correct 199 ms 31992 KB Output is correct
65 Correct 110 ms 18936 KB Output is correct
66 Correct 150 ms 23940 KB Output is correct
67 Correct 209 ms 29460 KB Output is correct
68 Correct 224 ms 34496 KB Output is correct
69 Correct 277 ms 29948 KB Output is correct
70 Correct 94 ms 45036 KB Output is correct
71 Correct 239 ms 40696 KB Output is correct
72 Correct 157 ms 28504 KB Output is correct
73 Correct 157 ms 27132 KB Output is correct
74 Correct 181 ms 25892 KB Output is correct
75 Correct 243 ms 33500 KB Output is correct
76 Correct 168 ms 25976 KB Output is correct
77 Correct 201 ms 29280 KB Output is correct
78 Correct 215 ms 33204 KB Output is correct
79 Correct 112 ms 19532 KB Output is correct
80 Correct 168 ms 26104 KB Output is correct
81 Correct 209 ms 29448 KB Output is correct
82 Correct 224 ms 34816 KB Output is correct
83 Correct 249 ms 23280 KB Output is correct
84 Correct 67 ms 44028 KB Output is correct
85 Correct 143 ms 32508 KB Output is correct
86 Correct 131 ms 24836 KB Output is correct
87 Correct 159 ms 23756 KB Output is correct
88 Correct 200 ms 28900 KB Output is correct
89 Correct 162 ms 24704 KB Output is correct
90 Correct 181 ms 29684 KB Output is correct
91 Correct 103 ms 19204 KB Output is correct
92 Correct 187 ms 25484 KB Output is correct
93 Correct 210 ms 30972 KB Output is correct
94 Correct 273 ms 29428 KB Output is correct
95 Correct 343 ms 38648 KB Output is correct
96 Correct 173 ms 35056 KB Output is correct
97 Correct 153 ms 27644 KB Output is correct
98 Correct 173 ms 27776 KB Output is correct
99 Correct 162 ms 24316 KB Output is correct
100 Correct 204 ms 35264 KB Output is correct
101 Correct 169 ms 25600 KB Output is correct
102 Correct 238 ms 31084 KB Output is correct
103 Correct 238 ms 34676 KB Output is correct
104 Correct 134 ms 21968 KB Output is correct
105 Correct 201 ms 29944 KB Output is correct
106 Correct 238 ms 33568 KB Output is correct
107 Correct 166 ms 39380 KB Output is correct
108 Correct 268 ms 29728 KB Output is correct
109 Correct 276 ms 30160 KB Output is correct
110 Correct 268 ms 30404 KB Output is correct
111 Correct 278 ms 29936 KB Output is correct
112 Correct 270 ms 28668 KB Output is correct
113 Correct 272 ms 28524 KB Output is correct
114 Correct 282 ms 29248 KB Output is correct
115 Correct 273 ms 28668 KB Output is correct
116 Correct 285 ms 28536 KB Output is correct