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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <bits/stdc++.h>

using namespace std;


enum State {
    OPEN = 0,
    EVERYONE = 1,
    NONE = 2
};


struct Set {
    State state;
    int pointer;
    int id;
    int members;
};


Set get_parent(Set set, vector<Set>& sets) {
    if (set.pointer == -1) {
        return set;
    } else {
        set.pointer = get_parent(sets[set.pointer], sets).id;
        return sets[set.pointer];
    }
}


int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);

    int n, q;

    cin >> n >> q;

    vector<char> states = vector<char>(n, '0');
    map<int, int> id_to_set;
    vector<Set> sets;

    for (int i = 0; i < q; i++) {
        string event;
        cin >> event;

        if (event == "+") {
            int a, b;
            cin >> a >> b;
            a--;
            b--;

            int a_set_id = -1, b_set_id = -1;

            if (id_to_set.find(a) != id_to_set.end()
                && id_to_set[a] != -1)
            {
                id_to_set[a] = get_parent(sets[id_to_set[a]], sets).id;
                if (sets[id_to_set[a]].state == NONE) {
                    states[a] = '0';
                    sets[id_to_set[a]].members--;
                    id_to_set[a] = -1;
                } else if (sets[id_to_set[a]].state == EVERYONE) {
                    states[a] = '1';
                    sets[id_to_set[a]].members--;
                    id_to_set[a] = -1;
                } else {
                    a_set_id = id_to_set[a];
                }
            } else {
                a_set_id = -1;
            }

            if (id_to_set.find(b) != id_to_set.end()
                && id_to_set[b] != -1)
            {
                id_to_set[b] = get_parent(sets[id_to_set[b]], sets).id;
                if (sets[id_to_set[b]].state == NONE) {
                    states[b] = '0';
                    sets[id_to_set[b]].members--;
                    id_to_set[b] = -1;
                } else if (sets[id_to_set[b]].state == EVERYONE) {
                    states[b] = '1';
                    sets[id_to_set[b]].members--;
                    id_to_set[b] = -1;
                } else {
                    b_set_id = id_to_set[b];
                }
            } else {
                b_set_id = -1;
            }


            if (a == b && a_set_id == -1) {
                states[a] = '1';
            } else if (states[a] == '1') {
                states[b] = '1';
                if (b_set_id != -1) {
                    sets[b_set_id].state = EVERYONE;
                    sets[b_set_id].members--;
                    id_to_set[b] = -1;
                }
            } else if (states[b] == '1') {
                states[a] = '1';
                if (a_set_id != -1) {
                    sets[a_set_id].state = EVERYONE;
                    sets[a_set_id].members--;
                    id_to_set[a] = -1;
                }
            } else if (a_set_id == -1 && b_set_id == -1) {
                sets.push_back({OPEN, -1, int(sets.size()), 2});
                id_to_set[a] = sets.size() - 1;
                id_to_set[b] = sets.size() - 1;
                states[a] = '?';
                states[b] = '?';
            } else if (a_set_id == -1) {
                id_to_set[a] = b_set_id;
                sets[b_set_id].members++;
                states[a] = '?';
            } else if (b_set_id == -1) {
                id_to_set[b] = a_set_id;
                sets[a_set_id].members++;
                states[b] = '?';
            } else if (a_set_id == b_set_id) {
                sets[a_set_id].state = EVERYONE;
                sets[a_set_id].members -= 2;
                states[a] = '1';
                states[b] = '1';
                id_to_set[a] = -1;
                id_to_set[b] = -1;
            } else {
                sets[b_set_id].pointer = a_set_id;
                sets[a_set_id].members += sets[b_set_id].members;
            }
        } else if (event == "-") {
            int x;
            cin >> x;
            x--;

            states[x] = '0';
            if (id_to_set.find(x) != id_to_set.end() && id_to_set[x] != -1) {
                id_to_set[x] = get_parent(sets[id_to_set[x]], sets).id;

                sets[id_to_set[x]].members--;
                if (sets[id_to_set[x]].members <= 1 and sets[id_to_set[x]].state == OPEN) {
                    sets[id_to_set[x]].state = NONE;
                }
                id_to_set[x] = -1;
            }
        } else if (event == "?") {
            int x;
            cin >> x;
            x--;
            if (states[x] == '1') {
                cout << "1";
            } else {
                int x_set_id;
                if (id_to_set.find(x) != id_to_set.end() && id_to_set[x] != -1) {
                    id_to_set[x] = get_parent(sets[id_to_set[x]], sets).id;
                    x_set_id = id_to_set[x];
                } else {
                    x_set_id = -1;
                }
                if (states[x] == '0') {
                    cout << "0";
                } else if (x_set_id == -1 or sets[x_set_id].state == NONE) {
                    cout << "0";
                } else if (sets[x_set_id].state == EVERYONE) {
                    cout << "1";
                } else {
                    cout << "?";
                }
            }
        }
    }
    cout << endl;
}