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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#ifndef LOCAL
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#endif

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>
// using namespace __gnu_cxx;
// using namespace __gnu_pbds;

using namespace std;

template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

using ld = long double;
using ll = long long;
using PLL = pair<ll, ll>;
using PII = pair<int, int>;
using VI = V<int>;
using VLL = V<ll>;
using VVI = VV<int>;
using VVLL = VV<ll>;
using Gr = VVI;
using MLL = map<ll, ll>;
#define UMLL unordered_map<ll, ll, custom_hash>

#define fast                                                                                                           \
    ios_base::sync_with_stdio(0);                                                                                      \
    cin.tie(nullptr);                                                                                                  \
    cout.tie(nullptr);

#define CR const &

#define ALL(obj) begin(obj), end(obj)
#define RALL(obj) rbegin(obj), rend(obj)

#ifdef LOCAL
#define OK cerr << "OK" << endl;
#else
#define OK
#endif

template <class T> T sqr(T x) { return x * x; }

template <class T, class U = T> void cmin(T &a, U const &b) {
    if (b < a)
        a = b;
}

template <class T, class U = T> void cmax(T &a, U const &b) {
    if (a < b)
        a = b;
}

#ifdef LOCAL

void print_raw() {}

template <class T> void print_raw(T CR);

void print_raw(string CR);

template <class T, class U> void print_raw(pair<T, U> CR);

template <ranges::range T> void print_raw(T CR);

void print_raw(const char *);

template <class T> void print_raw(T CR t) { cerr << t << " "; }

void print_raw(string CR t) { cerr << t << " "; }

template <class T, class U> void print_raw(pair<T, U> CR t) {
    cerr << '{';
    print_raw(t.first);
    cerr << "\b, ";
    print_raw(t.second);
    cerr << "\b} ";
}

template <ranges::range T> void print_raw(T CR t) {
    cerr << '{';

    for (auto i : t)
        print_raw(i);
    if (!empty(t))
        cerr << '\b';

    cerr << "} ";
}

void print_raw(const char *t) {
    if (!t)
        return; //?

    auto sz = strlen(t);
    cerr << t;
    if (sz == 0 || t[sz - 1] != '\n')
        cerr << ' ';
}

template <class T, class... Ts> void print_raw(T CR t, Ts CR... vals) {
    print_raw(t);
    print_raw(vals...);
}

template <class... Ts> void print(Ts CR... vals) {
    print_raw(vals...);
    cerr << '\n';
}

#else

template <class... Ts> void print_raw(Ts CR... vals) {}

template <class... Ts> inline void print(Ts CR... vals) {}

#endif

template <class T> void show(T CR t, ostream &os = cerr) {
    for (auto CR i : t)
        os << i << " ";
    os << "\n";
}

template <class T> void show2d(T CR t, ostream &os = cerr) {
    for (auto CR i : t)
        show(i, os);
    os << "\n";
}

constexpr ll MOD = 998244353;

constexpr ll INF = 1e16;

const ld PI = atanl(1.0L) * 4;
constexpr ld eps = 1e-6, EPS = 1e-9;

// https://codeforces.com/blog/entry/62393
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

template <class T> T &addfix(T &x, ll mod = MOD) {
    if (x >= mod)
        x -= mod;
    return x;
}

template <class T> T &subfix(T &x, T mod = MOD) {
    if (x < 0)
        x += mod;
    return x;
}

template <class T> T &afix(T &x, ll mod = MOD) {
    if (x < 0)
        x += mod;
    else if (x >= mod)
        x -= mod;
    return x;
}

template <class T> T &fix(T &x, T mod = MOD) {
    x %= mod;
    subfix(x, mod);
    return x;
}

ll qpow(ll a, ll b, ll mod = MOD) {
    ll res = 1;
    while (b) {
        if (b & 1LL)
            res = res * a % mod;

        b >>= 1LL;
        a = sqr(a) % mod;
    }

    return res;
}

ll inv(ll a, ll mod = MOD) { return qpow(a, mod - 2, mod); }

ll rand(ll l, ll r) { return rand() % (r - l + 1) + l; }

class DSU {
    struct T {
        int p = -1, sz = 1;
        bool full = false;
    };

    V<T> a;

    int get(int u) {
        if (a[u].p == -1)
            return u;
        return a[u].p = get(a[u].p);
    }

  public:
    DSU(int n) : a(n) {}

    void unite(int u, int v) {
        u = get(u);
        v = get(v);

        if (u == v) {
            a[u].full = true;
            return;
        }

        if (a[u].sz < a[v].sz)
            swap(u, v);

        a[v].p = u;
        a[u].full |= a[v].full;
        a[u].sz += a[v].sz;
    }

    bool is_full(int u) { return a[get(u)].full; }
    bool is_solo(int u) { return a[get(u)].sz == 1; }

    void remove(int u) { a[get(u)].sz -= 1; }
};

void init() {}

void solve(int test_id) {
    int n, q;
    cin >> n >> q;

    VI ind(n);
    iota(ALL(ind), 0);
    int last = n;

    DSU dsu(n + q);
    Gr G(n + q);

    while (q--) {
        char c;
        cin >> c;

        int u;
        cin >> u;
        --u;

        if (c == '?') {
            u = ind[u];
            if (dsu.is_full(u))
                cout << '1';
            else if (dsu.is_solo(u))
                cout << '0';
            else
                cout << '?';
        } else if (c == '-') {
            dsu.remove(ind[u]);
            ind[u] = last++;
        } else if (c == '+') {
            int v;
            cin >> v;
            --v;

            u = ind[u];
            v = ind[v];

            dsu.unite(u, v);
        }
    }
    cout << '\n';
}

int main() {
    auto seed = time(nullptr);
    // cerr << "seed: " << seed << "\n";
    srand(seed);
    fast;
    init();
    int q = 1;
    // cin >> q;

    cout << setprecision(999);
    for (int id = 1; id <= q; ++id)
        solve(id);
}