1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| #include <cstdio> #include <map> #include <vector> #define fi first #define se second #define mp make_pair using namespace std; template <class Type> void read(Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; if (flag) x = ~x + 1; } template <class Type, class ...rest> void read(Type &x, rest &...y) { read(x), read(y...); } template <class Type> void write(Type x) { if (x < 0) putchar('-'), x = ~x + 1; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int N = 1e5 + 10; vector <PIB> stk; map <PII, int> pre; int n, m, q, p[N << 1], d[N << 1]; struct Edge { int u, v; } e[N]; struct Node { int l, r; vector <int> v; } tr[N << 2]; void build (int l = 1, int r = q, int x = 1) { tr[x].l = l, tr[x].r = r; if (l == r) return; int mid = l + r >> 1; build(l, mid, x << 1), build(mid + 1, r, x << 1 | 1); } void modify (int l, int r, int t, int x = 1) { if (tr[x].l >= l && tr[x].r <= r) return tr[x].v.push_back(t); int mid = tr[x].l + tr[x].r >> 1; if (l <= mid) modify(l, r, t, x << 1); if (r > mid) modify(l, r, t, x << 1 | 1); } int fa (int x) { while (x ^ p[x]) x = p[x]; return x; } void merge (int x, int y) { if (x == y) return; if (d[x] > d[y]) swap(x, y); stk.push_back(mp(x, d[x] == d[y])); p[x] = y, d[y] += d[x] == d[y]; } void dfs (int x = 1) { auto t = stk.size(); bool flag = true; for (int i : tr[x].v) { if (!(flag &= fa(e[i].u) != fa(e[i].v))) break; merge(fa(e[i].u + n), fa(e[i].v)); merge(fa(e[i].v + n), fa(e[i].u)); } if (flag) { if (tr[x].l == tr[x].r) puts("YES"); else dfs(x << 1), dfs(x << 1 | 1); } else for (int i = tr[x].l; i <= tr[x].r; ++i) puts("NO"); for (; stk.size() > t; stk.pop_back()) { int k = stk.back().fi; d[p[k]] -= stk.back().se, p[k] = k; } } int main () { read(n, q); build(); for (int i = 1; i <= q; ++i) { read(e[i].u, e[i].v); if (e[i].u > e[i].v) swap(e[i].u, e[i].v); PII t = mp(e[i].u, e[i].v); if (pre.count(mp(e[i].u, e[i].v))) { modify(pre[t], i - 1, i); pre.erase(t); } else pre[t] = i; } for (auto i : pre) modify(i.se, q, i.se); for (int i = 1; i <= n << 1; ++i) p[i] = i; dfs(); return 0; }
|