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

typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
const ll LINF = 1e18;
const int INF = 1e9;

class DisjointSet {
public:
    vi parent;
    vi set_size;
    vi cnt;
    vi mapping;
    int size;

    DisjointSet(int n, int mapping_size) : parent(n), set_size(n, 1), cnt(n, 0), mapping(mapping_size, 0), size(mapping_size) {
        for (int i = 0; i < n; ++i) {
            parent[i] = i;
        }

        for (int i = 0; i < mapping_size; ++i) {
            mapping[i] = i;
        }
    }

    void merge(int a, int b) {
        a = find(a);
        b = find(b);
        if (a == b) {
            ++cnt[a];
            return;
        }

        if (set_size[a] < set_size[b]) {
            swap(a, b);
        }
        parent[b] = a;
        set_size[a] += set_size[b];
        cnt[a] += cnt[b] + 1;
    }

    int find_inner(int a) {
        if (parent[a] == a) {
            return a;
        }
        parent[a] = find_inner(parent[a]);
        return parent[a];
    }

    int find(int a) {
        return find_inner(mapping[a]);
    }

    void remove(int a) {
        int repr = find(a);
        --cnt[repr];
        --set_size[repr];
        mapping[a] = size++;
    }

    // void clear() {
        // for (size_t i = 0; i < parent.size(); ++i) {
        //     parent[i] = i;
        //     set_size[i] = 1;
        // }
    // }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

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

    DisjointSet ds(n + q + 2, n + 1);
    string s_out;

    char type;
    int a, b, c, d;
    for (int i = 0; i < q; ++i) {
        cin >> type;
        if (type == '+') {
            cin >> a >> b;
            ds.merge(a, b);
        } else if (type == '-') {
            cin >> c;
            ds.remove(c);
        } else {
            cin >> d;
            int repr = ds.find(d);

            if (ds.cnt[repr] == ds.set_size[repr]) {
                s_out += '1';
            } else if (ds.cnt[repr] == 0) {
                s_out += '0';
            } else {
                s_out += '?';
            }
        }
    }

    cout << s_out << endl;

    return 0;
}