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
#include <bits/stdc++.h>

using namespace std;

typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldbl;
typedef pair<int, int> pii;
typedef pair<uint, uint> puu;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<uint> vu;
typedef vector<ll> vll;
typedef vector<ull> vull;
typedef vector<pii> vpii;
typedef vector<puu> vpuu;
typedef vector<pll> vpll;
typedef vector<pull> vpull;
typedef vector<string> vstr;
typedef vector<double> vdbl;
typedef vector<ldbl> vldbl;
#define pb push_back
#define ppb pop_back
#define pfr push_front
#define ppfr pop_front
#define emp emplace
#define empb emplace_back
#define be begin
#define rbe rbegin
#define all(x) (x).be(), (x).end()
#define rall(x) (x).rbe(), (x).rend()
#define fir first
#define sec second
#define mkp make_pair
#define brif(cond) if (cond) break
#define ctif(cond) if (cond) continue
#define retif(cond) if (cond) return
void canhazfast() {ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);}
template<typename T> T gcd(T a, T b) {return b ? gcd(b, a%b) : a;}
template<typename T> T extgcd(T a, T b, T &x, T &y)
{
    T x0 = 1, y0 = 0, x1 = 0, y1 = 1;
    while (b) {
        T q = a/b; a %= b; swap(a, b);
        x0 -= q*x1; swap(x0, x1);
        y0 -= q*y1; swap(y0, y1);
    }
    x = x0; y = y0; return a;
}
int ctz(uint x) {return __builtin_ctz(x);}
int ctzll(ull x) {return __builtin_ctzll(x);}
int clz(uint x) {return __builtin_clz(x);}
int clzll(ull x) {return __builtin_clzll(x);}
int popcnt(uint x) {return __builtin_popcount(x);}
int popcntll(ull x) {return __builtin_popcountll(x);}
int bsr(uint x) {return 31^clz(x);}
int bsrll(ull x) {return 63^clzll(x);}

struct Node {
    int p;
    int sz;
    ///int rnk;
    int elem;
    int cnt;
    int next_leaf;
    int tail_leaf;
};

#define N 300016
#define Q 1000016

char ans[Q];
char f[N];
int cur_node[N];
Node nodes[N];

bool is_single(int a)
{
    int u = cur_node[a];
    return nodes[u].p == u && nodes[u].sz == 1;
}

void rst_node(int u, int a)
{
    nodes[u].p = u;
    nodes[u].sz = 1;
    ///nodes[u].rnk = 0;
    nodes[u].elem = a;
    nodes[u].cnt = 0;
    nodes[u].next_leaf = u; /// ?
    nodes[u].tail_leaf = u; /// ?
}

void put_leaf(int r, int u)
{
    nodes[u].next_leaf = nodes[r].next_leaf;
    nodes[r].next_leaf = u;
}

int fin(int u)
{
    retif(nodes[u].p == u) u;
    int p = nodes[u].p;
    int r = fin(p);
    if (p != r) {
        if (--nodes[p].cnt == 0) put_leaf(r, p);
        ++nodes[r].cnt;
        nodes[u].p = r;
    }
    return r;
}

void uni(int u, int v)
{
    /*u = fin(u);
    v = fin(v);
    retif(u == v) false;*/
    if (nodes[u].sz < nodes[v].sz) swap(u, v);
    nodes[v].p = u;
    ++nodes[u].cnt;
    nodes[u].sz += nodes[v].sz;
    nodes[nodes[u].tail_leaf].next_leaf = nodes[v].next_leaf;
    nodes[u].tail_leaf = nodes[v].tail_leaf;
    //return true;
}

void del(int a)
{
    f[a] = '0';
    int u = cur_node[a];
    int r = fin(u); /// -> u is root or child of root
    retif(nodes[r].sz == 1);
    /// sz >= 2:
    int v = nodes[r].next_leaf;
    if (v != u) {
        int b = nodes[v].elem;
        cur_node[b] = u;
        cur_node[a] = v;
        nodes[u].elem = b;
        ///nodes[v].elem = a;
    }
    /// now remove 1st leaf v
    int p = nodes[v].p;
    if (--nodes[p].cnt == 0) {
        if (p == r) {
            /// sz 2, reset both
            f[nodes[r].elem] = '0';
            rst_node(r, nodes[r].elem);
        } else {
            /// p replaces v as 1st leaf
            if (nodes[r].tail_leaf == v) {
                /// v was the only leaf
                nodes[r].next_leaf = p;
                nodes[r].tail_leaf = p;
            } else {
                /// another leaf exists
                nodes[r].next_leaf = p;
                nodes[p].next_leaf = nodes[v].next_leaf;
            }
        }
    } else {
        /// another leaf exists, only remove v
        nodes[r].next_leaf = nodes[v].next_leaf;
    }
    rst_node(v, a);
}

void set_cc(int r)
{
    int u = nodes[r].next_leaf;
    int tail = nodes[r].tail_leaf;
    nodes[tail].next_leaf = 0;
    while (u) {
        int p = nodes[u].p;
        int a = nodes[u].elem;
        if (--nodes[p].cnt == 0) {
            nodes[tail].next_leaf = p;
            nodes[p].next_leaf = 0;
            tail = p;
        }
        int nxt = nodes[u].next_leaf;
        rst_node(u, a);
        f[a] = '1';
        u = nxt;
    }
}

void set_elem(int a)
{
    int u = cur_node[a];
    int r = fin(u);
    retif(nodes[r].sz > 1) set_cc(r);
    f[a] = '1';
}

int main()
{
    canhazfast();

    int n, m, k = 0;

    cin >> n >> m;
    fill_n(f+1, n, '0');
    for (int i = 1; i <= n; ++i) {
        cur_node[i] = i;
        rst_node(i, i);
    }
    for (int i = 0; i < m; ++i) {
        char t;
        int a, b;
        cin >> t >> a;
        if (t == '+') {
            cin >> b;
            if (a == b) {
                set_elem(a);
            } else if (f[a] == '1') {
                set_elem(b);
            } else if (f[b] == '1') {
                set_elem(a);
            } else {
                int u = cur_node[a], v = cur_node[b];
                int ru = fin(u), rv = fin(v);
                if (ru == rv) set_cc(ru);
                else uni(ru, rv);
            }
        } else if (t == '-') {
            del(a);
        } else {
            ans[k++] = is_single(a) ? f[a] : '?';
        }
    }
    cout << ans << '\n';

    return 0;
}