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
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_set>
using namespace std;

struct find_union{
    int n;
    vector<int> p;
    vector<int> rank;
    vector<int> cc_size;

    find_union(int n) : n(n), p(vector<int>(n)), rank(vector<int>(n, 0)), cc_size(n, 1){
        for(int i = 0; i < n; ++i) p[i] = i;
    }

    int find(int x){
        if(p[x] != x) p[x] = find(p[x]);
        return p[x];
    }

    void onion(int x, int y){        
        x = find(x);
        y = find(y);
        if(x == y) return;

        if(rank[x] > rank[y]) {
            swap(x, y);
        }

        p[x] = y;
        cc_size[y] += cc_size[x];
        if(rank[x] == rank[y]) rank[y] += 1;
    }

    int get_cc_size(int v){
        return cc_size[find(v)];
    }

    void decrement_cc_size(int v){
        cc_size[find(v)]--;
    }
};

void case1(){
    int n, q;
    cin >> n >> q;

    int id[n + 1];
    find_union fu(q + n + 1);
    vector<bool> noForSure(q + n + 1, true);
    int next_id = n + 1;

    for(int i = 0; i < n + 1; ++i)id[i] = i;

    for(int i = 0; i < q; ++i){
        char type;
        cin >> type;

        if(type == '+'){
            int v, w;
            cin >> v >> w;
            v = id[v];
            w = id[w];

            noForSure[v] = false;
            noForSure[w] = false;
            
            if(fu.find(v) == fu.find(w)) fu.onion(0, v);
            else fu.onion(v, w);

        }else if(type == '-'){
            int v;
            cin >> v;

            fu.decrement_cc_size(id[v]);
            id[v] = next_id;
            ++next_id;
        }else if(type == '?'){
            int v;
            cin >> v;
            v = id[v];

            if(noForSure[v] || fu.get_cc_size(v) == 1){
                cout<<'0';
                continue;
            }

            if(fu.find(v) == fu.find(0)) cout << '1';
            else cout << '?';
        }
    }

    cout<<'\n';
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    case1();
    return 0;
}