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

using namespace std;

const size_t MAX_N = 300000;
const size_t MAX_Q = 1000000;

vector<size_t> master;
char state[MAX_N + 9];
size_t tmp[MAX_N + 9];
size_t timestamp;
vector<vector<size_t> > bases;
vector<vector<size_t> > removed_bases;

void reset(size_t a, char st) {
    state[a] = st;
    master[a] = master.size();
    master.push_back(master.size());
    bases.push_back({a});
    removed_bases.push_back({});
}

void set_master(size_t x, size_t y) {
    master[x] = y;
    while (bases[x].size()) {
        bases[y].push_back(bases[x].back());
        bases[x].pop_back();
    }
    while (removed_bases[x].size()) {
        removed_bases[y].push_back(removed_bases[x].back());
        removed_bases[x].pop_back();
    }
}

void push_master(size_t x) {
    if (master[master[x]] != master[x]) {
        push_master(master[x]);
        size_t y = master[master[x]];
        master[x] = x;
        if (bases[x].size() <= bases[y].size()) {
            set_master(x, y);
        }
        else {
            set_master(y, x);
        }
    }
}

size_t get_master(size_t x) {
    push_master(x);
    return master[x];
}

void set_tree_to_1(size_t x) {
    timestamp++;
    for (size_t v : removed_bases[x]) {
        tmp[v] = timestamp;
    }
    for (size_t v : bases[x]) {
        if (tmp[v] < timestamp) {
            reset(v, '1');
        }
    }
}

void connect(size_t a, size_t b) {
    size_t x = get_master(a);
    size_t y = get_master(b);
    
    if (state[a] == '1') {
        set_tree_to_1(y);
        return;
    }
    if (state[b] == '1') {
        set_tree_to_1(x);
        return;
    }
    state[a] = '?';
    state[b] = '?';
    
    if (x != y) {
        if (bases[x].size() <= bases[y].size()) {
            set_master(x, y);
        }
        else {
            set_master(y, x);
        }
        return;
    }
    else {
        set_tree_to_1(x);
    }
}

void erase(size_t a) {
    if (state[a] == '1') {
        state[a] = '0';
        return;
    }
    if (state[a] == '0') {
        return;
    }

    size_t x = get_master(a);
    removed_bases[x].push_back(a);
    reset(a, '0');
}

int main() {
    unsigned int n,q;
    scanf("%u%u", &n, &q);
    master.resize(n+1);
    bases.resize(n+1);
    removed_bases.resize(n+1);
    for (size_t i = 1; i <= n; ++i) {
        reset(i, '0');
    }

    for (size_t qi = 0; qi < q; ++qi) {
        char c;
        scanf(" %c", &c);
        switch (c) {
            case '+': {
                unsigned int a,b;
                scanf("%u%u", &a, &b);
                connect(a, b);
                break;
            }
            case '-': {
                unsigned int a;
                scanf("%u", &a);
                erase(a);
                break;
            }
            case '?': {
                unsigned int a;
                scanf("%u", &a);
                printf("%c", state[a]);
                break;
            }
        }
    }
    return 0;
}