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
//#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")

#include <bits/stdc++.h>

#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define fi first
#define se second

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;

template<typename T>
bool umin(T &a, T b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

template<typename T>
bool umax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

#ifdef KIVI
#define DEBUG for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl

template<class ...Ts>
auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }

#else
#define DEBUG while (false)
#define LOG(...)
#endif

mt19937 rng(4242);

const int max_n = 3e5 + 42, inf = 1000111222;

struct dsu {
    int p_or_sz[max_n];

    void init(int n) {
        for (int i = 0; i < n; ++i) {
            p_or_sz[i] = -1;
        }
    }

    int find_set(int v) {
        if (p_or_sz[v] < 0) {
            return v;
        }
        return p_or_sz[v] = find_set(p_or_sz[v]);
    }

    bool union_set(int v1, int v2) {
        v1 = find_set(v1);
        v2 = find_set(v2);
        if (v1 == v2) {
            return false;
        }
        if (-p_or_sz[v1] > -p_or_sz[v2]) {
            swap(v1, v2);
        }
        p_or_sz[v2] += p_or_sz[v1];
        p_or_sz[v1] = v2;
        return true;
    }
};

int status[max_n];

vector<int> g[max_n];

void solve() {
    int n, q;
    cin >> n >> q;
    dsu DSU; DSU.init(n);
    while(q--) {
        char type;
        cin >> type;
        if(type == '?') {
            int v; cin >> v; v--;
            if(status[v] == 1) cout << '1';
            else {
                if(g[v].empty()) cout << '0';
                else cout << '?';
            }
        } else if(type == '+') {
            int a, b;
            cin >> a >> b; a--; b--;
            bool turn_on = false;
            if(status[a] || status[b]) turn_on = true;
            if(DSU.find_set(a) == DSU.find_set(b)) turn_on = true;
            if(turn_on) {
                queue<int> q; q.push(a); q.push(b);
                while(!q.empty()) {
                    auto cr = q.front(); q.pop();
                    DSU.p_or_sz[cr] = -1;
                    status[cr] = 1;
                    for(auto& to : g[cr]) {
                       if(!status[to]) q.push(to);
                    }
                    g[cr].clear();
                }
            } else {
                g[a].pb(b);
                g[b].pb(a);
                DSU.union_set(a, b);
            }
        } else if(type == '-') {
            int v; cin >> v; v--;
            if(status[v] == 1) {
                status[v] = 0;
            } else {
                queue<int> q; q.push(v);
                while(!q.empty()) {
                    auto cr = q.front(); q.pop();
                    DSU.p_or_sz[cr] = -1;
                    status[cr] = 1;
                    for(auto& to : g[cr]) {
                        if(!status[to]) q.push(to);
                    }
                    g[cr].clear();
                }
                status[v] = 0;
            }
        }
    }
}

int main() {
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int t = 1;

//    cin >> t;

    while (t--) solve();

}

/*
KIVI
*/