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

const int N = 3000001;
bool hasComputer[N];
int n, q, a, b;
char operation;
string result;

bool isCycle(vector<vector<int>>& graph, int start) {
	vector<bool> visited(graph.size(), false);
	queue<pair<int, int>> q;

	q.push({ start, -1 });
	visited[start] = true;

	while (!q.empty()) {
		int current = q.front().first;
		int parent = q.front().second;
		q.pop();

		for (int neighbor : graph[current]) {
			if (!visited[neighbor]) {
				visited[neighbor] = true;
				q.push({ neighbor, current });
			}
			else if (neighbor != parent) {

				return true;
			}
		}
	}
	return false;
}

bool isPartOfCycle(vector<vector<int>>& graph, int vertex) {
	return isCycle(graph, vertex);
}

int main() {
	cin >> n >> q;
	vector<vector<int>> graph(n);
	for (int i = 0; i < q; i++) {
		cin >> operation;
		if (operation == '?') {
			cin >> a;
			//Jesli napewno nie ma komputera
			if (hasComputer[a] == 0) {
				result += "0";
				continue;
			}
			//Jesli jest czescia cyklu lub graniczy z cyklem
			if (isPartOfCycle(graph, a)) {
				result += "1";
				continue;
			}
			//Nie wiemy						
			result += "?";
		}
		if (operation == '+') {
			cin >> a >> b;
			graph[a].push_back(b);			
			graph[b].push_back(a);
			hasComputer[a] = 1;
			hasComputer[b] = 1;
		}
		if (operation == '-') {
			cin >> a;
			hasComputer[a] = 0;
		}
	}
	cout << result;
	return 0;
}