#include <cstdio>
#include <cstring>
#include <cassert>
#include <string>
#include <deque>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
using i64 = long long;
using f80 = long double;
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false)
#define N 200005
#define ALL(x) x.begin(), x.end()
int n, m, q, e[N][2], r[N+1];
struct dsu
{
vector<pair<int, int>> p;
vector<int> rank;
int cb, dumb;
vector<tuple<int, int, pair<int, int>, int, int, int>> save;
dsu(int n) : rank(n, 1), cb(1) { for (int i = 0; i < n; ++i) p.emplace_back(i, 0); }
pair<int, int> find(int x)
{
if (p[x].first != x)
{
auto [A, B] = find(p[x].first);
p[x].second ^= B;
return {A, p[x].second};
}
else return p[x];
}
int unite(int x0, int y0)
{
auto [x, A] { find(x0) };
auto [y, B] { find(y0) };
if (rank[x] > rank[y]) swap(x, y);
save.emplace_back(x, y, p[x], cb, rank[x], rank[y]);
if (x == y)
{
if (A == B) cb = 0;
return 0;
}
if (rank[x] == rank[y]) ++rank[y];
p[x] = {y, A^B^1};
return 1;
}
void rollback()
{
auto [x, y, px, cb0, rx, ry] = save.back(); save.pop_back();
if (x != y) p[x] = px;
rank[x] = rx, rank[y] = ry;
cb = cb0;
}
};
struct qdsu
{
dsu ds;
int bottom;
struct upd { char type; int x, y; };
vector<upd> S;
qdsu(int n) : ds(n), bottom(0) {}
void bottomify()
{
while (bottom < (int)S.size() && S[bottom].type == 'B') ++bottom;
}
void fix()
{
if (S.empty() || S.back().type == 'A') return;
bottomify();
deque<upd> s[2];
for (int j = (int)S.size() - 1; (s[1].size() != s[0].size() || s[0].empty()) && j >= bottom; --j)
s[S[j].type == 'A'].push_front(S[j]), ds.rollback(), S.pop_back();
for (auto j : {0, 1}) for (auto x : s[j]) S.push_back(x), ds.unite(x.x, x.y);
bottomify();
}
void new_phase()
{
for (auto i = S.size(); i--;) ds.rollback();
for (auto i = S.size(); i--;) S[i].type = 'A', ds.unite(S[i].x, S[i].y);
reverse(S.begin(), S.end());
bottom = 0;
}
void pop()
{
bottomify();
if (bottom == (int)S.size()) new_phase();
fix();
S.pop_back();
ds.rollback();
}
int unite(int x, int y)
{
S.push_back(upd{'B', x, y});
return ds.unite(x, y);
}
};
int main()
{
ShinLena;
cin >> n >> m >> q;
qdsu qd(n);
for (int i = 0; i < m; ++i) cin >> e[i][0] >> e[i][1], qd.unite(--e[i][0], --e[i][1]) ;
for (int i = 0, j = 0; i < m; ++i)
{
for (; !qd.ds.cb && j < m; ++j) qd.pop();
if (qd.ds.cb) r[i] = j - 1;
else r[i] = 1e9;
qd.unite(e[i][0], e[i][1]);
}
for (int i = 0, x, y; i < q; ++i) cin >> x >> y, cout << (r[x-1] <= y-1 ? "NO\n" : "YES\n");
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
0 ms |
2512 KB |
Output is correct |
6 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
0 ms |
2512 KB |
Output is correct |
6 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
71 ms |
16204 KB |
Output is correct |
4 |
Correct |
75 ms |
29120 KB |
Output is correct |
5 |
Incorrect |
83 ms |
18896 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
0 ms |
2512 KB |
Output is correct |
6 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
0 ms |
2512 KB |
Output is correct |
6 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
0 ms |
2512 KB |
Output is correct |
6 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |