#include <bits/stdc++.h> #define dbg(x) " [" << #x << ": " << (x) << "] " using namespace std; using ll = long long; template<typename A, typename B> ostream& operator<<(ostream& out, const pair<A,B>& p) { return out << "(" << p.first << ", " << p.second << ")"; } template<typename T> ostream& operator<<(ostream& out, const vector<T>& c) { out << "{"; for(auto it = c.begin(); it != c.end(); it++) { if(it != c.begin()) out << ", "; out << *it; } return out << "}"; } const int N = 3e5 + 10; unordered_set<int> st[N]; int info_id[N]; bool black[N]; void make_black(int a) { for(int x : st[a]) { black[x] = true; } st[a].clear(); } void un(int a, int b) { if(st[a].size() < st[b].size()) swap(a, b); for(int x : st[b]) { info_id[x] = info_id[a]; st[a].insert(x); } st[b].clear(); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; vector<int> info(n); info.reserve(n + q); for(int i = 1; i <= n; i++) { info_id[i] = i - 1; info[i - 1] = i; st[i].insert(i); } while(q--) { char op; int a, b; cin >> op; if(op == '?') { cin >> a; if(black[a]) { cout << 1; } else if(st[info[info_id[a]]].size() == 1) { cout << 0; } else { cout << '?'; } } else if(op == '-') { cin >> a; if(black[a]) { black[a] = false; } else { int p = info[info_id[a]]; st[p].erase(a); if(p == a && st[p].size()) { int nw_p = *st[p].begin(); info[info_id[a]] = nw_p; st[p].swap(st[nw_p]); } } info_id[a] = info.size(); info.push_back(a); st[a].insert(a); } else { cin >> a >> b; if(black[a]) { b = info[info_id[b]]; make_black(b); } else if(black[b]) { a = info[info_id[a]]; make_black(a); } else { a = info[info_id[a]]; b = info[info_id[b]]; if(a == b) make_black(a); else un(a, b); } } } cout << endl; 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 | #include <bits/stdc++.h> #define dbg(x) " [" << #x << ": " << (x) << "] " using namespace std; using ll = long long; template<typename A, typename B> ostream& operator<<(ostream& out, const pair<A,B>& p) { return out << "(" << p.first << ", " << p.second << ")"; } template<typename T> ostream& operator<<(ostream& out, const vector<T>& c) { out << "{"; for(auto it = c.begin(); it != c.end(); it++) { if(it != c.begin()) out << ", "; out << *it; } return out << "}"; } const int N = 3e5 + 10; unordered_set<int> st[N]; int info_id[N]; bool black[N]; void make_black(int a) { for(int x : st[a]) { black[x] = true; } st[a].clear(); } void un(int a, int b) { if(st[a].size() < st[b].size()) swap(a, b); for(int x : st[b]) { info_id[x] = info_id[a]; st[a].insert(x); } st[b].clear(); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; vector<int> info(n); info.reserve(n + q); for(int i = 1; i <= n; i++) { info_id[i] = i - 1; info[i - 1] = i; st[i].insert(i); } while(q--) { char op; int a, b; cin >> op; if(op == '?') { cin >> a; if(black[a]) { cout << 1; } else if(st[info[info_id[a]]].size() == 1) { cout << 0; } else { cout << '?'; } } else if(op == '-') { cin >> a; if(black[a]) { black[a] = false; } else { int p = info[info_id[a]]; st[p].erase(a); if(p == a && st[p].size()) { int nw_p = *st[p].begin(); info[info_id[a]] = nw_p; st[p].swap(st[nw_p]); } } info_id[a] = info.size(); info.push_back(a); st[a].insert(a); } else { cin >> a >> b; if(black[a]) { b = info[info_id[b]]; make_black(b); } else if(black[b]) { a = info[info_id[a]]; make_black(a); } else { a = info[info_id[a]]; b = info[info_id[b]]; if(a == b) make_black(a); else un(a, b); } } } cout << endl; return 0; } |