#include <bits/stdc++.h>
using namespace std;
struct DSU
{
vector<int> rt, sz;
vector<int> stk;
DSU(int n)
{
rt.resize(n);
iota(rt.begin(), rt.end(), 0);
sz.resize(n, 1);
}
int find(int u)
{
if (u == rt[u])
{
return u;
}
return find(rt[u]);
}
bool same(int u, int v)
{
return find(u) == find(v);
}
void unite(int u, int v)
{
u = find(u), v = find(v);
if (u == v)
{
stk.push_back(-1);
return;
}
if (sz[u] > sz[v])
{
swap(u, v);
}
rt[u] = v;
sz[v] += sz[u];
stk.push_back(u);
}
void undo()
{
int u = stk.back();
stk.pop_back();
if (u == -1)
{
return;
}
int v = rt[u];
sz[v] -= sz[u];
rt[u] = u;
}
};
const int B = 450;
int main()
{
cin.tie(0)->sync_with_stdio(0);
int n, m, q;
cin >> n >> m >> q;
vector<pair<int, int>> ed(m);
for (int i = 0; i < m; i++)
{
int u, v;
cin >> u >> v;
u--, v--;
ed[i] = make_pair(u, v);
}
vector<int> rr(m, -1);
DSU dsu(2 * n);
bool f = 0;
for (int r = m - 1; r >= 0; r--)
{
if (f)
{
continue;
}
bool f2 = 0;
int l = 0;
while (l <= r && l <= 210)
{
auto [u, v] = ed[l];
dsu.unite(u, v + n);
dsu.unite(u + n, v);
if (dsu.same(u, v))
{
for (int j = 0; j <= l; j++)
{
dsu.undo();
dsu.undo();
}
f2 = 1;
break;
}
l++;
}
rr[r] = l;
if (!f2)
{
for (int j = 0; j < l; j++)
{
dsu.undo();
dsu.undo();
}
}
auto [u, v] = ed[r];
dsu.unite(u, v + n);
dsu.unite(u + n, v);
if (dsu.same(u, v))
{
f = 1;
}
}
// for (int b = 0; b * B < m; b++)
// {
// int l1 = b * B, l2 = min(m, (b + 1) * B);
// int r = -1;
// DSU dsu(2 * n);
// for (int i = 0; i < l1; i++)
// {
// auto [u, v] = ed[i];
// dsu.unite(u, v + n);
// dsu.unite(u + n, v);
// if (dsu.same(u, v))
// {
// r = i;
// break;
// }
// }
// if (r != -1)
// {
// break;
// }
// for (int i = l1; i < l2; i++)
// {
// auto [u, v] = ed[i];
// dsu.unite(u, v + n);
// dsu.unite(u + n, v);
// if (dsu.same(u, v))
// {
// r = i;
// break;
// }
// }
// if (r == -1)
// {
// r = m - 1;
// while (r >= l2)
// {
// auto [u, v] = ed[r];
// dsu.unite(u, v + n);
// dsu.unite(u + n, v);
// if (dsu.same(u, v))
// {
// dsu.undo();
// dsu.undo();
// break;
// }
// r--;
// }
// for (int i = m - 1; i > r; i--)
// {
// dsu.undo();
// dsu.undo();
// }
// for (int i = l1; i < l2; i++)
// {
// dsu.undo();
// dsu.undo();
// }
// }
// else
// {
// for (int i = l1; i <= r; i++)
// {
// dsu.undo();
// dsu.undo();
// }
// r = m - 1;
// }
// for (int i = m - 1; i > r; i--)
// {
// auto [u, v] = ed[i];
// dsu.unite(u, v + n);
// dsu.unite(u + n, v);
// }
// for (int i = r; i >= l1 && !dsu.same(ed[i].first, ed[i].second); i--)
// {
// int l = l1;
// while (l < l2 && l <= i)
// {
// auto [u, v] = ed[l];
// dsu.unite(u, v + n);
// dsu.unite(u + n, v);
// if (dsu.same(u, v))
// {
// for (int j = l1; j <= l; j++)
// {
// dsu.undo();
// dsu.undo();
// }
// break;
// }
// l++;
// }
// rr[i] = l;
// auto [u, v] = ed[i];
// dsu.unite(u, v + n);
// dsu.unite(u + n, v);
// }
// }
while (q--)
{
int l, r;
cin >> l >> r;
l--, r--;
cout << (rr[r] >= l ? "NO" : "YES") << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
428 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 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 |
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 |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
428 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Incorrect |
4 ms |
604 KB |
Output isn't correct |
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 |
328 ms |
6256 KB |
Output is correct |
4 |
Correct |
578 ms |
11920 KB |
Output is correct |
5 |
Correct |
110 ms |
10712 KB |
Output is correct |
6 |
Correct |
85 ms |
9848 KB |
Output is correct |
7 |
Correct |
98 ms |
9816 KB |
Output is correct |
8 |
Correct |
198 ms |
8912 KB |
Output is correct |
9 |
Correct |
240 ms |
9356 KB |
Output is correct |
10 |
Correct |
384 ms |
10960 KB |
Output is correct |
11 |
Correct |
338 ms |
9824 KB |
Output is correct |
12 |
Correct |
335 ms |
11076 KB |
Output is correct |
13 |
Correct |
356 ms |
8400 KB |
Output is correct |
14 |
Correct |
232 ms |
9168 KB |
Output is correct |
15 |
Correct |
339 ms |
10392 KB |
Output is correct |
16 |
Correct |
426 ms |
11472 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 |
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 |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
428 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
328 ms |
6256 KB |
Output is correct |
30 |
Correct |
578 ms |
11920 KB |
Output is correct |
31 |
Correct |
110 ms |
10712 KB |
Output is correct |
32 |
Correct |
85 ms |
9848 KB |
Output is correct |
33 |
Correct |
98 ms |
9816 KB |
Output is correct |
34 |
Correct |
198 ms |
8912 KB |
Output is correct |
35 |
Correct |
240 ms |
9356 KB |
Output is correct |
36 |
Correct |
384 ms |
10960 KB |
Output is correct |
37 |
Correct |
338 ms |
9824 KB |
Output is correct |
38 |
Correct |
335 ms |
11076 KB |
Output is correct |
39 |
Correct |
356 ms |
8400 KB |
Output is correct |
40 |
Correct |
232 ms |
9168 KB |
Output is correct |
41 |
Correct |
339 ms |
10392 KB |
Output is correct |
42 |
Correct |
426 ms |
11472 KB |
Output is correct |
43 |
Correct |
318 ms |
10152 KB |
Output is correct |
44 |
Correct |
580 ms |
12072 KB |
Output is correct |
45 |
Correct |
525 ms |
12372 KB |
Output is correct |
46 |
Correct |
73 ms |
10188 KB |
Output is correct |
47 |
Correct |
98 ms |
10320 KB |
Output is correct |
48 |
Correct |
293 ms |
9912 KB |
Output is correct |
49 |
Correct |
416 ms |
11788 KB |
Output is correct |
50 |
Correct |
313 ms |
9788 KB |
Output is correct |
51 |
Correct |
332 ms |
11116 KB |
Output is correct |
52 |
Correct |
350 ms |
11796 KB |
Output is correct |
53 |
Correct |
304 ms |
8736 KB |
Output is correct |
54 |
Correct |
230 ms |
9928 KB |
Output is correct |
55 |
Correct |
339 ms |
11152 KB |
Output is correct |
56 |
Correct |
392 ms |
11724 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 |
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 |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
428 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Incorrect |
4 ms |
604 KB |
Output isn't correct |
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 |
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 |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
428 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Incorrect |
4 ms |
604 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |