// #pragma GCC optimize("O3,unroll-loops") #include <bits/stdc++.h> // #include <x86intrin.h> using namespace std; #if __cplusplus >= 202002L using namespace numbers; #endif template<bool Enable_small_to_large = true> struct disjoint_set{ int n, _group_count; vector<int> p; vector<list<int>> group; disjoint_set(){ } disjoint_set(int n): n(n), _group_count(n), p(n, -1), group(n){ assert(n >= 0); for(auto i = 0; i < n; ++ i) group[i] = {i}; } int make_set(){ p.push_back(-1); group.push_back(list<int>{n}); ++ _group_count; return n ++; } int root(int u){ return p[u] < 0 ? u : p[u] = root(p[u]); } bool share(int a, int b){ return root(a) == root(b); } int size(int u){ return -p[root(u)]; } bool merge(int u, int v){ u = root(u), v = root(v); if(u == v) return false; -- _group_count; if constexpr(Enable_small_to_large) if(p[u] > p[v]) swap(u, v); p[u] += p[v], p[v] = u; group[u].splice(group[u].end(), group[v]); return true; } bool merge(int u, int v, auto act){ u = root(u), v = root(v); if(u == v) return false; -- _group_count; bool swapped = false; if constexpr(Enable_small_to_large) if(p[u] > p[v]) swap(u, v), swapped = true; p[u] += p[v], p[v] = u; group[u].splice(group[u].end(), group[v]); act(u, v, swapped); return true; } int group_count() const{ return _group_count; } const list<int> &group_of(int u){ return group[root(u)]; } vector<vector<int>> group_up(){ vector<vector<int>> g(n); for(auto i = 0; i < n; ++ i) g[root(i)].push_back(i); g.erase(remove_if(g.begin(), g.end(), [&](auto &s){ return s.empty(); }), g.end()); return g; } void clear(){ _group_count = n; fill(p.begin(), p.end(), -1); for(auto i = 0; i < n; ++ i) group[i] = {i}; } friend ostream &operator<<(ostream &out, disjoint_set dsu){ auto gs = dsu.group_up(); out << "{"; if(!gs.empty()) for(auto i = 0; i < (int)gs.size(); ++ i){ out << "{"; for(auto j = 0; j < (int)gs[i].size(); ++ j){ out << gs[i][j]; if(j + 1 < (int)gs[i].size()) out << ", "; } out << "}"; if(i + 1 < (int)gs.size()) out << ", "; } return out << "}"; } }; int main(){ cin.tie(0)->sync_with_stdio(0); cin.exceptions(ios::badbit | ios::failbit); int n, qn; cin >> n >> qn; vector<int> repr(n); iota(repr.begin(), repr.end(), 0); vector<int> index(n + qn); iota(index.begin(),index.end(), 0); vector<int> state(n + qn, -1); disjoint_set dsu(n); auto isolate = [&](int ru, int x)->void{ int u = index[ru]; if(!~u){ return; } index[ru] = -1; repr[u] = dsu.make_set(); index[repr[u]] = u; state[repr[u]] = x; }; for(auto qi = 0; qi < qn; ++ qi){ char type; cin >> type; if(type == '+'){ int u, v; cin >> u >> v, -- u, -- v; int ru = repr[u], rv = repr[v]; assert(state[ru] != 1 || state[rv] != 1); if(state[rv] == 1){ swap(ru, rv); } if(dsu.share(ru, rv)){ assert(state[ru] != 1 && state[rv] != 1); ru = dsu.root(ru); for(auto g = dsu.group_of(rv); auto rw: g){ isolate(rw, 1); } } else{ if(state[ru] == 1){ for(auto g = dsu.group_of(rv); auto rw: g){ isolate(rw, 1); } } else{ state[ru] = state[rv] = 0; dsu.merge(ru, rv); } } } else if(type == '-'){ int u; cin >> u, -- u; int ru = repr[u]; assert(state[ru] != -1); isolate(ru, -1); } else{ int u; cin >> u, -- u; int ru = repr[u]; cout << "0?1"[state[ru] + 1]; } } cout << "\n"; return 0; } /* */
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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | // #pragma GCC optimize("O3,unroll-loops") #include <bits/stdc++.h> // #include <x86intrin.h> using namespace std; #if __cplusplus >= 202002L using namespace numbers; #endif template<bool Enable_small_to_large = true> struct disjoint_set{ int n, _group_count; vector<int> p; vector<list<int>> group; disjoint_set(){ } disjoint_set(int n): n(n), _group_count(n), p(n, -1), group(n){ assert(n >= 0); for(auto i = 0; i < n; ++ i) group[i] = {i}; } int make_set(){ p.push_back(-1); group.push_back(list<int>{n}); ++ _group_count; return n ++; } int root(int u){ return p[u] < 0 ? u : p[u] = root(p[u]); } bool share(int a, int b){ return root(a) == root(b); } int size(int u){ return -p[root(u)]; } bool merge(int u, int v){ u = root(u), v = root(v); if(u == v) return false; -- _group_count; if constexpr(Enable_small_to_large) if(p[u] > p[v]) swap(u, v); p[u] += p[v], p[v] = u; group[u].splice(group[u].end(), group[v]); return true; } bool merge(int u, int v, auto act){ u = root(u), v = root(v); if(u == v) return false; -- _group_count; bool swapped = false; if constexpr(Enable_small_to_large) if(p[u] > p[v]) swap(u, v), swapped = true; p[u] += p[v], p[v] = u; group[u].splice(group[u].end(), group[v]); act(u, v, swapped); return true; } int group_count() const{ return _group_count; } const list<int> &group_of(int u){ return group[root(u)]; } vector<vector<int>> group_up(){ vector<vector<int>> g(n); for(auto i = 0; i < n; ++ i) g[root(i)].push_back(i); g.erase(remove_if(g.begin(), g.end(), [&](auto &s){ return s.empty(); }), g.end()); return g; } void clear(){ _group_count = n; fill(p.begin(), p.end(), -1); for(auto i = 0; i < n; ++ i) group[i] = {i}; } friend ostream &operator<<(ostream &out, disjoint_set dsu){ auto gs = dsu.group_up(); out << "{"; if(!gs.empty()) for(auto i = 0; i < (int)gs.size(); ++ i){ out << "{"; for(auto j = 0; j < (int)gs[i].size(); ++ j){ out << gs[i][j]; if(j + 1 < (int)gs[i].size()) out << ", "; } out << "}"; if(i + 1 < (int)gs.size()) out << ", "; } return out << "}"; } }; int main(){ cin.tie(0)->sync_with_stdio(0); cin.exceptions(ios::badbit | ios::failbit); int n, qn; cin >> n >> qn; vector<int> repr(n); iota(repr.begin(), repr.end(), 0); vector<int> index(n + qn); iota(index.begin(),index.end(), 0); vector<int> state(n + qn, -1); disjoint_set dsu(n); auto isolate = [&](int ru, int x)->void{ int u = index[ru]; if(!~u){ return; } index[ru] = -1; repr[u] = dsu.make_set(); index[repr[u]] = u; state[repr[u]] = x; }; for(auto qi = 0; qi < qn; ++ qi){ char type; cin >> type; if(type == '+'){ int u, v; cin >> u >> v, -- u, -- v; int ru = repr[u], rv = repr[v]; assert(state[ru] != 1 || state[rv] != 1); if(state[rv] == 1){ swap(ru, rv); } if(dsu.share(ru, rv)){ assert(state[ru] != 1 && state[rv] != 1); ru = dsu.root(ru); for(auto g = dsu.group_of(rv); auto rw: g){ isolate(rw, 1); } } else{ if(state[ru] == 1){ for(auto g = dsu.group_of(rv); auto rw: g){ isolate(rw, 1); } } else{ state[ru] = state[rv] = 0; dsu.merge(ru, rv); } } } else if(type == '-'){ int u; cin >> u, -- u; int ru = repr[u]; assert(state[ru] != -1); isolate(ru, -1); } else{ int u; cin >> u, -- u; int ru = repr[u]; cout << "0?1"[state[ru] + 1]; } } cout << "\n"; return 0; } /* */ |