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

using namespace std;

int n;
int new_label;
int parent[1000010];
int group_s[1000010];
int group_c[1000010];
int kc[1000010];

int Find(int a){
    if(parent[a] != a){
        parent[a] = Find(parent[a]);
    }
    return parent[a];
}

void Union(int a, int b){
    a = Find(a);
    b = Find(b);
    group_c[a]++;    
    if(a == b) return;

    parent[b] = a;
    group_c[a] += group_c[b];
    group_s[a] += group_s[b];
}

void add_c(int a, int b){
    Union(kc[a],kc[b]);
}

void del_c(int a){
    int par = Find(kc[a]);
    group_s[par]--;
    group_c[par]--;

    kc[a] = new_label;
    new_label++;
    parent[kc[a]] = kc[a];
    group_s[kc[a]] = 1;
}

char ask_c(int a){
    int par = Find(kc[a]);
    if(group_s[par] == 1 && group_c[par] == 0){
        return '0';
    }
    if(group_s[par] == group_c[par]){
        return '1';
    }
    return '?';
} 



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

    int q; cin >> n >> q;
    new_label = n + 1;
    for(int i = 1; i <= n; i++){
        parent[i] = i;
        group_s[i] = 1;
        kc[i] = i;
    }
    char op;
    int a,b;
    while(q--){
        cin >> op;
        if(op == '+'){
            cin >> a >> b;
            add_c(a,b);
        }
        if(op == '-'){
            cin >> a;
            del_c(a);
        }
        if(op == '?'){
            cin >> a;
            cout << ask_c(a);
        }
    }
    cout << "\n";
    return 0;
}