#include <iostream> #include <string> #include <unordered_set> #include <vector> #define MAXN 300002 #define MAXQ 1000002 using namespace std; int n, q, a, b; string s; int fails[MAXN]; vector<unordered_set<int>> vs(1); vector<int> where(1); // 0 - full, other - not int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> q; for (int i = 1; i <= n; i++) { where.push_back(i); vs.push_back({i}); } string res = ""; for (int i = 0; i < q; i++) { cin >> s >> a; int t = where[a]; switch (s[0]) { case '+': { cin >> b; if (!where[a] || !where[b] || where[a] == where[b]) { if (!where[b]) swap(a, b); int t2 = where[b]; for (auto c : vs[t2]) { where[c] = 0; } } else { if (vs[where[a]].size() < vs[where[b]].size()) swap(a, b); int t2 = where[b]; t = where[a]; for (auto c : vs[t2]) { vs[t].insert(c); where[c] = t; } } break; } case '-': { if (t != 0) { vs[t].erase(a); } where[a] = vs.size(); vs.push_back({a}); break; } case '?': { if (t == 0) res += '1'; else if (vs[t].size() == 1) res += '0'; else res += '?'; break; } } } cout << res << "\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 | #include <iostream> #include <string> #include <unordered_set> #include <vector> #define MAXN 300002 #define MAXQ 1000002 using namespace std; int n, q, a, b; string s; int fails[MAXN]; vector<unordered_set<int>> vs(1); vector<int> where(1); // 0 - full, other - not int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> q; for (int i = 1; i <= n; i++) { where.push_back(i); vs.push_back({i}); } string res = ""; for (int i = 0; i < q; i++) { cin >> s >> a; int t = where[a]; switch (s[0]) { case '+': { cin >> b; if (!where[a] || !where[b] || where[a] == where[b]) { if (!where[b]) swap(a, b); int t2 = where[b]; for (auto c : vs[t2]) { where[c] = 0; } } else { if (vs[where[a]].size() < vs[where[b]].size()) swap(a, b); int t2 = where[b]; t = where[a]; for (auto c : vs[t2]) { vs[t].insert(c); where[c] = t; } } break; } case '-': { if (t != 0) { vs[t].erase(a); } where[a] = vs.size(); vs.push_back({a}); break; } case '?': { if (t == 0) res += '1'; else if (vs[t].size() == 1) res += '0'; else res += '?'; break; } } } cout << res << "\n"; return 0; } |