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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <cstdio>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <ctype.h>

#pragma warning ( disable : 4996 )

using namespace std;

typedef vector<int> vi;

struct DJS {
	vector<int> parent;
	vector<int> size;
	vector<set<int>> children;

	bool join(int i, int j); // definition is below
	int root(int i); // definition is below
	void deleteVertex(int v, int some_other_vertex); // definition is below
	void resetIsolatedVertex(int i) { parent[i] = i; size[i] = 1; children[i].clear(); }
	void decSizeSince(int i) { while (parent[i] != i) { size[parent[i]]--; i = parent[i]; } }

	DJS(int n_) : parent(vi(n_)), size(vi(n_, 1)), children(n_, set<int>()) { for (int i = 0; i < n_; i++) { parent[i] = i; } }
};

int DJS::root(int i) {
	while (i != parent[i]) {
		i = parent[i];
	}
	return parent[i];
}

bool DJS::join(int i, int j) {
	int iRoot = root(i);
	int jRoot = root(j);
	if (iRoot == jRoot)
		return false;
	else {
		if (size[iRoot] >= size[jRoot]) {
			parent[jRoot] = iRoot;
			size[iRoot] += size[jRoot];
			// jRoot was a root till now, it has not belonged to any of children[...]
			children[iRoot].insert(jRoot);
		}
		else { // size[iRoot] < size[jRoot]
			parent[iRoot] = jRoot;
			size[jRoot] += size[iRoot];
			// iRoot was a root till now, it has not belonged to any of children[...]
			children[jRoot].insert(iRoot);
		}
		return true;
	}
}

/**
some_other_vertex is sued ONLY when v is root
*/
void DJS::deleteVertex(int v, int some_other_vertex) {
	if (v != parent[v]) {
		children[parent[v]].erase(v);
		for (int c : children[v])
			parent[c] = parent[v];
		decSizeSince(v);
		resetIsolatedVertex(v);
	}
	else if (size[v] == 1) {
#ifdef _DEBUG
		_DEBUG_ERROR("I suppoed it's an impossible branch; maybe check why we got here?..");
#endif
		resetIsolatedVertex(v);
	}
	else { // v != parent[v] (i.e. v is root) and component is non-trivial; 
		int w = some_other_vertex;
		while (parent[w] != w) {
			size[parent[w]] -= size[some_other_vertex];
			w = parent[w];
		}
		for (int c : children[v]) {
			parent[c] = some_other_vertex;
			size[some_other_vertex] += size[c];
		}
		resetIsolatedVertex(v);
	}
}

vector<set<int> > graphSameUnkn;
vi stForBFS;
int currUsedValueForStForBFS;

string resForEachVertex;

void bfsAndMarkTrue(int start, DJS &theDJS) {
	currUsedValueForStForBFS++;
	queue<int> qqq;
	stForBFS[start] = currUsedValueForStForBFS;
	qqq.push(start);
	while (!qqq.empty()) {
		int curr = qqq.front();
		qqq.pop();
		resForEachVertex[curr] = '1';
		theDJS.resetIsolatedVertex(curr);
		theDJS.children[curr].clear();
		for (int next : graphSameUnkn[curr])
			if (stForBFS[next] < currUsedValueForStForBFS) {
				qqq.push(next);
				stForBFS[next] = currUsedValueForStForBFS;
			};
		graphSameUnkn[curr].clear();
	}
}

int main(int argc, char* argv[])
{
#ifdef _DEBUG
	freopen("input.txt", "rt", stdin);
#endif
	int n, totQueries;
	scanf("%d %d\n", &n, &totQueries);
	DJS theDJS(n+1);
	graphSameUnkn.assign(n+1, set<int>());
	currUsedValueForStForBFS = 0;
	stForBFS.assign(n+1, currUsedValueForStForBFS);
	resForEachVertex = string(n + 1, '0'); resForEachVertex[0] = '~';
	for (int q = 0; q < totQueries; q++) {
#ifdef _DEBUG
		fprintf(stderr, "%d\t%s\n", q, resForEachVertex.c_str());
#endif

		char c;
		int u, v;
		do {
			scanf("%c", &c);
		} while (isspace(c));
		switch (c)
		{
		case '?':
			scanf("%d\n", &v);
			printf("%c", resForEachVertex[v]);
			break;
		case '-':
			scanf("%d\n", &v);
			if (resForEachVertex[v] == '0') {
#ifdef _DEBUG
				_DEBUG_ERROR("resForEachVertex[v] == '0'");
#endif
			}
			else if (resForEachVertex[v] == '1') {
				resForEachVertex[v] = '0';
			}
			else if (resForEachVertex[v] == '?') {
				if (graphSameUnkn[v].empty()) {
#ifdef _DEBUG
					_DEBUG_ERROR("resForEachVertex[v] == '?' should be incompartible with graphSameUnkn[v].empty()");
#endif
				} 
				else if (graphSameUnkn[v].size() == 1) {
					int other = graphSameUnkn[v].begin().operator*();
					if (graphSameUnkn[other].size() == 1) { // exactly one-edge graph
						theDJS.resetIsolatedVertex(v);
						theDJS.resetIsolatedVertex(other);
						resForEachVertex[other] = '0';
						graphSameUnkn[other].clear();
						graphSameUnkn[v].clear();
					}
					else {
						theDJS.decSizeSince(v);
						theDJS.children[theDJS.parent[v]].erase(v);
						theDJS.resetIsolatedVertex(v);
						graphSameUnkn[other].erase(v);
						graphSameUnkn[v].clear();
					}
				}
				else { // graphSameUnkn[v].size() > 1
					vi nabs(graphSameUnkn[v].begin(), graphSameUnkn[v].end());
					theDJS.deleteVertex(v, nabs[0]);
					theDJS.resetIsolatedVertex(v);
					for (unsigned i = 1; i < nabs.size(); i++) {
						graphSameUnkn[nabs[i - 1]].insert(nabs[i]);
						graphSameUnkn[nabs[i]].insert(nabs[i - 1]);
					}
					for (int w : graphSameUnkn[v])
						graphSameUnkn[w].erase(v);
					graphSameUnkn[v].clear();
				}
				resForEachVertex[v] = '0'; // in any case
			}
			else {
#ifdef _DEBUG
				_DEBUG_ERROR("unknown resForEachVertex[v]");
#endif
			}
			break;
		case '+':
			scanf("%d %d\n", &v, &u);
			if (v == u) {
				if (!graphSameUnkn[v].empty()) {
					bfsAndMarkTrue(u, theDJS);
				}
				resForEachVertex[v] = '1'; // in any case
			}
			else { // v != u
				if (resForEachVertex[v] == '1' && resForEachVertex[u] == '1') {
#ifdef _DEBUG
					_DEBUG_ERROR("resForEachVertex[v] == '1' && resForEachVertex[u] == '1'");
#endif
				}
				else if (resForEachVertex[v] == '1') {
					bfsAndMarkTrue(u, theDJS);
				}
				else if (resForEachVertex[u] == '1') {
					bfsAndMarkTrue(v, theDJS);
				}
				else { // for both vertice, resForEachVertex[.] ISN'T '1'
					if (theDJS.join(v, u)) { // new edge, component is growing; all cases 00, 0?, ?0 and ?? allowed
						graphSameUnkn[v].insert(u);
						graphSameUnkn[u].insert(v);
						resForEachVertex[v] = resForEachVertex[u] = '?';
					}
					else { // loop created
						bfsAndMarkTrue(v, theDJS);
					}
				}
			}
			break;
		default:
#ifdef _DEBUG
			_DEBUG_ERROR("Unknown command");
#endif
			;
		}
	}

	printf("\n");
#ifdef _DEBUG
	vi data(100);
	for (int i = 0; i < 100; i++) {
		system("pause");
		scanf("%d", &(data[i]));
	}
#endif
	return 0;
}