#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #define FOR(i, n) for (int i = 0; i < n; i++) #define f first #define s second #define pb push_back #define all(s) s.begin(), s.end() #define sz(s) (int)s.size() using vi = vector<int>; const int N = 3e5 + 6; bool ma[N]; int where[N]; vector<unordered_set<int>> comps; int n, q; void print_stan() { cout << "STAN: " << endl; vi vis(n + 1); cout << "MAJĄ: "; for (int i = 1; i <= n; i++) { if (ma[i]) { cout << i << ' '; } } cout << endl; for (int i = 1; i <= n; i++) { cout << "DEBUG: " << i << ' ' << ma[i] << ' ' << where[i] << endl; if (not ma[i] and not vis[i]) { cout << "SPOJNA: "; for (auto &u : comps[where[i]]) { cout << u << ' '; vis[u] = 1; } cout << endl; } } cout << endl; } void solve() { cin >> n >> q; for (int i = 1; i <= n; i++) { ma[i] = 0; where[i] = sz(comps); comps.emplace_back(); comps.back().insert(i); } FOR(query, q) { // cout << flush; char rodzaj; cin >> rodzaj; auto change_to_satisfied = [](int comp) { for (auto &u : comps[comp]) { ma[u] = 1; // cout << "UU " << u << endl; } }; if (rodzaj == '?') { int x; cin >> x; cout << (ma[x] ? '1' : (sz(comps[where[x]]) == 1 ? '0' : '?')); } else if (rodzaj == '+') { int a, b; cin >> a >> b; // cout << "query " << query << ' ' << '+' << a << ' ' << b << endl; assert(not(ma[a] and ma[b])); if (where[a] == where[b]) { // cout << "WSZYSCY MAJĄ! " << endl; change_to_satisfied(where[a]); } else if (ma[a]) { change_to_satisfied(where[b]); } else if (ma[b]) { change_to_satisfied(where[a]); } else { if (sz(comps[where[a]]) < sz(comps[where[b]])) { swap(a, b); } for (auto &u : comps[where[b]]) { where[u] = where[a]; comps[where[a]].insert(u); } } } else if (rodzaj == '-') { int x; cin >> x; // cout << "query " << query << ' ' << '-' << x << endl; assert(ma[x] or sz(comps[where[x]]) > 1); if (not ma[x]) { comps[where[x]].erase(x); } ma[x] = 0; where[x] = sz(comps); comps.emplace_back(); comps.back().insert(x); } else { assert(false); } if (rodzaj == '?') { continue; } // print_stan(); } cout << '\n'; } int main() { ios::sync_with_stdio(0); cin.tie(0); int tests = 1; // cin >> tests; for (int test = 1; test <= tests; test++) { 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 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 158 159 160 161 162 163 164 165 | #pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #define FOR(i, n) for (int i = 0; i < n; i++) #define f first #define s second #define pb push_back #define all(s) s.begin(), s.end() #define sz(s) (int)s.size() using vi = vector<int>; const int N = 3e5 + 6; bool ma[N]; int where[N]; vector<unordered_set<int>> comps; int n, q; void print_stan() { cout << "STAN: " << endl; vi vis(n + 1); cout << "MAJĄ: "; for (int i = 1; i <= n; i++) { if (ma[i]) { cout << i << ' '; } } cout << endl; for (int i = 1; i <= n; i++) { cout << "DEBUG: " << i << ' ' << ma[i] << ' ' << where[i] << endl; if (not ma[i] and not vis[i]) { cout << "SPOJNA: "; for (auto &u : comps[where[i]]) { cout << u << ' '; vis[u] = 1; } cout << endl; } } cout << endl; } void solve() { cin >> n >> q; for (int i = 1; i <= n; i++) { ma[i] = 0; where[i] = sz(comps); comps.emplace_back(); comps.back().insert(i); } FOR(query, q) { // cout << flush; char rodzaj; cin >> rodzaj; auto change_to_satisfied = [](int comp) { for (auto &u : comps[comp]) { ma[u] = 1; // cout << "UU " << u << endl; } }; if (rodzaj == '?') { int x; cin >> x; cout << (ma[x] ? '1' : (sz(comps[where[x]]) == 1 ? '0' : '?')); } else if (rodzaj == '+') { int a, b; cin >> a >> b; // cout << "query " << query << ' ' << '+' << a << ' ' << b << endl; assert(not(ma[a] and ma[b])); if (where[a] == where[b]) { // cout << "WSZYSCY MAJĄ! " << endl; change_to_satisfied(where[a]); } else if (ma[a]) { change_to_satisfied(where[b]); } else if (ma[b]) { change_to_satisfied(where[a]); } else { if (sz(comps[where[a]]) < sz(comps[where[b]])) { swap(a, b); } for (auto &u : comps[where[b]]) { where[u] = where[a]; comps[where[a]].insert(u); } } } else if (rodzaj == '-') { int x; cin >> x; // cout << "query " << query << ' ' << '-' << x << endl; assert(ma[x] or sz(comps[where[x]]) > 1); if (not ma[x]) { comps[where[x]].erase(x); } ma[x] = 0; where[x] = sz(comps); comps.emplace_back(); comps.back().insert(x); } else { assert(false); } if (rodzaj == '?') { continue; } // print_stan(); } cout << '\n'; } int main() { ios::sync_with_stdio(0); cin.tie(0); int tests = 1; // cin >> tests; for (int test = 1; test <= tests; test++) { solve(); } return 0; } |