#include <vector> #include <stdio.h> #include <list> #include <cassert> #define fi first #define sc second #define pb push_back #define rep(i,p,k) for(int i=(p);i<(k);++i) #define forn(i,p,k) for(int i=(p);i<=(k);++i) #define forr(i,k,p) for(int i=(k)-1;i>=(p);--i) #define each(a,b) for(auto&a:b) #define all(v) begin(v), end(v) #define RET(smth) return void(cout<<(smth)<<'\n'); #define sz(v) (int)v.size() using namespace std; using pii = pair<int,int>; using ll = long long; using Vi = vector<int>; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif const int MN = 3e5+13; const int MQ = 1e6+13; struct NN { int par; int sz; list<int> ls; } F[MN+MQ]; list<int>::iterator my_it[MN]; int idx[MN]; char state[MN]; int IND = 0; int Find(int x) { return x == F[x].par ? x : F[x].par = Find(F[x].par); } void Union(int a, int b) { a = Find(a); b = Find(b); if(F[a].sz < F[b].sz) swap(a,b); auto & A = F[a]; auto & B = F[b]; A.sz += B.sz; B.par = a; A.ls.splice(A.ls.end(),B.ls); } void collapse(int x) { x = Find(x); while(F[x].ls.size() > 1) { auto v = F[x].ls.front(); state[v] = '1'; int id = idx[v]; F[id].sz = 1; F[id].par = id; F[id].ls.splice(F[id].ls.end(), F[x].ls, my_it[v]); } auto v = F[x].ls.front(); state[v] = '1'; idx[v] = x; F[x].sz = 1; F[x].par = x; } void remove(int a) { if(state[a] == '1') { state[a] = '0'; return; } auto & x = F[Find(idx[a])]; auto & nx = F[IND]; nx.sz = 1; nx.par = IND; idx[a] = IND; nx.ls.splice(nx.ls.end(),x.ls,my_it[a]); IND++; x.sz--; if(x.sz == 1) state[x.ls.front()] = '0'; state[a] = '0'; } template<typename... T> void read(T&... xs) { ([](auto&x){ char c=0; for(;c<'0'||c>'9';c=getchar()); for(x=0;'0'<=c&&c<='9';c=getchar()) x = x * 10 + (c & 15); }(xs),...); } char read_char() { char c = 0; while(c != '+' && c != '-' && c != '?') c=getchar(); return c; } int main() { int n,q; read(n,q); IND = n; rep(i,0,n) { F[i].par = i; F[i].sz = 1; my_it[i] = F[i].ls.insert(F[i].ls.end(), i); idx[i] = i; state[i] = '0'; } while(q--) { char z = read_char(); int a; read(a); a--; if(z == '+') { int b; read(b); --b; if(state[b] == '1') swap(a,b); if(state[a] == '1') { if(state[b] == '0') state[b] = '1'; else if(state[b] == '?') collapse(idx[b]); else assert(false); } else { state[a] = state[b] = '?'; a = Find(idx[a]); b = Find(idx[b]); if(a == b) collapse(a); else Union(a,b); } } else if(z == '-') { remove(a); } else { putchar(state[a]); } } putchar('\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 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 | #include <vector> #include <stdio.h> #include <list> #include <cassert> #define fi first #define sc second #define pb push_back #define rep(i,p,k) for(int i=(p);i<(k);++i) #define forn(i,p,k) for(int i=(p);i<=(k);++i) #define forr(i,k,p) for(int i=(k)-1;i>=(p);--i) #define each(a,b) for(auto&a:b) #define all(v) begin(v), end(v) #define RET(smth) return void(cout<<(smth)<<'\n'); #define sz(v) (int)v.size() using namespace std; using pii = pair<int,int>; using ll = long long; using Vi = vector<int>; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif const int MN = 3e5+13; const int MQ = 1e6+13; struct NN { int par; int sz; list<int> ls; } F[MN+MQ]; list<int>::iterator my_it[MN]; int idx[MN]; char state[MN]; int IND = 0; int Find(int x) { return x == F[x].par ? x : F[x].par = Find(F[x].par); } void Union(int a, int b) { a = Find(a); b = Find(b); if(F[a].sz < F[b].sz) swap(a,b); auto & A = F[a]; auto & B = F[b]; A.sz += B.sz; B.par = a; A.ls.splice(A.ls.end(),B.ls); } void collapse(int x) { x = Find(x); while(F[x].ls.size() > 1) { auto v = F[x].ls.front(); state[v] = '1'; int id = idx[v]; F[id].sz = 1; F[id].par = id; F[id].ls.splice(F[id].ls.end(), F[x].ls, my_it[v]); } auto v = F[x].ls.front(); state[v] = '1'; idx[v] = x; F[x].sz = 1; F[x].par = x; } void remove(int a) { if(state[a] == '1') { state[a] = '0'; return; } auto & x = F[Find(idx[a])]; auto & nx = F[IND]; nx.sz = 1; nx.par = IND; idx[a] = IND; nx.ls.splice(nx.ls.end(),x.ls,my_it[a]); IND++; x.sz--; if(x.sz == 1) state[x.ls.front()] = '0'; state[a] = '0'; } template<typename... T> void read(T&... xs) { ([](auto&x){ char c=0; for(;c<'0'||c>'9';c=getchar()); for(x=0;'0'<=c&&c<='9';c=getchar()) x = x * 10 + (c & 15); }(xs),...); } char read_char() { char c = 0; while(c != '+' && c != '-' && c != '?') c=getchar(); return c; } int main() { int n,q; read(n,q); IND = n; rep(i,0,n) { F[i].par = i; F[i].sz = 1; my_it[i] = F[i].ls.insert(F[i].ls.end(), i); idx[i] = i; state[i] = '0'; } while(q--) { char z = read_char(); int a; read(a); a--; if(z == '+') { int b; read(b); --b; if(state[b] == '1') swap(a,b); if(state[a] == '1') { if(state[b] == '0') state[b] = '1'; else if(state[b] == '?') collapse(idx[b]); else assert(false); } else { state[a] = state[b] = '?'; a = Find(idx[a]); b = Find(idx[b]); if(a == b) collapse(a); else Union(a,b); } } else if(z == '-') { remove(a); } else { putchar(state[a]); } } putchar('\n'); return 0; } |