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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>
#include <vector>

using namespace std;

#define MAXN 300123

char stan[MAXN];
int color[MAXN];	//	0 - not colored (stan = '?'),  -1 (stan = '1'),  >0 - color (stan = '?')
int colorSize[MAXN];
int colorCount = 0;
vector<int> colorList[MAXN];	//	colorList[i] = list of nodes with color i

int col;

int main() {
	ios_base::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);
	for (int i = 0; i < MAXN; i++) {
		stan[i] = '0';
	}
	int n, q;
	cin >> n >> q;

	char c;
	int a, b;
	for (int i = 0; i < q; i++) {
		cin >> c;
		switch(c) {
			case '+':
				cin >> a >> b;
				if (a==b) {
					stan[a] = '1';
					color[a] = -1;
					break;
				}
				if (color[a] == -1) {	//	stan[a] = '1', so b must be '1'
					stan[b] = '1';
					col = color[b];
					color[b] = -1;
					for (int node : colorList[col]) {
						stan[node] = '1';
						color[node] = -1;
					}
					colorList[col].clear();
					break;
				}
				if (color[b] == -1) {	//	stan[b] = '1', so a must be '1'
					stan[a] = '1';
					col = color[a];
					color[a] = -1;
					for (int node : colorList[col]) {
						stan[node] = '1';
						color[node] = -1;
					}
					colorList[col].clear();
					break;
				}
				if (color[a] == color[b]) {
					if (color[a] == 0) {
						//	both not colored = 0
						colorCount++;
						color[a] = colorCount;
						color[b] = colorCount;
						stan[a] = '?';
						stan[b] = '?';
						colorList[colorCount].push_back(a);
						colorList[colorCount].push_back(b);
						colorSize[colorCount] = 2;
					} else {
						//	both colored != 0
						col = color[a];
						for (int node : colorList[col]) {
							stan[node] = '1';
							color[node] = -1;
						}
						colorList[col].clear();
					}
				} else {	// different color
					if (color[a] == 0) {
						//	a not colored, b colored
						color[a] = color[b];
						stan[a] = '?';
						colorList[color[b]].push_back(a);
						colorSize[color[b]]++;
					} else if (color[b] == 0) {
						//	b not colored, a colored
						color[b] = color[a];
						stan[b] = '?';
						colorList[color[a]].push_back(b);
						colorSize[color[a]]++;
					} else {
						//	both colored
						if (colorSize[color[a]] < colorSize[color[b]]) {
							//	move a to b
							colorSize[color[b]] += colorSize[color[a]];
							colorSize[color[a]] = 0;
							col = color[a];
							for (int node : colorList[col]) {
								color[node] = color[b];
								colorList[color[b]].push_back(node);
							}
							colorList[col].clear();
						} else {
							//	move b to a
							colorSize[color[a]] += colorSize[color[b]];
							colorSize[color[b]] = 0;
							col = color[b];
							for (int node : colorList[col]) {
								color[node] = color[a];
								colorList[color[a]].push_back(node);
							}
							colorList[col].clear();
						}
					}
				}
				break;

			case '-':
				cin >> a;
				if (color[a] == -1) {
					stan[a] = '0';
					color[a] = 0;
					break;
				}
				//	stan[a] = '?'
				col = color[a];
				for (int node : colorList[col]) {
					stan[node] = '1';
					color[node] = -1;
				}
				colorList[col].clear();
				colorSize[col] = 0;
				color[a] = 0;
				stan[a] = '0';
				break;

			case '?':
				cin >> a;
				cout << stan[a];
			break;
			
		}
	}

	return 0;
}