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
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;

const int MAXQ = 1000001;
const int MAXN = 300001;
vector<unordered_set<int>>Grupa(MAXQ);
vector<int>Belong(MAXN);
vector<char>HC(MAXN, '0');

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int n, q, it = 0;
	cin >> n >> q;
	for (int i = 1; i <= n; i++) {
		Grupa[i].insert(i);
		Belong[i] = ++it;
	}

	string res = "";
	while (q--) {
		char z;
		cin >> z;
		if (z == '?') {
			int a; cin >> a;
			res += HC[a];
		}
		else if (z == '+') {
			int a, b;
			cin >> a >> b;

			if (Belong[a] == Belong[b]) {
				for (auto x : Grupa[Belong[a]])
					HC[x] = '1';
				Grupa[Belong[a]].clear();
			} 
			else if (HC[a] == '1') {
				for (auto x : Grupa[Belong[b]])
					HC[x] = '1';
				Grupa[Belong[b]].clear();
			}
			else if (HC[b] == '1') {
				for (auto x : Grupa[Belong[a]])
					HC[x] = '1';
				Grupa[Belong[a]].clear();
			} 
			else {
				HC[a] = '?';
				HC[b] = '?';
				if (Grupa[Belong[a]].size() < Grupa[Belong[b]].size()) swap(a, b);
				int numer = Belong[b];
				for (auto& x : Grupa[numer]) {
					Belong[x] = Belong[a];
					Grupa[Belong[a]].insert(x);
				}
				Grupa[numer].clear();
			}
		}
		else {
			int a;
			cin >> a;
			Grupa[Belong[a]].erase(a);
			if (Grupa[Belong[a]].size() == 1) {
				HC[*Grupa[Belong[a]].begin()] = '0';
			}
			Belong[a] = ++it;
			Grupa[Belong[a]].insert(a);
			HC[a] = '0';
		}
	}

	cout << res << '\n';

	return 0;
}