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
#include <iostream>
#include <list>

using namespace std;

struct superlist {
    int n;
    int t;
    int s;
    list<int>l;
};

int n, q, in1, in2, f, w1, w2;
char in;
superlist tab[2000001];

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>q;
    f = n;
    for(int i = 1; i <= n; i++) {
        tab[i].l.push_back(i);
        tab[i].t = -1;
        tab[i].n = i;
        tab[i].s = 1;
    }
    for(int i = 0; i < q; i++) {
        cin>>in;
        if(in == '+') {
            cin>>in1>>in2;
            w1 = tab[in1].n;
            w2 = tab[in2].n;
            if(w1 == w2) tab[w1].t = 1;
            else {
                if(tab[w1].l.size() < tab[w2].l.size()) {
                    for(auto x : tab[w1].l) {
                        if(tab[x].n == w1) {
                            tab[w2].l.push_back(x);
                            tab[x].n = w2;
                        }
                    }
                    tab[w1].l.clear();
                    if(tab[w1].t == 1 || tab[w2].t == 1) tab[w2].t = 1;
                    else tab[w2].t = 0;
                    tab[w2].s += tab[w1].s;
                    tab[w1].s = 0;
                }
                else {
                    for(auto x : tab[w2].l) {
                        if(tab[x].n == w2) {
                            tab[w1].l.push_back(x);
                            tab[x].n = w1;
                        }
                    }
                    tab[w2].l.clear();
                    if(tab[w2].t == 1 || tab[w1].t == 1) tab[w1].t = 1;
                    else tab[w1].t = 0;
                    tab[w1].s += tab[w2].s;
                    tab[w2].s = 0;
                }
            }
        }
        else if(in == '-') {
            cin>>in1;
            w1 = tab[in1].n;
            if(--tab[w1].s == 1 && tab[w1].t == 0) tab[w1].t = -1;
            tab[in1].n = ++f;
            tab[f].l.push_back(in1);
            tab[f].t = -1;
            tab[f].s = 1;
        }
        else {
            cin>>in1;
            if(tab[tab[in1].n].t == -1) cout<<0;
            else if(tab[tab[in1].n].t == 1) cout<<1;
            else cout<<'?';
        }
    }
}