#include<bits/stdc++.h> #define MAXN 300311 #define pb push_back using namespace std; int n, q; int a, b, c, d; char ch; char state[MAXN]; queue<char> buff; set<int>* spoj[MAXN]; void newSpoj(int x) { set<int> s; s.insert(x); spoj[x] = new set<int>; (*spoj[x]) = s; } void clearSpoj(int x) { for (int e : (*spoj[x])) { if (e != x) { newSpoj(e); state[e] = 't'; } } newSpoj(x); state[x] = 't'; } void unionSpoj(int x, int y) { if ((*spoj[x]).size() < (*spoj[y]).size()) { swap(x, y); } for (int e : (*spoj[y])) { spoj[e] = spoj[x]; (*spoj[x]).insert(e); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> q; for (int i = 0; i <= n; i++) { state[i] = 'e'; newSpoj(i); } for (int i = 0; i < q; i++) { cin >> ch; if (ch == '+') { cin >> a >> b; if (a == b) { clearSpoj(a); } else if (state[a] == 'u' && state[b] == 'u' && (*spoj[a]).find(b) != (*spoj[a]).end()) { clearSpoj(a); } else if (state[a] == 'u' && state[b] == 'u') { unionSpoj(a, b); } else if (state[a] == 't' && state[b] == 'u') { clearSpoj(b); } else if(state[a] == 'u' && state[b] == 't') { clearSpoj(a); } else if ((state[a] == 'e' && state[b] == 'u') || (state[a] == 'u' && state[b] == 'e') || (state[a] == 'e' && state[b] == 'e')) { state[a] = state[b] = 'u'; unionSpoj(a, b); } else if ((state[a] == 't' && state[b] == 'e') || (state[a] == 'e' && state[b] == 't')) { state[a] = state[b] = 't'; } } if (ch == '-') { cin >> c; if ((*spoj[c]).size() == 2) { for(int e : (*spoj[c])) { state[e] = 'e'; if(e != c) { newSpoj(e); } } newSpoj(c); } else { state[c] = 'e'; (*spoj[c]).erase(c); } newSpoj(c); } if (ch == '?') { cin >> d; if (state[d] == 't') { buff.push('1'); } else if (state[d] == 'u') { buff.push('?'); } else if (state[d] == 'e') { buff.push('0'); } } } while(!buff.empty()) { cout<<buff.front(); buff.pop(); } cout<<"\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 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 | #include<bits/stdc++.h> #define MAXN 300311 #define pb push_back using namespace std; int n, q; int a, b, c, d; char ch; char state[MAXN]; queue<char> buff; set<int>* spoj[MAXN]; void newSpoj(int x) { set<int> s; s.insert(x); spoj[x] = new set<int>; (*spoj[x]) = s; } void clearSpoj(int x) { for (int e : (*spoj[x])) { if (e != x) { newSpoj(e); state[e] = 't'; } } newSpoj(x); state[x] = 't'; } void unionSpoj(int x, int y) { if ((*spoj[x]).size() < (*spoj[y]).size()) { swap(x, y); } for (int e : (*spoj[y])) { spoj[e] = spoj[x]; (*spoj[x]).insert(e); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> q; for (int i = 0; i <= n; i++) { state[i] = 'e'; newSpoj(i); } for (int i = 0; i < q; i++) { cin >> ch; if (ch == '+') { cin >> a >> b; if (a == b) { clearSpoj(a); } else if (state[a] == 'u' && state[b] == 'u' && (*spoj[a]).find(b) != (*spoj[a]).end()) { clearSpoj(a); } else if (state[a] == 'u' && state[b] == 'u') { unionSpoj(a, b); } else if (state[a] == 't' && state[b] == 'u') { clearSpoj(b); } else if(state[a] == 'u' && state[b] == 't') { clearSpoj(a); } else if ((state[a] == 'e' && state[b] == 'u') || (state[a] == 'u' && state[b] == 'e') || (state[a] == 'e' && state[b] == 'e')) { state[a] = state[b] = 'u'; unionSpoj(a, b); } else if ((state[a] == 't' && state[b] == 'e') || (state[a] == 'e' && state[b] == 't')) { state[a] = state[b] = 't'; } } if (ch == '-') { cin >> c; if ((*spoj[c]).size() == 2) { for(int e : (*spoj[c])) { state[e] = 'e'; if(e != c) { newSpoj(e); } } newSpoj(c); } else { state[c] = 'e'; (*spoj[c]).erase(c); } newSpoj(c); } if (ch == '?') { cin >> d; if (state[d] == 't') { buff.push('1'); } else if (state[d] == 'u') { buff.push('?'); } else if (state[d] == 'e') { buff.push('0'); } } } while(!buff.empty()) { cout<<buff.front(); buff.pop(); } cout<<"\n"; return 0; } |