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
#include <vector>
#include <iostream>

enum State : int {
    no_computer,
    has_computer,
    maybe_computer
};

State state_reverse(State item) {
    if (item == has_computer) {
        return no_computer;
    } else if (item == no_computer) {
        return has_computer;
    } else {
        return maybe_computer;
    }
}

int fu_id = 0;
std::vector<int> fu_fathers(1);
std::vector<int> fu_cnt(1);
std::vector<State> fu_state(1);

std::vector<int> ids;

int get_unique_fu_id(State s) {
    int id = fu_fathers.size();

    fu_fathers.push_back(id);
    fu_cnt.push_back(1);
    fu_state.push_back(s);

    return id;
}


int fu_find(int i) {
    if (fu_fathers[i] == i) 
        return i;
    fu_fathers[i] = fu_find(fu_fathers[i]);
    return fu_fathers[i];
}

void fu_union(int i, int j) {
    int father1 = fu_find(i);
    int father2 = fu_find(j);

    fu_fathers[father1] = father2;
    fu_cnt[father2] += fu_cnt[father1];
}

void fu_update(int i) {
    int leader = fu_find(ids[i]);
    if (fu_cnt[leader] != 1 && fu_state[leader] != maybe_computer) {
        fu_cnt[leader]--;
        ids[i] = get_unique_fu_id(fu_state[leader]);
    }
}

void handle_p() {
    int a,b;
    std::cin >> a >> b;
    fu_update(a);
    fu_update(b);

    int leader_a = fu_find(ids[a]);
    int leader_b = fu_find(ids[b]);

    State s1 = fu_state[leader_a];
    State s2 = fu_state[leader_b];

    if (a == b) {
       fu_state[leader_a] = has_computer;
    } else if (s1 == has_computer) {
        fu_state[leader_b] = has_computer;
    } else if (s2 == has_computer) {
        fu_state[leader_a] = has_computer;
    } else if (s1 == maybe_computer && s2 == maybe_computer) {
        if (leader_a == leader_b) {
            fu_state[leader_a] = has_computer;
        } else {
            fu_union(leader_a, leader_b);
        }
    } else {
        fu_union(leader_a, leader_b);
        fu_state[leader_a] = maybe_computer;
        fu_state[leader_b] = maybe_computer;
    }
}

void handle_m() {
    int c;
    std::cin >> c;

    fu_update(c);

    int leader = fu_find(ids[c]);
    ids[c] = get_unique_fu_id(no_computer);
    fu_cnt[leader]--;

    if (fu_cnt[leader] == 1)
        fu_state[leader] = no_computer;
}

void handle_q() {
    int d;
    std::cin >> d;
    fu_update(d);
    switch(fu_state[fu_find(ids[d])]) {
    case no_computer:
        std::cout << '0';
        break;
    case has_computer:
        std::cout << '1';
        break;
    case maybe_computer:
        std::cout << '?';
        break;
    }
}

int main() {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    int n, q;
    std::cin >> n >> q;

    ids = std::vector<int>(n + 1);

    for (int i = 0; i <= ids.size(); i++)
        ids[i] = get_unique_fu_id(no_computer);

    while (q--) {
        char c;
        std::cin >> c;
        switch(c) {
        case '+':
            handle_p();
            break;
        case '-':
            handle_m();
            break;
        case '?':
            handle_q();
            break;
        }
    }
}