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

#define fwd(i, a, n) for (int i = (a); i < (n); i ++)
#define rep(i, n) fwd(i, 0, n)
#define all(X) begin(X), end(X)
#define sz(X) ((int)X.size())
#define st first
#define nd second
#define pii pair<int, int>
#define vi vector<int>

#ifdef LOC
auto &operator << (auto &out, pair<auto, auto> a) {
	return out << "(" << a.st << ", " << a.nd << ")";
}

auto &operator << (auto &out, auto a) {
	out << "{";
	for (auto b : a)
		out << b << ", ";
	return out << "}";
}

void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; }

#define debug(x...) cerr << "[" #x "]: ", dump(x)
#else
#define debug(...) 0
#endif

vi siz, repr;

int fnd(int v) {
	return repr[v] == v ? v : repr[v] = fnd(repr[v]);
}

int uni(int v, int u) {
	if (siz[v] < siz[u])
		swap(v, u);
	siz[v] += siz[u];
	repr[u] = v;
	return v;
}

int32_t main() {
   	ios_base::sync_with_stdio(0), cin.tie(0);
	int n, q;
	cin >> n >> q;
	siz.resize(n + q, 1);
	repr.resize(n + q);
	iota(all(repr), 0);
	vi comps(n + q, 0);
	vi dudes(n + q, 1);
	vi who(n);
	iota(all(who), 0);
	int nodes = n;
	string res;
	while (q --) {
		char t;
		cin >> t;
		if (t == '?') {
			int d;
			cin >> d;
			d --;
			int dc = fnd(who[d]);
			if (comps[dc] == 0) {
				res.push_back('0');
			} else if (comps[dc] == dudes[dc]) {
				res.push_back('1');
			} else {
				res.push_back('?');
			}
		} else if (t == '+') {
			int a, b;
			cin >> a >> b;
			a --, b --;
			int ac = fnd(who[a]);
			int bc = fnd(who[b]);
			if (ac == bc) {
				comps[ac] ++;
			} else {
				int abc = uni(ac, bc);
				comps[abc] = comps[ac] + comps[bc] + 1;
				dudes[abc] = dudes[ac] + dudes[bc];
			}
		} else {
			int c;
			cin >> c;
			c --;
			int cc = fnd(who[c]);
			comps[cc] --;
			dudes[cc] --;
			who[c] = nodes ++;
		}
	}
	cout << res << '\n';
   	return 0;
}