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

using namespace std;

#define st first
#define nd second
typedef long long ll;
typedef long double ld;
typedef __int128 int128;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef pair<double,double> pd;
typedef pair<ll,ll> pl;

const int max_n = 13e5+7;

bool on[max_n];
int rep[max_n];
vi g[max_n];
int rozmiar[max_n];

int akt_rep = 0;

vi temp;
void rebuild(int r){
    if(rozmiar[r] > g[r].size()/2) return;

    for(auto x:g[r])
        if(rep[x] == r) temp.push_back(x);
    
    g[r] = temp;
    temp.clear();
}

void link(int a, int b){
    //cout << "link od a: " << a << " b: " << b << '\n';
    a = rep[a]; b = rep[b];
    rebuild(a); rebuild(b);
    //cout << "rep a: " << a << " rep b: " << b << '\n';
    if(g[a].size() < g[b].size()) swap(a,b);
    for(auto x:g[b]){
        if(rep[x] != b) continue;
        rozmiar[a]++;
        g[a].push_back(x);
        rep[x] = a;
    }
    //g[b].clear(); //imo nie trzeba nawet, bo potem kolejny numer bedzie
}

void clear(int a){
    int r = rep[a];

    /* cout << "clear rep od: " << r << '\n';
    cout << "g: ";
    for(auto x:g[r]) cout << x << ' ';
    cout << '\n'; */

    for(auto x:g[r]){
        if(rep[x] != r) continue;
        rep[x] = ++akt_rep;
        g[rep[x]].push_back(x);
        rozmiar[rep[x]] = 1;
        on[x] = 1;
    }
}

int main(){

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

    int n,q;
    cin >> n >> q;

    for(int i = 1; i <= n; i++){
        rep[i] = i;
        g[i].push_back(i);
        rozmiar[i] = 1;
    }

    akt_rep = n;

    while(q--){

        //cout << '\n';

        char c;
        cin >> c;

        if(c == '+'){
            int a,b;
            cin >> a >> b;
            //cout << "dodanie a: " << a << " b: " << b << '\n';
            if(on[a]){
                //cout << "on a\n";
                clear(b);
            }
            else if(on[b]){
                //cout << "on b\n";
                clear(a);
            }
            else if(rep[a] == rep[b]){
                //cout << "same rep\n";
                clear(a);
            }
            else{
                //cout << "link\n";
                link(a,b);
            }
            //cout << "nowe rep: " << rep[a] << '\n';
        }

        else if(c == '-'){
            int a;
            cin >> a;
            rozmiar[rep[a]]--; //aha tutaj caly czas spojna moze miec numer a!
            int prev_r = rep[a];

            rep[a] = ++akt_rep;
            g[akt_rep].push_back(a);
            rozmiar[akt_rep] = 1;
            on[a] = 0;
            //cout << "usuniecie od a: " << a << " nower rep: " << rep[a] 
            //     << " on: " << on[a] << '\n';
            rebuild(prev_r);
        }
        else{
            int a;
            cin >> a;
            //cout << "zapytanie od a: " << a << " rep: " << rep[a] << " size: "
            //     << g[rep[a]].size() << '\n';
            if(on[a]){
                cout << 1;
                //cout << " on\n";
            }
            else if(rozmiar[rep[a]] == 1){
                cout << 0;
                //cout << " izolowany v\n";
            }
            else{
                cout << '?';
                //cout << " wieksza spojna\n";
            }
        }



    }

    return 0;
}

//g++ -O3 -static -Wall .cpp -std=c++17