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

constexpr int MAX_N = 1.5e6 + 7;

int nxt[MAX_N], siz[MAX_N];
bool has[MAX_N];

int find(int u) {
	if (nxt[u] == u) {
		return u;
	}
	nxt[u] = find(nxt[u]);
	return nxt[u];
}

void unite(int u, int v) {
	u = find(u);
	v = find(v);
	if (u == v) {
		has[u] = true;
	}
	else {
		if (siz[u] > siz[v]) {
			swap(u, v);
		}
		siz[v] += siz[u];
		if (has[u]) {
			has[v] = true;
		}
		nxt[u] = v;
	}
}

int who[MAX_N], last_who;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int n, q;
	cin >> n >> q;
	iota(nxt, nxt + n + q + 5, 0);
	fill(siz, siz + n + q + 5, 1);
	iota(who, who + n + q + 5, 0);
	last_who = n + 1;
	
	string result = "";
	while (q--) {
		char type;
		cin >> type;
		if (type == '+') {
			int u, v;
			cin >> u >> v;
			unite(who[u], who[v]);
		}
		else if (type == '-') {
			int u;
			cin >> u;
			auto tmp = find(who[u]);
			siz[tmp]--;
			who[u] = last_who++;
		}
		else { // type == '?'
			int u;
			cin >> u;
			auto tmp = find(who[u]);
			if (has[tmp]) {
				result += '1';
			}
			else if (siz[tmp] == 1) {
				result += '0';
			}
			else {
				result += '?';
			}
		}
	}
	cout << result << "\n";
	
	return 0;
}