#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); --i)
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
using namespace std;
using ll = long long;
template<class T, class U> bool chmin(T& a, const U& b) { return a > b ? a = b, true : false; }
template<class T, class U> bool chmax(T& a, const U& b) { return a < b ? a = b, true : false; }
class UndoableUF {
vector<int> par;
vector<pair<int, int>> history;
public:
UndoableUF(int n) : par(n, -1) {}
int find(int x) { return par[x] < 0 ? x : find(par[x]); }
bool merge(int x, int y) {
x = find(x), y = find(y);
history.emplace_back(x, par[x]);
history.emplace_back(y, par[y]);
if (x == y) return false;
if (par[x] > par[y]) swap(x, y);
par[x] += par[y];
par[y] = x;
return true;
}
bool same(int x, int y) { return find(x) == find(y); }
void undo() {
auto [x, px] = history.back(); history.pop_back();
auto [y, py] = history.back(); history.pop_back();
par[x] = px;
par[y] = py;
}
void snapshot() { history.clear(); }
void rollback() {
while (!history.empty()) undo();
}
};
int main() {
int N, M, Q; cin >> N >> M >> Q;
vector<pair<int, int>> A(M);
rep (i, M) {
cin >> A[i].first >> A[i].second;
--A[i].first, --A[i].second;
}
rep (_, Q) {
UndoableUF uf(2 * N);
int l, r; cin >> l >> r;
--l;
rep (i, M) {
if (l <= i && i < r) continue;
uf.merge(A[i].first, A[i].second + N);
uf.merge(A[i].first + N, A[i].second);
}
bool f = false;
rep (i, N) {
if (uf.same(i, i + N)) f = true;
}
if (f) puts("YES");
else puts("NO");
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
436 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
436 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
436 KB |
Output is correct |
29 |
Correct |
34 ms |
344 KB |
Output is correct |
30 |
Correct |
69 ms |
448 KB |
Output is correct |
31 |
Correct |
67 ms |
604 KB |
Output is correct |
32 |
Correct |
55 ms |
604 KB |
Output is correct |
33 |
Correct |
43 ms |
604 KB |
Output is correct |
34 |
Correct |
84 ms |
604 KB |
Output is correct |
35 |
Correct |
80 ms |
600 KB |
Output is correct |
36 |
Correct |
39 ms |
604 KB |
Output is correct |
37 |
Correct |
63 ms |
604 KB |
Output is correct |
38 |
Correct |
63 ms |
604 KB |
Output is correct |
39 |
Correct |
59 ms |
600 KB |
Output is correct |
40 |
Correct |
49 ms |
600 KB |
Output is correct |
41 |
Correct |
54 ms |
600 KB |
Output is correct |
42 |
Correct |
46 ms |
604 KB |
Output is correct |
43 |
Correct |
61 ms |
604 KB |
Output is correct |
44 |
Correct |
72 ms |
604 KB |
Output is correct |
45 |
Correct |
89 ms |
604 KB |
Output is correct |
46 |
Correct |
72 ms |
604 KB |
Output is correct |
47 |
Correct |
45 ms |
600 KB |
Output is correct |
48 |
Correct |
55 ms |
604 KB |
Output is correct |
49 |
Correct |
59 ms |
604 KB |
Output is correct |
50 |
Correct |
68 ms |
604 KB |
Output is correct |
51 |
Correct |
48 ms |
604 KB |
Output is correct |
52 |
Correct |
65 ms |
600 KB |
Output is correct |
53 |
Correct |
67 ms |
600 KB |
Output is correct |
54 |
Correct |
79 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Execution timed out |
2020 ms |
20348 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
436 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
436 KB |
Output is correct |
29 |
Execution timed out |
2020 ms |
20348 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
436 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
436 KB |
Output is correct |
29 |
Correct |
34 ms |
344 KB |
Output is correct |
30 |
Correct |
69 ms |
448 KB |
Output is correct |
31 |
Correct |
67 ms |
604 KB |
Output is correct |
32 |
Correct |
55 ms |
604 KB |
Output is correct |
33 |
Correct |
43 ms |
604 KB |
Output is correct |
34 |
Correct |
84 ms |
604 KB |
Output is correct |
35 |
Correct |
80 ms |
600 KB |
Output is correct |
36 |
Correct |
39 ms |
604 KB |
Output is correct |
37 |
Correct |
63 ms |
604 KB |
Output is correct |
38 |
Correct |
63 ms |
604 KB |
Output is correct |
39 |
Correct |
59 ms |
600 KB |
Output is correct |
40 |
Correct |
49 ms |
600 KB |
Output is correct |
41 |
Correct |
54 ms |
600 KB |
Output is correct |
42 |
Correct |
46 ms |
604 KB |
Output is correct |
43 |
Correct |
61 ms |
604 KB |
Output is correct |
44 |
Correct |
72 ms |
604 KB |
Output is correct |
45 |
Correct |
89 ms |
604 KB |
Output is correct |
46 |
Correct |
72 ms |
604 KB |
Output is correct |
47 |
Correct |
45 ms |
600 KB |
Output is correct |
48 |
Correct |
55 ms |
604 KB |
Output is correct |
49 |
Correct |
59 ms |
604 KB |
Output is correct |
50 |
Correct |
68 ms |
604 KB |
Output is correct |
51 |
Correct |
48 ms |
604 KB |
Output is correct |
52 |
Correct |
65 ms |
600 KB |
Output is correct |
53 |
Correct |
67 ms |
600 KB |
Output is correct |
54 |
Correct |
79 ms |
604 KB |
Output is correct |
55 |
Execution timed out |
2090 ms |
20848 KB |
Time limit exceeded |
56 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
436 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
436 KB |
Output is correct |
29 |
Correct |
34 ms |
344 KB |
Output is correct |
30 |
Correct |
69 ms |
448 KB |
Output is correct |
31 |
Correct |
67 ms |
604 KB |
Output is correct |
32 |
Correct |
55 ms |
604 KB |
Output is correct |
33 |
Correct |
43 ms |
604 KB |
Output is correct |
34 |
Correct |
84 ms |
604 KB |
Output is correct |
35 |
Correct |
80 ms |
600 KB |
Output is correct |
36 |
Correct |
39 ms |
604 KB |
Output is correct |
37 |
Correct |
63 ms |
604 KB |
Output is correct |
38 |
Correct |
63 ms |
604 KB |
Output is correct |
39 |
Correct |
59 ms |
600 KB |
Output is correct |
40 |
Correct |
49 ms |
600 KB |
Output is correct |
41 |
Correct |
54 ms |
600 KB |
Output is correct |
42 |
Correct |
46 ms |
604 KB |
Output is correct |
43 |
Correct |
61 ms |
604 KB |
Output is correct |
44 |
Correct |
72 ms |
604 KB |
Output is correct |
45 |
Correct |
89 ms |
604 KB |
Output is correct |
46 |
Correct |
72 ms |
604 KB |
Output is correct |
47 |
Correct |
45 ms |
600 KB |
Output is correct |
48 |
Correct |
55 ms |
604 KB |
Output is correct |
49 |
Correct |
59 ms |
604 KB |
Output is correct |
50 |
Correct |
68 ms |
604 KB |
Output is correct |
51 |
Correct |
48 ms |
604 KB |
Output is correct |
52 |
Correct |
65 ms |
600 KB |
Output is correct |
53 |
Correct |
67 ms |
600 KB |
Output is correct |
54 |
Correct |
79 ms |
604 KB |
Output is correct |
55 |
Execution timed out |
2020 ms |
20348 KB |
Time limit exceeded |
56 |
Halted |
0 ms |
0 KB |
- |