#ifdef DEBUG #define _GLIBCXX_DEBUG #endif //#pragma GCC optimize("O3") #include<bits/stdc++.h> using namespace std; #define pb push_back typedef long long ll; typedef long double ld; const int maxN = 3e5 + 10; const int maxP = 2e6 + 10; vector<int> tree[maxP]; int SZ[maxP]; int TIMER = 0; int batya[maxN]; int n; int tp[maxN]; int q; void add(int a, int b) { assert(!tp[a] || !tp[b]); if (tp[a]) { swap(a, b); } if (tp[b]) { int x = batya[a]; for (auto& it : tree[x]) { if (batya[it] != x) continue; tp[it] = 1; } tree[x].clear(); tree[x].shrink_to_fit(); } else { int A = batya[a]; int B = batya[b]; if (A == B) { for (auto& it : tree[A]) { if (batya[it] != A) continue; tp[it] = 1; } tree[A].clear(); tree[A].shrink_to_fit(); return; } if ((int)tree[A].size() < (int)tree[B].size()) { swap(A, B); } for (auto& u : tree[B]) { if (batya[u] != B) continue; tree[A].emplace_back(u); batya[u] = A; } SZ[A] += SZ[B]; tree[B].clear(); tree[B].shrink_to_fit(); } } void del(int c) { if (tp[c] == 1) { tp[c] = 0; TIMER++; assert(TIMER < maxP); tree[TIMER] = {c}; SZ[TIMER] = 1; batya[c] = TIMER; } else { int A = batya[c]; assert(SZ[A] >= 2); SZ[A]--; TIMER++; assert(TIMER < maxP); batya[c] = TIMER; tree[TIMER] = {c}; SZ[TIMER] = 1; } } void solve() { TIMER = 0; cin >> n >> q; for (int i = 1; i <= n; i++) { TIMER++; batya[i] = TIMER; tree[TIMER] = {i}; SZ[TIMER] = 1; } for (int i = 1; i <= q; i++) { char f; cin >> f; if (f == '+') { int a, b; cin >> a >> b; add(a, b); } else if (f == '-') { int c; cin >> c; del(c); } else { assert(f == '?'); int d; cin >> d; if (tp[d] == 1) { cout << 1; } else if (SZ[batya[d]] == 1) { cout << 0; } else { cout << "?"; } } } cout << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); #ifdef DEBUG freopen("input.txt", "r", stdin); #endif int tst = 1; // cin >> tst; while (tst--) { solve(); } 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 | #ifdef DEBUG #define _GLIBCXX_DEBUG #endif //#pragma GCC optimize("O3") #include<bits/stdc++.h> using namespace std; #define pb push_back typedef long long ll; typedef long double ld; const int maxN = 3e5 + 10; const int maxP = 2e6 + 10; vector<int> tree[maxP]; int SZ[maxP]; int TIMER = 0; int batya[maxN]; int n; int tp[maxN]; int q; void add(int a, int b) { assert(!tp[a] || !tp[b]); if (tp[a]) { swap(a, b); } if (tp[b]) { int x = batya[a]; for (auto& it : tree[x]) { if (batya[it] != x) continue; tp[it] = 1; } tree[x].clear(); tree[x].shrink_to_fit(); } else { int A = batya[a]; int B = batya[b]; if (A == B) { for (auto& it : tree[A]) { if (batya[it] != A) continue; tp[it] = 1; } tree[A].clear(); tree[A].shrink_to_fit(); return; } if ((int)tree[A].size() < (int)tree[B].size()) { swap(A, B); } for (auto& u : tree[B]) { if (batya[u] != B) continue; tree[A].emplace_back(u); batya[u] = A; } SZ[A] += SZ[B]; tree[B].clear(); tree[B].shrink_to_fit(); } } void del(int c) { if (tp[c] == 1) { tp[c] = 0; TIMER++; assert(TIMER < maxP); tree[TIMER] = {c}; SZ[TIMER] = 1; batya[c] = TIMER; } else { int A = batya[c]; assert(SZ[A] >= 2); SZ[A]--; TIMER++; assert(TIMER < maxP); batya[c] = TIMER; tree[TIMER] = {c}; SZ[TIMER] = 1; } } void solve() { TIMER = 0; cin >> n >> q; for (int i = 1; i <= n; i++) { TIMER++; batya[i] = TIMER; tree[TIMER] = {i}; SZ[TIMER] = 1; } for (int i = 1; i <= q; i++) { char f; cin >> f; if (f == '+') { int a, b; cin >> a >> b; add(a, b); } else if (f == '-') { int c; cin >> c; del(c); } else { assert(f == '?'); int d; cin >> d; if (tp[d] == 1) { cout << 1; } else if (SZ[batya[d]] == 1) { cout << 0; } else { cout << "?"; } } } cout << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); #ifdef DEBUG freopen("input.txt", "r", stdin); #endif int tst = 1; // cin >> tst; while (tst--) { solve(); } return 0; } |