#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "algo/debug.h"
#endif
#define f first
#define s second
template<class T> using V = vector<T>;
using vi = V<int>;
using vb = V<bool>;
using vs = V<string>;
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define pb push_back
#define lb lower_bound
#define ub upper_bound
template<class T> int lwb(V<T>& a, const T& b) { return lb(all(a),b)-begin(a); }
template<class T> int upb(V<T>& a, const T& b) { return ub(all(a),b)-begin(a); }
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a=b, true : false; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a=b, true : false; }
#define ASSERT(exp) do { if (!(exp)) while (true); } while(0)
const int K = 1.8e4, max_n = 2e5, max_q = 2000;
struct query {
int l, r, ind;
bool operator <(const query& o) const {
if (l/K == o.l/K) return r > o.r;
return l/K < o.l/K;
}
};
V<query> queries[(max_n-1)/K + 1];
int n, m, q, ans[max_q];
array<int, 2> edges[max_n];
array<int, 2> par[max_n];
int rnk[max_n];
void make_set(int u) {
par[u] = {u, 0};
rnk[u] = 0;
}
array<int, 2> find(int u) {
if (u == par[u][0]) return par[u];
auto res = find(par[u][0]);
res[1] ^= par[u][1];
return res;
}
bool is_bipartite;
V<pair<array<int, 2>&, array<int, 2>>> undo_par;
V<pair<bool&, bool>> undo_bipartite;
V<pair<int&, int>> undo_rnk;
void unite(int u, int v) {
auto U = find(u), V = find(v);
if (rnk[U[0]] < rnk[V[0]]) swap(U, V);
undo_bipartite.pb({is_bipartite, is_bipartite});
undo_par.pb({par[V[0]], par[V[0]]});
undo_rnk.pb({rnk[U[0]], rnk[U[0]]});
if (U[0] == V[0]) {
if (U[1] == V[1]) {
is_bipartite = false;
}
} else {
int t = U[1]^V[1]^1;
par[V[0]] = {U[0], t};
if (rnk[U[0]] == rnk[V[0]]) rnk[U[0]]++;
}
}
int snapshot() { return undo_par.size(); }
void rollback(int until) {
while (undo_par.size() > until) {
undo_par.back().f = undo_par.back().s;
undo_bipartite.back().f = undo_bipartite.back().s;
undo_rnk.back().f = undo_rnk.back().s;
undo_par.pop_back();
undo_bipartite.pop_back();
undo_rnk.pop_back();
}
}
void reset() {
is_bipartite = true;
for (int i = 0; i < n; i++) make_set(i);
undo_par.clear();
undo_bipartite.clear();
undo_rnk.clear();
}
void solve() {
cin >> n >> m >> q;
for (int i = 0; i < m; i++) {
cin >> edges[i][0] >> edges[i][1];
edges[i][0]--; edges[i][1]--;
}
for (int i = 0; i < q; i++) {
int l, r; cin >> l >> r; l--;
queries[l/K].pb({l, r, i});
}
for (int i = 0; i <= (m-1)/K; i++) sort(all(queries[i]));
reset();
for (int b = 0; b <= (m-1)/K; b++) {
int l = b*K, r = m;
for (int i = max((b-1)*K, 0); i < l; i++) unite(edges[i][0], edges[i][1]);
for (auto [L, R, I] : queries[b]) {
while (r > R) {
r--;
unite(edges[r][0], edges[r][1]);
}
int photo = snapshot();
while (l < L) {
unite(edges[l][0], edges[l][1]);
l++;
}
ans[I] = !is_bipartite;
rollback(photo);
l = b*K;
}
}
for (int i = 0; i < q; i++) cout << (ans[i] ? "YES" : "NO") << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
solve();
return 0;
}
Compilation message
Joker.cpp: In function 'void rollback(int)':
Joker.cpp:73:28: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<std::array<int, 2>&, std::array<int, 2> >, std::allocator<std::pair<std::array<int, 2>&, std::array<int, 2> > > >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
73 | while (undo_par.size() > until) {
| ~~~~~~~~~~~~~~~~^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2400 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
2 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2400 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
2 ms |
2396 KB |
Output is correct |
29 |
Correct |
32 ms |
2648 KB |
Output is correct |
30 |
Correct |
54 ms |
2564 KB |
Output is correct |
31 |
Correct |
62 ms |
2652 KB |
Output is correct |
32 |
Correct |
59 ms |
600 KB |
Output is correct |
33 |
Correct |
63 ms |
676 KB |
Output is correct |
34 |
Correct |
61 ms |
2804 KB |
Output is correct |
35 |
Correct |
60 ms |
2652 KB |
Output is correct |
36 |
Correct |
61 ms |
2652 KB |
Output is correct |
37 |
Correct |
63 ms |
2900 KB |
Output is correct |
38 |
Correct |
59 ms |
2728 KB |
Output is correct |
39 |
Correct |
58 ms |
2648 KB |
Output is correct |
40 |
Correct |
60 ms |
2576 KB |
Output is correct |
41 |
Correct |
61 ms |
2652 KB |
Output is correct |
42 |
Correct |
61 ms |
2732 KB |
Output is correct |
43 |
Correct |
60 ms |
2652 KB |
Output is correct |
44 |
Correct |
61 ms |
600 KB |
Output is correct |
45 |
Correct |
62 ms |
2652 KB |
Output is correct |
46 |
Correct |
62 ms |
2652 KB |
Output is correct |
47 |
Correct |
60 ms |
2900 KB |
Output is correct |
48 |
Correct |
61 ms |
2652 KB |
Output is correct |
49 |
Correct |
60 ms |
2568 KB |
Output is correct |
50 |
Correct |
63 ms |
2652 KB |
Output is correct |
51 |
Correct |
62 ms |
2648 KB |
Output is correct |
52 |
Correct |
63 ms |
2728 KB |
Output is correct |
53 |
Correct |
59 ms |
604 KB |
Output is correct |
54 |
Correct |
61 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Runtime error |
66 ms |
17256 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2400 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
2 ms |
2396 KB |
Output is correct |
29 |
Runtime error |
66 ms |
17256 KB |
Execution killed with signal 11 |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2400 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
2 ms |
2396 KB |
Output is correct |
29 |
Correct |
32 ms |
2648 KB |
Output is correct |
30 |
Correct |
54 ms |
2564 KB |
Output is correct |
31 |
Correct |
62 ms |
2652 KB |
Output is correct |
32 |
Correct |
59 ms |
600 KB |
Output is correct |
33 |
Correct |
63 ms |
676 KB |
Output is correct |
34 |
Correct |
61 ms |
2804 KB |
Output is correct |
35 |
Correct |
60 ms |
2652 KB |
Output is correct |
36 |
Correct |
61 ms |
2652 KB |
Output is correct |
37 |
Correct |
63 ms |
2900 KB |
Output is correct |
38 |
Correct |
59 ms |
2728 KB |
Output is correct |
39 |
Correct |
58 ms |
2648 KB |
Output is correct |
40 |
Correct |
60 ms |
2576 KB |
Output is correct |
41 |
Correct |
61 ms |
2652 KB |
Output is correct |
42 |
Correct |
61 ms |
2732 KB |
Output is correct |
43 |
Correct |
60 ms |
2652 KB |
Output is correct |
44 |
Correct |
61 ms |
600 KB |
Output is correct |
45 |
Correct |
62 ms |
2652 KB |
Output is correct |
46 |
Correct |
62 ms |
2652 KB |
Output is correct |
47 |
Correct |
60 ms |
2900 KB |
Output is correct |
48 |
Correct |
61 ms |
2652 KB |
Output is correct |
49 |
Correct |
60 ms |
2568 KB |
Output is correct |
50 |
Correct |
63 ms |
2652 KB |
Output is correct |
51 |
Correct |
62 ms |
2648 KB |
Output is correct |
52 |
Correct |
63 ms |
2728 KB |
Output is correct |
53 |
Correct |
59 ms |
604 KB |
Output is correct |
54 |
Correct |
61 ms |
604 KB |
Output is correct |
55 |
Incorrect |
640 ms |
72492 KB |
Output isn't correct |
56 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2400 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
2 ms |
2396 KB |
Output is correct |
29 |
Correct |
32 ms |
2648 KB |
Output is correct |
30 |
Correct |
54 ms |
2564 KB |
Output is correct |
31 |
Correct |
62 ms |
2652 KB |
Output is correct |
32 |
Correct |
59 ms |
600 KB |
Output is correct |
33 |
Correct |
63 ms |
676 KB |
Output is correct |
34 |
Correct |
61 ms |
2804 KB |
Output is correct |
35 |
Correct |
60 ms |
2652 KB |
Output is correct |
36 |
Correct |
61 ms |
2652 KB |
Output is correct |
37 |
Correct |
63 ms |
2900 KB |
Output is correct |
38 |
Correct |
59 ms |
2728 KB |
Output is correct |
39 |
Correct |
58 ms |
2648 KB |
Output is correct |
40 |
Correct |
60 ms |
2576 KB |
Output is correct |
41 |
Correct |
61 ms |
2652 KB |
Output is correct |
42 |
Correct |
61 ms |
2732 KB |
Output is correct |
43 |
Correct |
60 ms |
2652 KB |
Output is correct |
44 |
Correct |
61 ms |
600 KB |
Output is correct |
45 |
Correct |
62 ms |
2652 KB |
Output is correct |
46 |
Correct |
62 ms |
2652 KB |
Output is correct |
47 |
Correct |
60 ms |
2900 KB |
Output is correct |
48 |
Correct |
61 ms |
2652 KB |
Output is correct |
49 |
Correct |
60 ms |
2568 KB |
Output is correct |
50 |
Correct |
63 ms |
2652 KB |
Output is correct |
51 |
Correct |
62 ms |
2648 KB |
Output is correct |
52 |
Correct |
63 ms |
2728 KB |
Output is correct |
53 |
Correct |
59 ms |
604 KB |
Output is correct |
54 |
Correct |
61 ms |
604 KB |
Output is correct |
55 |
Runtime error |
66 ms |
17256 KB |
Execution killed with signal 11 |
56 |
Halted |
0 ms |
0 KB |
- |