#include <iostream> #include <algorithm> using namespace std; const int M = 300005, Q = 1300005; int n, q, rep[Q], o[M], s[Q], s2[Q], cur; bool full[Q]; int Find(int v) { if (v != rep[v]) rep[v] = Find(rep[v]); return rep[v]; } void Union(int a, int b) { a = Find(a); b = Find(b); if (a != b) { if (s[a] < s[b]) swap(a, b); rep[b] = a; s[a] += s[b]; s2[a] += s2[b]; full[a] |= full[b]; } else full[a] = 1; } int32_t main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); cin >> n >> q; for (int i = 1; i <= n; i++) { o[i] = rep[i] = i; s[i] = s2[i] = 1; } cur = n + 1; for (; q--;) { char a; int b, c; cin >> a >> b; if (a == '+') { cin >> c; Union(o[b], o[c]); } else if (a == '-') { s2[Find(o[b])]--; o[b] = cur; rep[cur] = cur; s2[cur] = s[cur] = 1; cur++; } else { if (full[Find(o[b])]) cout << 1; else if (s2[Find(o[b])] == 1) cout << 0; else cout << '?'; } } }
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 | #include <iostream> #include <algorithm> using namespace std; const int M = 300005, Q = 1300005; int n, q, rep[Q], o[M], s[Q], s2[Q], cur; bool full[Q]; int Find(int v) { if (v != rep[v]) rep[v] = Find(rep[v]); return rep[v]; } void Union(int a, int b) { a = Find(a); b = Find(b); if (a != b) { if (s[a] < s[b]) swap(a, b); rep[b] = a; s[a] += s[b]; s2[a] += s2[b]; full[a] |= full[b]; } else full[a] = 1; } int32_t main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); cin >> n >> q; for (int i = 1; i <= n; i++) { o[i] = rep[i] = i; s[i] = s2[i] = 1; } cur = n + 1; for (; q--;) { char a; int b, c; cin >> a >> b; if (a == '+') { cin >> c; Union(o[b], o[c]); } else if (a == '-') { s2[Find(o[b])]--; o[b] = cur; rep[cur] = cur; s2[cur] = s[cur] = 1; cur++; } else { if (full[Find(o[b])]) cout << 1; else if (s2[Find(o[b])] == 1) cout << 0; else cout << '?'; } } } |