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
//
// Jan Zachar 12/03/2024
// Modernizacja Bajtocji [A]
// Solve: Laczymy graf. Jezeli zostaje utwozony cykl, to caly graf 
// (wczesniej drzewo) niepewniakow (!!!) staje sie pewniakami
//
#include <iostream>
#include <set>
using namespace std;


int n, q;
char Z;
int a, b;


struct {
	int par[300006];
	int siz[300006];

	void MakeSet(int v)
	{
		par[v] = v;
		siz[v] = 1;
	}

	int Find(int v)
	{
		if (par[v] != v) {
			par[v] = Find(par[v]);
		}
		return par[v];
	}

	void Union(int u, int v)
	{
		u = Find(u);
		v = Find(v);

		if (u == v) return;

		if (siz[u] > siz[v]) swap(u, v);
	
		par[u] = v;
		siz[v] += siz[u];
	}
} UF;

struct {
	set<int> Yes, No;
	set<int> G[300006];

	void init()
	{
		for (int i = 1; i <= n; i++) {
			No.insert(i);
			UF.MakeSet(i);
		}
	}

	void DFS_add(int v, int par)
	{
		for (auto &u : G[v]) {
			if (u != par) DFS_add(u, v);
		}
	
		Yes.insert(v);
		G[v].clear();
	}
	void add(int a, int b)
	{
		// Yes.count(a) && Yes.count(b) will never occur
		if (a == b) DFS_add(a, a);
		else if (Yes.count(a)) DFS_add(b, b);
		else if (Yes.count(b)) DFS_add(a, a);
		else if (UF.Find(a) == UF.Find(b)) DFS_add(a, a);
		else {
			// Just a safety measure
			if (G[a].empty()) UF.MakeSet(a);
			if (G[b].empty()) UF.MakeSet(b);
			UF.Union(a, b);

			G[a].insert(b);
			G[b].insert(a);
		}
		if (No.count(a)) No.erase(a);
		if (No.count(b)) No.erase(b);
	}

	void DFS_root(int v, int par)
	{
		UF.MakeSet(v);
		for (auto &u: G[v]) {
			if (u != par) {
				DFS_root(v, u);
				UF.Union(u, v);
			}
		}
	}
	void remove(int a)
	{
		Yes.erase(a);
		No.insert(a);
		if (!G[a].empty()) {
			// Edge case:
			// a is connected to a single node, which has no other neighbours except
			// node a. We can confidently say that it belongs to `No` set. We clear it
			// later on.
			if (G[a].size() == 1 && G[*G[a].begin()].size() == 1) No.insert(*G[a].begin());
			for (auto &u : G[a]) {
				G[u].erase(a);
				DFS_root(a, a);
			}
			G[a].clear();
		}
	}

	char query(int a)
	{
		if (Yes.count(a)) return '1';
		if (No.count(a)) return '0';
		return '?';
	}
} solve;



int main()
{
	ios_base::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);


	cin >> n >> q;
	solve.init();
	

	while (q--) {
		cin >> Z;
		switch (Z) {
		case '+':
			cin >> a >> b;
			solve.add(a, b);
			break;
		case '-':
			cin >> a;
			solve.remove(a);
			break;
		case '?':
			cin >> a;
			cout << solve.query(a);
			break;
		default: cout << 1/0; // Impossible
		}
	}

	cout << '\n';
	return 0;
}