#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, ne, h[N], e[N][2], r[N+1];
struct dsu
{
vector<int> p, pp, bi;
vector<tuple<int, int, int, int, int, int, int>> save;
int cb;
dsu(int n) : p(n, -1), pp(n, 0), bi(n, 1), cb(n) {}
int find(int x)
{
if (p[x] < 0) return p[x];
auto X { find(p[x]) };
if (p[x] >= 0) pp[x] = pp[p[x]] ^ 1;
return X;
}
int unite(int x0, int y0)
{
int x { find(x0) } , y { find(y0) };
if (x == y)
{
save.emplace_back(x, y, -1, pp[x], pp[x], bi[x], bi[x]);
if (pp[x0] == pp[y0]) cb += (0 - bi[x]), bi[x] = 0;
return 0;
}
if (-p[x] > -p[y]) swap(x, y);
save.emplace_back(x, y, p[x], pp[x], pp[y], bi[x], bi[y]);
pp[x] = pp[x0] ^ pp[y0] ^ 1;
p[y] += p[x];
p[x] = y;
cb -= bi[y];
bi[y] &= bi[x];
cb += bi[y];
return 1;
}
void rollback()
{
auto [x, y, px, ppx, ppy, bix, biy] = save.back(); save.pop_back();
if (px != -1) p[y] -= (p[x] = px);
pp[x] = ppx; pp[y] = ppy;
cb -= bi[y] + bi[x];
bi[y] = biy; bi[x] = bix;
cb += bi[y] + bi[x];
}
};
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)
{
if (S[j].type == 'A') s[1].push_front(S[j]);
else s[0].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 == 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;
for (int i = 0; i < m; ++i) cin >> e[i][0] >> e[i][1];
qdsu qd(n);
for (int i { m }; i--;) qd.unite(e[i][0], e[i][1]);
for (int j = m - 1, i = m - 1; i >= -1; --i)
{
while (j > i && j >= 0)
{
if (n != qd.ds.cb)
{
r[i+1] = j;
break;
}
else
{
qd.unite(e[j][0], e[j][1]);
--j;
}
}
qd.pop();
}
for (int i = 0, x, y; i < q; ++i) cin >> x >> y, cout << (r[x] >= y ? "YES\n" : "NO\n");
return 0;
}
Compilation message
Joker.cpp: In member function 'void qdsu::pop()':
Joker.cpp:109:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<qdsu::upd>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | if (bottom == S.size()) new_phase();
| ~~~~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2408 KB |
Output is correct |
3 |
Runtime error |
2 ms |
4700 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2408 KB |
Output is correct |
3 |
Runtime error |
2 ms |
4700 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2408 KB |
Output is correct |
3 |
Incorrect |
170 ms |
16164 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2408 KB |
Output is correct |
3 |
Runtime error |
2 ms |
4700 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2408 KB |
Output is correct |
3 |
Runtime error |
2 ms |
4700 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2408 KB |
Output is correct |
3 |
Runtime error |
2 ms |
4700 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |