#include <bits/stdc++.h> #define int long long using namespace std; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); int n, q; string ans = ""; cin >> n >> q; vector <int> link(n); vector <set <int>> sets(n); vector <int> type(n, 3); queue <int> empt; for(int i = 0; i < n; i++){ link[i] = i; sets[i].insert(i); } //ofstream cout("output.txt"); while(q--){ char t; cin >> t; if(t == '+'){ int a, b; cin >> a >> b; a--;b--; if(link[a] != link[b]){ if(sets[link[a]].size() < sets[link[b]].size()) { swap(a, b); } set <int> st = sets[link[b]]; if(type[link[b]] == 1) type[link[a]] = 1; sets[link[b]].clear(); empt.push(link[b]); for(auto x: st){ link[x] = link[a]; sets[link[a]].insert(x); } } else{ set <int> st = sets[link[a]]; sets[link[a]].clear(); empt.push(link[a]); for(auto x: st){ link[x] = empt.front(); type[link[x]] = 1; sets[link[x]].insert(x); empt.pop(); } } if(type[link[a]] == 1){ set <int> st = sets[link[a]]; sets[link[a]].clear(); empt.push(link[a]); for(auto x: st){ link[x] = empt.front(); type[link[x]] = 1; sets[link[x]].insert(x); empt.pop(); } } } else if(t == '-') { int c; cin >> c; c--; sets[link[c]].erase(c); if(sets[link[c]].size() == 1 && type[*sets[link[c]].begin()] != 1){ type[*sets[link[c]].begin()] = 3; } if(sets[link[c]].size() == 0) empt.push(link[c]); link[c] = empt.front(); sets[link[c]].insert(c); empt.pop(); type[link[c]] = 3; } else{ int d; cin >> d; d--; if(sets[link[d]].size() > 1) { ans += '?'; } else { if(type[link[d]] == 3) ans += '0'; if(type[link[d]] == 0) ans += '?'; if(type[link[d]] == 1) ans += '1'; } } } cout << ans; 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 | #include <bits/stdc++.h> #define int long long using namespace std; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); int n, q; string ans = ""; cin >> n >> q; vector <int> link(n); vector <set <int>> sets(n); vector <int> type(n, 3); queue <int> empt; for(int i = 0; i < n; i++){ link[i] = i; sets[i].insert(i); } //ofstream cout("output.txt"); while(q--){ char t; cin >> t; if(t == '+'){ int a, b; cin >> a >> b; a--;b--; if(link[a] != link[b]){ if(sets[link[a]].size() < sets[link[b]].size()) { swap(a, b); } set <int> st = sets[link[b]]; if(type[link[b]] == 1) type[link[a]] = 1; sets[link[b]].clear(); empt.push(link[b]); for(auto x: st){ link[x] = link[a]; sets[link[a]].insert(x); } } else{ set <int> st = sets[link[a]]; sets[link[a]].clear(); empt.push(link[a]); for(auto x: st){ link[x] = empt.front(); type[link[x]] = 1; sets[link[x]].insert(x); empt.pop(); } } if(type[link[a]] == 1){ set <int> st = sets[link[a]]; sets[link[a]].clear(); empt.push(link[a]); for(auto x: st){ link[x] = empt.front(); type[link[x]] = 1; sets[link[x]].insert(x); empt.pop(); } } } else if(t == '-') { int c; cin >> c; c--; sets[link[c]].erase(c); if(sets[link[c]].size() == 1 && type[*sets[link[c]].begin()] != 1){ type[*sets[link[c]].begin()] = 3; } if(sets[link[c]].size() == 0) empt.push(link[c]); link[c] = empt.front(); sets[link[c]].insert(c); empt.pop(); type[link[c]] = 3; } else{ int d; cin >> d; d--; if(sets[link[d]].size() > 1) { ans += '?'; } else { if(type[link[d]] == 3) ans += '0'; if(type[link[d]] == 0) ans += '?'; if(type[link[d]] == 1) ans += '1'; } } } cout << ans; return 0; } |