#include <bits/stdc++.h> const int MAX_N = 3e5; const int MAX_Q = 2e6; int father[MAX_Q + 5]; int size[MAX_Q + 5]; int real_size[MAX_Q + 5]; bool disabled[MAX_Q + 5]; bool set_disabled[MAX_Q + 5]; int vertex[MAX_N + 5]; int next_free; // x \in [q] int find_father(int x) { if (x != father[x]) father[x] = find_father(father[x]); return father[x]; } // u,v representants \in [q] void merge(int u, int v) { if (size[u] > size[v]) std::swap(u, v); father[u] = v; size[v] += size[u]; real_size[v] += real_size[u]; } // x \in [n] inline bool is_active(int x) { return !set_disabled[find_father(vertex[x])] && !disabled[vertex[x]]; } // x \in [n] void create_new_set(int x) { vertex[x] = next_free; father[next_free] = next_free; size[next_free] = real_size[next_free] = 1; next_free++; } // x \in [n] void disable(int x) { real_size[find_father(vertex[x])]--; disabled[vertex[x]] = true; } // x \in [n] void disable_set(int x) { int f = find_father(vertex[x]); set_disabled[f] = true; real_size[f] = 0; } // x,y \in [n] void add(int x, int y) { if (x == y) { disable_set(x); } else if (!is_active(x)) { disable_set(y); } else if (!is_active(y)) { disable_set(x); } else { int u = find_father(vertex[x]); int v = find_father(vertex[y]); if (u == v) { disable_set(x); } else { merge(u, v); } } } // x \in [n] void del(int x) { if (!is_active(x)) { create_new_set(x); } else { disable(x); create_new_set(x); } } // x \in [n] void query(int x) { if (!is_active(x)) { std::cout << "1"; } else if (real_size[find_father(vertex[x])] == 1) { std::cout << "0"; } else std::cout << "?"; } int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); int n, q; std::cin >> n >> q; for (int i = 1; i <= n; i++) { vertex[i] = i; father[i] = i; size[i] = real_size[i] = 1; } next_free = n + 1; while (q--) { char c; std::cin >> c; if (c == '+') { int x, y; std::cin >> x >> y; add(x, y); } else if (c == '-') { int x; std::cin >> x; del(x); } else if (c == '?') { int x; std::cin >> x; query(x); } } std::cout << "\n"; }
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | #include <bits/stdc++.h> const int MAX_N = 3e5; const int MAX_Q = 2e6; int father[MAX_Q + 5]; int size[MAX_Q + 5]; int real_size[MAX_Q + 5]; bool disabled[MAX_Q + 5]; bool set_disabled[MAX_Q + 5]; int vertex[MAX_N + 5]; int next_free; // x \in [q] int find_father(int x) { if (x != father[x]) father[x] = find_father(father[x]); return father[x]; } // u,v representants \in [q] void merge(int u, int v) { if (size[u] > size[v]) std::swap(u, v); father[u] = v; size[v] += size[u]; real_size[v] += real_size[u]; } // x \in [n] inline bool is_active(int x) { return !set_disabled[find_father(vertex[x])] && !disabled[vertex[x]]; } // x \in [n] void create_new_set(int x) { vertex[x] = next_free; father[next_free] = next_free; size[next_free] = real_size[next_free] = 1; next_free++; } // x \in [n] void disable(int x) { real_size[find_father(vertex[x])]--; disabled[vertex[x]] = true; } // x \in [n] void disable_set(int x) { int f = find_father(vertex[x]); set_disabled[f] = true; real_size[f] = 0; } // x,y \in [n] void add(int x, int y) { if (x == y) { disable_set(x); } else if (!is_active(x)) { disable_set(y); } else if (!is_active(y)) { disable_set(x); } else { int u = find_father(vertex[x]); int v = find_father(vertex[y]); if (u == v) { disable_set(x); } else { merge(u, v); } } } // x \in [n] void del(int x) { if (!is_active(x)) { create_new_set(x); } else { disable(x); create_new_set(x); } } // x \in [n] void query(int x) { if (!is_active(x)) { std::cout << "1"; } else if (real_size[find_father(vertex[x])] == 1) { std::cout << "0"; } else std::cout << "?"; } int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); int n, q; std::cin >> n >> q; for (int i = 1; i <= n; i++) { vertex[i] = i; father[i] = i; size[i] = real_size[i] = 1; } next_free = n + 1; while (q--) { char c; std::cin >> c; if (c == '+') { int x, y; std::cin >> x >> y; add(x, y); } else if (c == '-') { int x; std::cin >> x; del(x); } else if (c == '?') { int x; std::cin >> x; query(x); } } std::cout << "\n"; } |