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
// Author   : Jakub Rożek
// Task     : Modernizacja Bajtocji
// Contest  : PA 2024 r1 A
// Memory   : O(n + q)
// Time     : O(n + q*alpha)
// Solv     : Rozwiązanie wzorcowe

#include "bits/stdc++.h"
using namespace std;
using LL = long long;
template <typename T>
using P = pair<T, T>;
template <typename T>
using VV = vector<vector<T>>;
#define all(x) x.begin(), x.end()
#define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
#define FORD(i,a,b) for(int i=(a); i>=(b); --i)
#define REP(i,n) for(int i=0; i<(n); ++i)
#define ssize(x) int((x).size())
#ifdef DEBUG
template <typename T1, typename T2>
auto&operator<<(auto&o,pair<T1,T2>p){return o<<'('<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";for(auto e:x)o<<","<<e;return o<<"}";}
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(x)
#else
#define debug(...) {}
#endif

// const int INF = 1'000'000'009;
const int N = 300'000;
// const int Q = 1'000'000;

int n, q, id_rep, id_kraw, x, y;
char z;
vector<int> rep;
VV<P<int>> graph;
bool computer[N+1];
int neighbors[N+1];
vector<bool> active_edges;

int find(int a) {
    if (rep[a] != a) rep[a] = find(rep[a]);
    return rep[a];
}

void unionn(int a, int b) {
    a = find(a);
    b = find(b);
    if (a > n) {
        rep[b] = a;
    } else if (b > n) {
        rep[a] = b;
    } else {
        ++id_rep;
        rep.push_back(id_rep);
        rep[a] = id_rep;
        rep[b] = id_rep;
    }
}

void add_edge(int a, int b) {
    neighbors[a]++;
    neighbors[b]++;
    ++id_kraw;
    active_edges.push_back(true);
    graph[a].push_back({b, id_kraw});
    graph[b].push_back({a, id_kraw});
}

void set_computer(int a) {
    if (computer[a] == 1) return;
    computer[a] = 1;
    for (auto i:graph[a]) {
        if (active_edges[i.second]) {
            active_edges[i.second] = false;
            set_computer(i.first);
        }
    }
    graph[a].clear();
    neighbors[a] = 0;
    rep[a] = a;
}

void add(int a, int b) {
    if (computer[a]) {
        set_computer(b);
    } else if (computer[b]) {
        set_computer(a);
    } else {
        if (find(a) == find(b)) {
            set_computer(a);
        } else {
            unionn(a, b);
            add_edge(a, b);
        }
    }
}

void remove(int a) {
    int tmp = -1;
    for (auto i:graph[a]) {
        if (active_edges[i.second] == false) continue;
        active_edges[i.second] = false;
        --neighbors[i.first];
        if (tmp != -1) {
            add_edge(tmp, i.first);
        }
        tmp = i.first;
    }
    computer[a] = 0;
    neighbors[a] = 0;
    rep[a] = a;
    graph[a].clear();
}

char query(int a) {
    if (computer[a]) return '1';
    if (neighbors[a] == 0) return '0';
    return '?';
}

void solution() {
    cin >> n >> q;

    FOR (i, 0, n) {
        rep.push_back(i);
    }
    id_rep = n;
    id_kraw = -1;
    graph.resize(n+1);

    while (q--) {
        cin >> z;
        if (z == '+') {
            cin >> x >> y;
            add(x, y);
        } else if (z == '-') {
            cin >> x;
            remove(x);
        } else {
            cin >> x;
            cout << query(x);
        }
    }
    cout << '\n';
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int tests = 1;
    // cin>>tests;
    FOR (i, 1, tests) {
        solution();
    }    
    return 0;
}