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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Author: Kajetan Ramsza
#include "bits/stdc++.h"
using namespace std;

#define rep(i, a, b) for(int i = (a); i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

template<typename T> ostream &operator << ( ostream & os, const set< T > &v ) {
    os << "[";
    typename set< T > :: const_iterator it;
    for ( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << *it;
    }
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
    os << "[";
    typename map< F , S >::const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << it -> first << " = " << it -> second ;
    }
    return os << "]";
}

template<typename F, typename S> ostream& operator<<(ostream& os, const pair<F, S> &p) { return os<<"("<<p.first<<", "<<p.second<<")"; }
template<typename T> ostream &operator<<(ostream & os, const vector<T> &v) { os << "{"; typename vector< T > :: const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) { if( it != v.begin() ) os << ", "; os << *it; } return os << "}"; }


void dbg_out() { cerr<<'\n'; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr<<' '<<H; dbg_out(T...); }

#ifdef DEBUG
#define dbg(...) cerr<<"(" << #__VA_ARGS__ <<"):", dbg_out(__VA_ARGS__)
#else
#define dbg(...) 
#endif

int n, q;

vector<vi> ele;
vector<vi> deleted;
vi dsu;

vi computer;
vi indices; 

vi remove_deleted(int v) {
	vi res;
	sort(all(ele[v]));
	sort(all(deleted[v]));
	while(ele[v].size()) {
		if(deleted[v].size() && deleted[v].back() == ele[v].back())
			deleted[v].pop_back();
		else res.push_back(ele[v].back());
		ele[v].pop_back();
	}
	return res;
}

bool merge(int a, int b) {
	a = dsu[a];
	b = dsu[b];
	if(a == b) return true;
	if(sz(ele[a]) - sz(deleted[a]) < sz(ele[b]) - sz(deleted[b])) swap(a,b);
	vi elements = remove_deleted(b);
	for(auto x : elements) {
		dsu[x] = a;
		ele[a].push_back(x);
	}
	indices.push_back(b);
	return false;
}

void reset(int v, int val) {
	vi elements = remove_deleted(v);
	indices.push_back(v);
	// dbg("reset", v, find(v), val, elements);
	for(auto x : elements) {
		int ind = indices.back();
		indices.pop_back();
		dsu[x] = ind;
		ele[ind] = {x};
		computer[x] = val;
	}
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin>>n>>q;
	dsu.resize(n);
	ele.resize(n);
	deleted.resize(n);
	iota(all(dsu), 0);
	rep(i,0,n) ele[i] = {i};
	computer.assign(n, -1);
	rep(o,0,q) {
		char query;
		cin>>query;
		if(query == '+') {
			int a, b;
			cin>>a>>b;
			a--; b--;
			if(computer[a] == 1) {
				reset(dsu[b], 1);
				continue;
			}
			if(computer[b] == 1) {
				reset(dsu[a], 1);
				continue;
			}
			computer[a] = computer[b] = 0;
			if(merge(a,b)) reset(dsu[a], 1);
		} else if(query == '-') {
			int v; cin>>v; v--;
			deleted[dsu[v]].push_back(v);
			int siz = sz(ele[dsu[v]]) - sz(deleted[dsu[v]]);
			if(siz == 0) {
				ele[dsu[v]].clear();
				deleted[dsu[v]].clear();
				indices.push_back(dsu[v]);
			} else if(siz == 1) {
				ele[dsu[v]] = remove_deleted(dsu[v]);
				computer[ele[dsu[v]][0]] = -1;
			}
			dsu[v] = indices.back();
			indices.pop_back();
			ele[dsu[v]] = {v};
			computer[v] = -1;
		} else {
			int v; cin>>v; v--;
			if(computer[v] == -1) cout<<"0";
			else if(computer[v] == 0) cout<<"?";
			else if(computer[v] == 1) cout<<"1";
			continue;
		}
		dbg(dsu);
		dbg(ele);
		dbg(deleted);
		dbg(computer);
		dbg(indices);
		dbg("");
	}
	cout<<'\n';
}