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

struct DS {
	int parent;
	int rank;
	int computers;
	int people;
} d[3000009];

int make_set(int x) {
	d[x].parent = x;
	d[x].rank = 1;
	return x;
}

int find(int x) {
	if (d[x].parent == x) {
		return x;
	}
	d[x].parent = find(d[x].parent);
	return d[x].parent;
}

void union_(int x, int y) {
	int root_x = find(x);
	if (root_x == 0) {
		root_x = make_set(x);
	}
	int root_y = find(y);
	if (root_y == 0) {
		root_y = make_set(y);
	}
	int computers = 0;
	int people = 0;
	if (root_x == root_y) {
		computers = d[root_x].computers + 1;
		if (d[root_x].people == 0) {
			people = 1;
		} else {
			people = d[root_x].people;
		}
		d[root_x].computers = computers;
		d[root_x].people = people;
		return;
	}

	computers = d[root_x].computers + d[root_y].computers + 1;

	if (d[root_x].people == 0) {
		people += 1;
	} else {
		people += d[root_x].people;
	}

	if (d[root_y].people == 0) {
		people += 1;
	} else {
		people += d[root_y].people;
	}

	if (d[root_x].rank > d[root_y].rank) {
		d[root_y].parent = root_x;
		d[root_x].rank += d[root_y].rank;
		d[root_x].computers = computers;
		d[root_x].people = people;
		return;
	}
	d[root_x].parent = root_y;
	d[root_y].rank += d[root_x].rank;
	d[root_y].computers = computers;
	d[root_y].people = people;
}

int dd[300009];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	int n, q;
	string ans;
	cin >> n >> q;
	int id = 1;
	for (; id <= n; id++) {
		dd[id] = id;
	}
	while (q--) {
		char c;
		int a, b;
		cin >> c;
		if (c == '+') {
			cin >> a >> b;
			union_(dd[a], dd[b]);
		} else if (c == '-') {
			cin >> a;
			int x = find(dd[a]);
			d[x].computers--;
			d[x].people--;
			dd[a] = id;
			id++;
		} else if (c == '?') {
			cin >> a;
			int x = find(dd[a]);
			if (d[x].computers == 0) {
				ans += '0';
			} else if (d[x].computers == d[x].people) {
				ans += '1';
			} else {
				ans += '?';
			}
		}
	}
	cout << ans << "\n";
	return 0;
}