|
| 1 | +#include <bits/stdc++.h> |
| 2 | +const int N = 200000 + 10; |
| 3 | +int n, m, ans[N]; |
| 4 | +struct E |
| 5 | +{ |
| 6 | + int u, v; |
| 7 | + E(int a, int b) : u(a), v(b) {} |
| 8 | +}; |
| 9 | +std::map<int, int> g[N]; |
| 10 | + |
| 11 | +int L[N << 2], R[N << 2]; |
| 12 | +std::vector<E> tag[N << 2]; |
| 13 | +#define lc (o << 1) |
| 14 | +#define rc ((o << 1) | 1) |
| 15 | +void build(int o, int l, int r) |
| 16 | +{ |
| 17 | + L[o] = l; |
| 18 | + R[o] = r; |
| 19 | + if (l == r) |
| 20 | + return; |
| 21 | + int mid = (l + r) >> 1; |
| 22 | + build(lc, l, mid); |
| 23 | + build(rc, mid + 1, r); |
| 24 | +} |
| 25 | +// 把一条边拆到logm个区间上去. |
| 26 | +// 可以说线段树只是用来保证复杂度的... |
| 27 | +void query(int o, int l, int r, const E &e) |
| 28 | +{ |
| 29 | + if (L[o] > r || R[o] < l) |
| 30 | + return; |
| 31 | + if (l <= L[o] && R[o] <= r) |
| 32 | + { |
| 33 | + tag[o].push_back(e); |
| 34 | + return; |
| 35 | + } |
| 36 | + query(lc, l, r, e); |
| 37 | + query(rc, l, r, e); |
| 38 | +} |
| 39 | + |
| 40 | +// merged tree(root=x)->tree(root=y) |
| 41 | +struct P |
| 42 | +{ |
| 43 | + int x, y; |
| 44 | + P(int a, int b) : x(a), y(b) {} |
| 45 | +}; |
| 46 | +int fa[N], size[N], dis[N]; |
| 47 | +inline int getpar(int x) |
| 48 | +{ |
| 49 | + while (fa[x] != x) |
| 50 | + x = fa[x]; |
| 51 | + return x; |
| 52 | +} |
| 53 | + |
| 54 | +// 到并查集根所经过的边数mod2. |
| 55 | +// mod2意义下的加法即为xor. |
| 56 | +// 用来查询x和根是不是相同颜色. |
| 57 | +inline int getdis(int x) |
| 58 | +{ |
| 59 | + int d = 0; |
| 60 | + while (fa[x] != x) |
| 61 | + { |
| 62 | + d ^= dis[x]; |
| 63 | + x = fa[x]; |
| 64 | + } |
| 65 | + return d; |
| 66 | +} |
| 67 | +/// 按照size合并. |
| 68 | +inline int solve(int x, int y, std::stack<P> &stk) |
| 69 | +{ |
| 70 | + int dx = getdis(x), dy = getdis(y); |
| 71 | + x = getpar(x); |
| 72 | + y = getpar(y); |
| 73 | + if (x == y) |
| 74 | + return (dx ^ dy); |
| 75 | + if (size[x] > size[y]) |
| 76 | + { |
| 77 | + std::swap(x, y); |
| 78 | + std::swap(dx, dy); |
| 79 | + } |
| 80 | + size[y] += size[x]; |
| 81 | + fa[x] = y; |
| 82 | + dis[x] = (dx ^ dy ^ 1); |
| 83 | + stk.push(P(x, y)); |
| 84 | + return 1; |
| 85 | +} |
| 86 | +// 撤销(x->y)的合并.拆出来,size改回去.dis改成0 |
| 87 | +inline void back(P p) |
| 88 | +{ |
| 89 | + int x = p.x, y = p.y; |
| 90 | + size[y] -= size[x]; |
| 91 | + dis[fa[x] = x] = 0; |
| 92 | +} |
| 93 | +// 递归线段树,进入区间[l,r]时 |
| 94 | +// 保证时间轴上[l,r]范围内一直可用的边都被加入了 |
| 95 | +void dfs(int o, int flag) |
| 96 | +{ |
| 97 | + std::stack<P> stk; |
| 98 | + for (auto e : tag[o]) |
| 99 | + flag &= solve(e.u, e.v, stk); |
| 100 | + if (L[o] == R[o]) |
| 101 | + ans[L[o]] = flag; |
| 102 | + else |
| 103 | + { |
| 104 | + dfs(lc, flag); |
| 105 | + dfs(rc, flag); |
| 106 | + } |
| 107 | + // 撤销操作...不然从爸爸进入兄弟节点的时候开始讲到的那个性质会被破坏. |
| 108 | + // 注意撤销顺序... |
| 109 | + while (!stk.empty()) |
| 110 | + { |
| 111 | + back(stk.top()); |
| 112 | + stk.pop(); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +int read() |
| 117 | +{ |
| 118 | + int x = 0; |
| 119 | + char c; |
| 120 | + do |
| 121 | + { |
| 122 | + c = getchar(); |
| 123 | + } while (!isdigit(c)); |
| 124 | + do |
| 125 | + { |
| 126 | + x = x * 10 + c - '0'; |
| 127 | + c = getchar(); |
| 128 | + } while (isdigit(c)); |
| 129 | + return x; |
| 130 | +} |
| 131 | +int main() |
| 132 | +{ |
| 133 | + n = read(); |
| 134 | + m = read(); |
| 135 | + build(1, 1, m); |
| 136 | + int x, y; |
| 137 | + for (int i = 1; i <= m; i++) |
| 138 | + { |
| 139 | + x = read(); |
| 140 | + y = read(); |
| 141 | + if (x > y) |
| 142 | + std::swap(x, y); |
| 143 | + if (g[x][y] == 0) |
| 144 | + g[x][y] = i; |
| 145 | + else |
| 146 | + { |
| 147 | + query(1, g[x][y], i - 1, E(x, y)); |
| 148 | + g[x][y] = 0; |
| 149 | + } |
| 150 | + } |
| 151 | + for (int i = 1; i <= n; i++) |
| 152 | + { |
| 153 | + size[fa[i] = i] = 1; |
| 154 | + for (auto p : g[i]) |
| 155 | + if (p.second > 0) |
| 156 | + query(1, p.second, m, E(i, p.first)); |
| 157 | + } |
| 158 | + dfs(1, 1); |
| 159 | + for (int i = 1; i <= m; i++) |
| 160 | + puts(ans[i] ? "YES" : "NO"); |
| 161 | + return 0; |
| 162 | +} |
0 commit comments