#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i = (a); i < (b); i++) #define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--) #define SZ(a) int(a.size()) #define ALL(a) a.begin(), a.end() #define PB push_back #define MP make_pair #define F first #define S second typedef long long LL; typedef vector<int> VI; typedef pair<int, int> PII; typedef long double db; const int N = 300474; int p[N], ind[N]; char ans[N]; VI vec[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; FOR(i, 0, n) { p[i] = -1; ans[i] = '0'; } while (q--) { char t; cin >> t; if (t == '?') { int u; cin >> u; u--; cout << ans[u]; } else if (t == '+') { int u, v; cin >> u >> v; u--; v--; assert(ans[u] != '1' || ans[v] != '1'); if ((ans[u] == '?' && ans[v] == '0') || (ans[u] == '1' && ans[v] == '0') || (ans[u] == '1' && ans[v] == '?')) swap(u, v); if (ans[u] == '0' && ans[v] == '0') { if (u == v) { ans[u] = '1'; } else { vec[u] = {u, v}; p[u] = u; ind[u] = 0; p[v] = u; ind[v] = 1; ans[u] = '?'; ans[v] = '?'; } } else if (ans[u] == '0' && ans[v] == '?') { p[u] = p[v]; ind[u] = SZ(vec[p[u]]); vec[p[u]].PB(u); ans[u] = '?'; } else if (ans[u] == '0' && ans[v] == '1') { ans[u] = '1'; } else { assert(ans[u] == '?'); int pu = p[u], pv = p[v]; if (ans[v] == '1' || pu == pv) { for (int w : vec[pu]) { p[w] = -1; ans[w] = '1'; } vec[pu].clear(); } else { if (SZ(vec[pu]) > SZ(vec[pv])) { swap(u, v); swap(pu, pv); } vec[pv].reserve(SZ(vec[pv]) + SZ(vec[pu])); for (int w : vec[pu]) { p[w] = pv; ind[w] = SZ(vec[pv]); vec[pv].PB(w); } vec[pu].clear(); } } } else { assert(t == '-'); int u; cin >> u; u--; if (ans[u] == '?') { int pu = p[u], w = vec[pu].back(); swap(vec[pu][ind[u]], vec[pu][ind[w]]); swap(ind[u], ind[w]); assert(ind[u] == SZ(vec[pu]) - 1); p[u] = -1; vec[pu].pop_back(); } else { assert(ans[u] == '1'); } ans[u] = '0'; } } 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 | #include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i = (a); i < (b); i++) #define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--) #define SZ(a) int(a.size()) #define ALL(a) a.begin(), a.end() #define PB push_back #define MP make_pair #define F first #define S second typedef long long LL; typedef vector<int> VI; typedef pair<int, int> PII; typedef long double db; const int N = 300474; int p[N], ind[N]; char ans[N]; VI vec[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; FOR(i, 0, n) { p[i] = -1; ans[i] = '0'; } while (q--) { char t; cin >> t; if (t == '?') { int u; cin >> u; u--; cout << ans[u]; } else if (t == '+') { int u, v; cin >> u >> v; u--; v--; assert(ans[u] != '1' || ans[v] != '1'); if ((ans[u] == '?' && ans[v] == '0') || (ans[u] == '1' && ans[v] == '0') || (ans[u] == '1' && ans[v] == '?')) swap(u, v); if (ans[u] == '0' && ans[v] == '0') { if (u == v) { ans[u] = '1'; } else { vec[u] = {u, v}; p[u] = u; ind[u] = 0; p[v] = u; ind[v] = 1; ans[u] = '?'; ans[v] = '?'; } } else if (ans[u] == '0' && ans[v] == '?') { p[u] = p[v]; ind[u] = SZ(vec[p[u]]); vec[p[u]].PB(u); ans[u] = '?'; } else if (ans[u] == '0' && ans[v] == '1') { ans[u] = '1'; } else { assert(ans[u] == '?'); int pu = p[u], pv = p[v]; if (ans[v] == '1' || pu == pv) { for (int w : vec[pu]) { p[w] = -1; ans[w] = '1'; } vec[pu].clear(); } else { if (SZ(vec[pu]) > SZ(vec[pv])) { swap(u, v); swap(pu, pv); } vec[pv].reserve(SZ(vec[pv]) + SZ(vec[pu])); for (int w : vec[pu]) { p[w] = pv; ind[w] = SZ(vec[pv]); vec[pv].PB(w); } vec[pu].clear(); } } } else { assert(t == '-'); int u; cin >> u; u--; if (ans[u] == '?') { int pu = p[u], w = vec[pu].back(); swap(vec[pu][ind[u]], vec[pu][ind[w]]); swap(ind[u], ind[w]); assert(ind[u] == SZ(vec[pu]) - 1); p[u] = -1; vec[pu].pop_back(); } else { assert(ans[u] == '1'); } ans[u] = '0'; } } cout << "\n"; return 0; } |