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
#include <bits/stdc++.h>
using namespace std;
#define rep(a, b) for (int a = 0; a < (b); a++)
#define rep1(a, b) for (int a = 1; a <= (b); a++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int MOD = 1e9 + 7;

#define LOCAL false

const int MAX_N = 3e5 + 7;
const int MAX_Q = 1e6 + 7;
int n, q;

int id = MAX_N+1;
set<int> group[2*MAX_Q];
int groupid[MAX_N];

const int OFF = 0;
const int ON = 1;
const int IDK = 2;
int status[MAX_N];

void enable(int x) {
    int g = groupid[x];
    for (int el: group[g]) {
        status[el] = ON;
        groupid[el] = ++id;
        group[id] = {el};
    }
}
void disable(int x) {
    status[x] = OFF;
    group[groupid[x]].erase(x);
    if (group[groupid[x]].size() == 1) status[*group[groupid[x]].begin()] = OFF;
    groupid[x] = ++id;
    group[id] = {x};
}
void merge(int x, int y) {
    status[x] = IDK;
    status[y] = IDK;
    if (groupid[x] == groupid[y]) {
        enable(x);
        return;
    }

    if (group[groupid[x]].size() < group[groupid[y]].size()) swap(x, y);

    int gy = groupid[y];
    for (int val: group[gy]) {
        group[groupid[x]].insert(val);
        groupid[val] = groupid[x];
    }
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    if (LOCAL) {
        ignore=freopen("io/in", "r", stdin);
        ignore=freopen("io/out", "w", stdout);
    }

    cin >> n >> q;
    rep1(i, n) {
        groupid[i] = i;
        group[i] = {i};
    }

    char mode;
    int a, b;
    rep(i, q) {
        cin >> mode;
        if (mode == '?') {
            cin >> a;
            if (status[a] == IDK) cout << '?';
            else if (status[a] == ON) cout << '1';
            else cout << '0';
        } else if (mode == '+') {
            cin >> a >> b;
            if (status[b] == ON) swap(a, b);
            if (status[a] == ON) enable(b);
            else merge(a, b);
        } else {
            cin >> a;
            disable(a);
        }
    }
    cout << '\n';

    return 0;
}