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
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define REP(i, n) for(int i = 0; i < (n); ++i)

template<class T> int size(T && a) {
	return (int)(a.size());
}
ostream& operator << (ostream &os, string str) {
    for(char c : str) os << c;
    return os;
}
template <class A, class B> ostream& operator << (ostream &os, const pair<A, B> &p) {
	return os << '(' << p.first << "," << p.second << ')';
}
template <class T> auto operator << (ostream &os, T &&x) -> decltype(x.begin(), os) {
	os << '{';
	for(auto it = x.begin(); it != x.end(); ++it)
		os << *it << (it == prev(x.end()) ? "" : " ");
	return os << '}';
}
template <class T> ostream& operator << (ostream &os, vector<vector<T>> vec) {
    for(auto x : vec)
    	os << "\n  " << x;
    return os;
}
void dump() {}
template <class T, class... Args> void dump(T &&x, Args... args) {
    cerr << x << "; ";
    dump(args...);
}
#ifdef DEBUG
# define debug(x...) cerr << "[" #x "]: ", dump(x), cerr << '\n'
#else 
# define debug(...) 0
#endif

//--------------------------------------------------

mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
int rd(int l, int r) {
	return uniform_int_distribution<int>(l, r)(rng);
}

struct DSU {
	vector<int> leader, comp_size, comp_color;

	DSU(int n) : leader(n), comp_size(n, 1), comp_color(n) {
		REP (i, n) leader[i] = i;
	}

	void Union(int a, int b) {
		a = find(a), b = find(b);

		if (a == b) {
			comp_color[a] = 1;
			comp_color[b] = 1;
			return;
		}

		if (rd(0, 1)) {
			comp_size[b] += comp_size[a];
			comp_color[b] |= comp_color[a];
			leader[a] = b;
		} else {
			comp_size[a] += comp_size[b];
			comp_color[a] |= comp_color[b];
			leader[b] = a;
		}
	}

	int find(int a) {
		if (a == leader[a])
			return a;

		comp_color[leader[a]] |= comp_color[a];
		return leader[a] = find(leader[a]);
	}

	void remove(int a) {
		a = find(a);
		--comp_size[a];
	}

	char answer(int a) {
		a = find(a);
		if (comp_size[a] <= 1 and comp_color[a] == 0)
			return '0';
		return comp_color[a] ? '1' : '?'; 
	}
};

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

	int n, q;
	cin >> n >> q;

	int first_free = n;
	vector<int> nodes_mapping(n);
	REP (i, n) nodes_mapping[i] = i;

	DSU dsu(n + q + 1);

	while (q--) {
		char op; cin >> op;

		if (op == '+') {
			int a, b;
			cin >> a >> b;
			--a, --b;

			a = nodes_mapping[a];
			b = nodes_mapping[b];

			dsu.Union(a, b);
		} else if (op == '-') {
			int c; cin >> c;
			--c;

			dsu.remove(nodes_mapping[c]);
			nodes_mapping[c] = first_free++;
		} else if (op == '?') {
			int d; cin >> d;
			--d;

			d = nodes_mapping[d];
			cout << dsu.answer(d);
		}
	}
}