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
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (b); i >= (a); i--)
#define SZ(x) ((int)x.size())
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define st first
#define nd second
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
auto &operator<<(auto &o, pair<auto, auto> p) {
    return o << "(" << p.st << ", " << p.nd << ")";
}
auto operator<<(auto &o, auto x)->decltype(end(x), o) {
    o << "{"; int i=0; for(auto e: x) o << ", " + 2*!i++ << e;
    return o << "}";
}
#define deb(x...) cerr << "[" #x "]: ", [](auto...$) { ((cerr<<$<<"; "),...) << endl; }(x)

const int nax = 1e6 + 5;
int n, q;
bool komp[nax];
bool niema[nax];

vector<int> setki[nax];
int cnt[nax];
int where[nax];
int used = 0;

void usun(int id, int kto){
    where[kto] = -1;
    cnt[id] -= 1;
    if(cnt[id] == 1){
        int found = 0;
        for(int x : setki[id]){
            if(where[x] != id){
                continue;
            }
            found += 1;
            where[x] = -1;
            komp[x] = false;
        }
        setki[id].clear();
        cnt[id] = 0;
    }
}

void solve(){
    cin >> n >> q;
    rep(i, 1, n){
        komp[i] = false;
        where[i] = -1;
    }

    rep(i, 1, q){
        char tp; cin >> tp;
        if(tp == '?'){
            int d; cin >> d;
            if(where[d] != -1){
                cout << "?";
            }
            else cout << komp[d];
        }
        else if(tp == '+'){
            int a, b; cin >> a >> b;

            if(a == b){
                if(where[a] != -1){
                    int id = where[a];
                    for(int x : setki[id]){
                        if(where[x] != id) continue;
                        komp[x] = true;
                        where[x] = -1;
                    }
                    setki[id].clear();
                    cnt[id] = 0;
                }
                else komp[a] = true;
                continue;
            }

            if(where[a] != -1 && where[b] == -1) swap(a, b);

            if(where[a] == -1 && where[b] == -1){
                if(komp[a] || komp[b]){
                    komp[a] = komp[b] = true;
                }
                else{
                    used += 1;
                    where[a] = where[b] = used;
                    setki[used].pb(a);
                    setki[used].pb(b);
                    cnt[used] = 2;
                }
                continue;
            }

            else if(where[a] == -1){
                if(komp[a]){
                    int id = where[b];
                    for(int x : setki[id]){
                        if(where[x] != id) continue;
                        komp[x] = true;
                        where[x] = -1;
                    }
                    setki[id].clear();
                    cnt[id] = 0;
                }
                else{
                    where[a] = where[b];
                    setki[where[a]].pb(a);
                    cnt[where[a]] += 1;
                }
                continue;
            }

            else{
                int id1 = where[a];
                int id2 = where[b];
                if(id1 == id2){
                    for(int x : setki[id1]){
                        if(where[x] != id1) continue;
                        komp[x] = true;
                        where[x] = -1;
                    }
                    setki[id1].clear();
                    cnt[id1] = 0;
                }
                else{
                    if(SZ(setki[id1]) < SZ(setki[id2])) swap(id1, id2);
                    // id1 >= id2
                    for(int x : setki[id2]){
                        if(where[x] != id2) continue;
                        where[x] = id1;
                        setki[id1].pb(x);
                        cnt[id1] += 1;
                    }
                    setki[id2].clear();
                    cnt[id2] = 0;
                }
                continue;
            }
        }
        else{
            int x; cin >> x;
            if(komp[x]){
                komp[x] = false;
            }
            else usun(where[x], x);
        }
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int tt = 1;
    // cin >> tt;
    rep(te, 1, tt) solve();
    return 0;
}