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


void set_to_one(int start_node,vector<char>& stan,vector<vector<int>>& nodes, int N)
{
	queue<int> q;
	vector<bool> visited(N, false);
	q.push(start_node);
	visited[start_node] = true;
	while(!q.empty()) {
		int n = q.front();
		stan[n] = '1';
		q.pop();
		for(const auto& i: nodes[n]) {
			if(visited[i] == false) {
				visited[i] = true;
				q.push(i);
			}
		}
	}

}

void is_pair(int start_node,vector<char>& stan,vector<vector<int>>& nodes,int N)
{
	//for(const auto& i: nodes[start_node]) {
	//	if((nodes[i].size() == 1) && (nodes[i][0] == start_node)) {
	//		stan[i] = '0';
	//	}
	//}
	if(nodes[start_node].size() == 1) {
		int v = nodes[start_node][0];
		if((nodes[v].size() == 1)&&(nodes[v][0]==start_node))
		{
			stan[v] = '0';
		}
	}
}

void remove_node(int node,vector<vector<int>>& nodes)
{
	for(const auto& i: nodes[node]) {
		nodes[i].erase(find(nodes[i].begin(),nodes[i].end(), node));
	}
	nodes[node] = {};
}

bool is_cycle(int start_node,vector<char>& stan,vector<vector<int>>& nodes, int N)
{
	vector<int> parent(N, -1);
	queue<int> q;
	vector<bool> visited(N, false);
	q.push(start_node);
	visited[start_node] = true;
	while(!q.empty()) {
		int n = q.front();
		q.pop();
		if(stan[n] == '0') {
			return false;
		}
		for(const auto& i: nodes[n]) {
			if(visited[i] == false) {
				visited[i] = true;
				q.push(i);
				parent[i] = n;
			}
			else if(parent[n] != i) {
				return true;
			}
		}
	}
	return false;

}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n = 0;  // l mieszkańców
	int q = 0;  // l wydarzeń
	cin>>n>>q;
	vector<char> stan(n, '0');
	vector<vector<int>> nodes(n, vector<int>(0));
	char c;
	int a, b;
	for(int i = 0; i < q; i++) {
		cin>>c;
		if(c == '+') {
			cin>>a>>b;
			a--;
			b--;
			if(a == b) {
				//nodes[a].push_back(b);
				//nodes[b].push_back(a);
				set_to_one(a, stan, nodes, n);

			}
			else {
				if(stan[a] == '0' && stan[b] == '1') {
					stan[a] = '1';
				}
				else if(stan[a] == '1' && stan[b] == '0') {
					stan[b] = '1';
				}
				else if((stan[a] == '0' && stan[b] == '0')||
					(stan[a] == '0' && stan[b] == '?')||
					(stan[a] == '?' && stan[b] == '0')) {
					stan[b] = '?';
					stan[a] = '?';
				}
				else if(stan[a] == '1' && stan[b] == '?') {
					set_to_one(b,stan,nodes,n);
					//nodes[a].push_back(b);
					//nodes[b].push_back(a);
				}
				else if(stan[a] == '?' && stan[b] == '1') {
					set_to_one(a,stan,nodes,n);
					//nodes[a].push_back(b);
					//nodes[b].push_back(a);
				}

				nodes[a].push_back(b);
				nodes[b].push_back(a);
			}

		}
		else if(c == '-') {
			cin>>a;
			a--;
			if(stan[a] == '?') {
				// czy był cykl
				bool state = is_cycle(a, stan,nodes, n);
				if(state) {
					set_to_one(a,stan,nodes,n);
				}
				is_pair(a,stan,nodes,n);
			}
			stan[a] = '0';

			remove_node(a,nodes);

		}
		else { // ?
			cin>>a;
			a--;
			if(stan[a] != '?') {
				cout<<stan[a];
			}
			else {
				// czy cykl
				bool state = is_cycle(a, stan,nodes, n);
				if(state) {
					set_to_one(a,stan,nodes,n);
				}
				cout<<stan[a];
			}
		}
	}
	cout<<"\n";
	return 0;
}