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

using namespace std;

struct disjoint_sets {
    vector<int> parent;
    vector<int> size;
    vector<int> next;
    vector<int> prev;
    disjoint_sets(int n) : parent(n + 1), size(n + 1), next(n + 1), prev(n + 1) {}
};

void make_set(disjoint_sets& ds, int a) {
    ds.parent[a] = a;
    ds.size[a] = 1;
    ds.next[a] = a;
    ds.prev[a] = a;
}

int find_set(disjoint_sets& ds, int a) {
    if (a == ds.parent[a])
        return a;
    return ds.parent[a] = find_set(ds, ds.parent[a]);
}

void union_sets(disjoint_sets& ds, int a, int b) {
    a = find_set(ds, a);
    b = find_set(ds, b);
    if (a != b) {
        if (ds.size[a] < ds.size[b])
            swap(a, b);
        ds.parent[b] = a;
        ds.size[a] += ds.size[b];
        swap(ds.next[a], ds.next[b]);
        swap(ds.prev[ds.next[a]], ds.prev[ds.next[b]]);
    }
}

bool is_set(disjoint_sets& ds, int a) {
    return ds.parent[a] > 0;
}

bool is_singleton(disjoint_sets& ds, int a) {
    return is_set(ds, a) && ds.size[find_set(ds, a)] == 1;
}

void delete_set(disjoint_sets& ds, int a) {
    if (!is_set(ds, a))
        return;
    int b = a;
    do {
        int next = ds.next[b];
        ds.parent[b] = 0;
        ds.size[b] = 0;
        ds.next[b] = 0;
        ds.prev[b] = 0;
        b = next;
    } while (b != a);
}

void add_computer(disjoint_sets& ds, int a, int b) {
    // a or b doesn't have computer, i.e., a or b is a set.
    if (!is_set(ds, a)) {
        delete_set(ds, b);
        return;
    }
    if (!is_set(ds, b)) {
        delete_set(ds, a);
        return;
    }
    a = find_set(ds, a);
    b = find_set(ds, b);
    if (a == b) {
        delete_set(ds, a);
        return;
    }
    if (a != b) {
        union_sets(ds, a, b);
        return;
    }
}

void destroy_computer(disjoint_sets& ds, int a) {
    if (!is_singleton(ds, a)) {
        ds.next[ds.prev[a]] = ds.next[a];
        ds.prev[ds.next[a]] = ds.prev[a];
        int parent = find_set(ds, a);
        --ds.size[parent];
        if (parent == a) {
            parent = ds.next[a];
            ds.size[parent] = ds.size[a];
        }
        int b = ds.next[a];
        do {
            int next = ds.next[b];
            ds.parent[b] = parent;
            b = next;
        } while (b != ds.next[a]);
    }
    make_set(ds, a);
}

char has_computer(disjoint_sets& ds, int a) {
    if (!is_set(ds, a)) {
        return '1';
    } else if (ds.size[find_set(ds, a)] == 1) {
        return '0';
    } else {
        return '?';
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    int n, q;
    cin >> n >> q;
    disjoint_sets ds(n);
    for (int i = 1; i <= n; ++i)
        make_set(ds, i);
    for (int i = 0; i < q; ++i) {
        char event;
        cin >> event;
        if (event == '+') {
            int a, b;
            cin >> a >> b;
            add_computer(ds, a, b);
        } else if (event == '-') {
            int c;
            cin >> c;
            destroy_computer(ds, c);
        } else if (event == '?') {
            int d;
            cin >> d;
            cout << has_computer(ds, d);
        }
    }
    cout << "\n";
    return 0;
}