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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>
#include <climits>
#include <unistd.h>
#include <cstdint>
#include <ctype.h>
using namespace std;
 
using ll = long long;
using ul = unsigned long long;
using db = long double;
using pi = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pi>;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define x first
#define y second

template<class T> using V = vector<T>; 
template<class T, size_t SZ> using AR = array<T,SZ>; 

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define each(a,x) for (auto& a: x)
 
#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
#define rep(i,a,b) for(int i = (a); i < (b); i++)
#define per(i,a,b) for(int i = (b) - 1; i >= (a); i--)

#ifdef LOCAL 
template<class A, class B> auto& operator<<(auto &o, pair<A, B> p) { return o << '(' << p.x << ", " << p.y << ')'; }
auto& operator<<(auto& o, auto a) {
    o << "{";
    for (auto b : a) o << b << ", ";
    return o << "}";
}
void dump(auto... x) { ((cerr << x << ", "), ...) << "\n"; }
#define debug(x...) cerr << "[" #x "]: ", dump(x)
#else
#define debug(...) ;
#endif

template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
template<class T> int lwb(V<T>& a, const T& b) { return int(lb(all(a),b)-bg(a)); }
template<class T> int upb(V<T>& a, const T& b) { return int(ub(all(a),b)-bg(a)); }
template<class T> void remDup(vector<T>& v) { sort(all(v)); v.erase(unique(all(v)),end(v)); }

namespace fast_io {
	static constexpr uint64_t MAX_INPUT_SIZE = 16'000'064, MAX_OUTPUT_SIZE = 1'000'064;
	
	static char input_buffer[MAX_INPUT_SIZE], output_buffer[MAX_OUTPUT_SIZE];
	static uint64_t input_offset = 0, output_offset = 0;
	
	constexpr static inline bool is_ASCII_digit(char character) {
		return '0' <= character;
	}
	
	static inline void read_int(int &number) {
        number = 0;
		char c = input_buffer[input_offset++];
        while (!is_ASCII_digit(c)) c = input_buffer[input_offset++];
		do {
			number = number * 10 + (c - '0');
		} while (is_ASCII_digit(c = input_buffer[input_offset++]));
	}

    static inline void read_char(char &c) {
        c = input_buffer[input_offset++];
    }
	
	static inline void write_char(const char c) {
		output_buffer[output_offset++] = c;
	}
	
	static inline void read_all() {
		read(0, input_buffer, MAX_INPUT_SIZE);
	}
	
	static inline void write_all() {
		write(1, output_buffer, output_offset);
	}
};

constexpr char NO = '0';
constexpr char IDK = '?';
constexpr char YES = '1';

const int N = 3e5+10;
const int Q = 2e6+1024;
int dsu[Q];
int siz[Q];
int nod[Q];
int rnod[Q];
char has[Q];
bool del[Q];
pi edge[Q];
vi g[Q];
vi comp;
int next_edge;
int next_node;

int fnd(int x) { return dsu[x] == x ? x : dsu[x] = fnd(dsu[x]); }
void uni(int a, int b) {
    a = fnd(a);
    b = fnd(b);
    // assert(a != b);
    if (siz[a] > siz[b]) swap(a, b);
    dsu[a] = b;
    siz[b] += siz[a];
}
bool con(int a, int b) { return fnd(a) == fnd(b); }

void dfs(int u, int p = -1) {
    comp.pb(u);
    for (auto e : g[u]) if (!del[e]) {
        int v = edge[e].x == u ? edge[e].y : edge[e].x;
        if (v != p) {
            dfs(v, u);
        }
    }
}

void clr(int u, char new_has) {
    dsu[u] = u;
    siz[u] = 1;
    has[u] = new_has;
    g[u].clear();
}

void clear_comp(int u) {
    comp.clear();
    dfs(u);
    for (auto v : comp) {
        clr(v, YES);
    }
}

void init() {
    fill(siz, siz + Q, 1);
    iota(dsu, dsu + Q, 0);
    fill(has, has + Q, NO);
    iota(nod, nod + Q, 0);
    iota(rnod, rnod + Q, 0);
}

void add_edge(int a, int b) {
    // assert(has[a] != YES || has[b] != YES);
    if (has[a] == YES) {
        clear_comp(b);
        return;
    }
    if (has[b] == YES) {
        clear_comp(a);
        return;
    }
    if (con(a, b)) {
        clear_comp(a);
        // assert(g[a].empty());
        // assert(g[b].empty());
        return;
    }
    has[a] = has[b] = IDK;
    edge[next_edge] = {a, b};
    g[a].pb(next_edge);
    g[b].pb(next_edge);
    next_edge++;
    uni(a, b);
}

void reduce(vi &v) {
    while (!v.empty() && del[v.back()]) v.pop_back();
}

void mark_minus(int u) {
    if (has[u] == NO) cerr << u << " " << has[u] << '\n';
    // assert (has[u] != NO);
    // assert (rnod[u] != -1);

    vpi sons;
    for (auto e : g[u]) if (!del[e]) {
        int v = edge[e].x == u ? edge[e].y : edge[e].x;
        sons.eb(v, e);
    }

    if (sons.empty()) {
        clr(u, NO);
        return;
    }

    int v = rnod[u];
    rnod[u] = -1;
    g[u].clear();

    nod[v] = next_node;
    rnod[next_node] = v;
    next_node++;

    auto [son, e_id] = sons[0];
    del[e_id] = true;
    if (sz(sons) == 1) {
        reduce(g[son]);
        
        if (g[son].empty()) {
            clr(son, NO);
        }

        return;
    }

    rep(i,1,sz(sons)) {
        auto [a, _] = sons[i - 1];
        auto [b, e] = sons[i];
        edge[e] = {a, b};
        g[a].pb(e);
    }
}

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr);

    init();
    fast_io::read_all();

    int n, q;
    fast_io::read_int(n);
    fast_io::read_int(q);
    next_node = n + 1;
    while (q--) {
        char c;
        fast_io::read_char(c);
        if (c == '?') {
            int x;
            fast_io::read_int(x);
            fast_io::write_char(has[nod[x]]);
        }
        else if (c == '+') {
            int a, b;
            fast_io::read_int(a);
            fast_io::read_int(b);
            add_edge(nod[a], nod[b]);
        }
        else {
            // assert(c == '-');
            int x;
            fast_io::read_int(x);
            mark_minus(nod[x]);
        }
    }

    // debug(next_node);
    // debug(next_edge);

    fast_io::write_all();

    return 0;
}