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

#define FOR(i,s,e) for(int i=(s); i<=(e); i++)
#define FORD(i,s,e) for(int i=(s); i>=(e); i--)
#define ALL(k) (k).begin(), (k).end()
#define e1 first
#define e2 second
#define mp make_pair
 
using namespace std;
using LL=long long;
using LD=long double;
using PII=pair<int,int>;

const int MAXN = 1000111;

int union_rep[MAXN];
vector<int> union_list[MAXN];
int union_ones_count[MAXN];

int find(int a){
    int a0 = a;
    if (union_rep[a] < 0){
        return a;
    }
    union_rep[a] = find(union_rep[a]);
    return union_rep[a];
}

bool are_joined(int a, int b){
    int aa = find(a), bb = find(b);
    return aa == bb;
}

bool join(int a, int b){
    int aa = find(a), bb = find(b);
    if (aa == bb){
        return false;
    }
    else{
        if (union_rep[aa] > union_rep[bb]){
            swap(aa, bb);
        }
        union_rep[aa] += union_rep[bb];
        union_rep[bb] = aa;
        for (auto& elem : union_list[bb]){
            union_list[aa].push_back(elem);
        }
        union_ones_count[aa] += union_ones_count[bb];
        union_ones_count[bb] = 0;
        return true;
    }
}

int node_state[MAXN];
int latest_node_representative[MAXN];

char input_line[111];
char res[MAXN];


main(){
    fgets(input_line, 110, stdin);
    int n, q; sscanf(input_line, "%d%d",&n, &q);
    int next_node_value = n+1;
    FOR(i,1,n){
        latest_node_representative[i] = i;
        union_rep[i] = -1;
        union_list[i].push_back(i);
    }
    int query_idx = 1;
    FOR(i,1,q){
        fgets(input_line, 110, stdin);
        // printf("Line %s\n", input_line);
        if (input_line[0] == '+'){
            int a0, b0;
            sscanf(input_line, "+ %d %d", &a0, &b0);
            int a = latest_node_representative[a0];
            int b = latest_node_representative[b0];
            if (a == b || are_joined(a, b)){
                int aa = find(a);
                for(auto v: union_list[aa]){
                    node_state[v] = 1;
                }
            }
            else{
                if (node_state[a] == 1) {
                    int bb = find(b);
                    for (auto &bv : union_list[bb]){
                        node_state[bv] = 1;
                    }
                    union_ones_count[bb] = union_list[bb].size();
                }
                else if (node_state[b] == 1) {
                    int aa = find(a);
                    for (auto &av : union_list[aa]) {
                        node_state[av] = 1;
                    }
                    union_ones_count[aa] = union_list[aa].size();
                }
                else {
                    node_state[a] = node_state[b] = -1;
                }

                join(a, b);
            }
        } 
        else if (input_line[0] == '-') {
            int c0; 
            sscanf(input_line, "- %d", &c0);
            int c = latest_node_representative[c0];

            if (node_state[c] != 1){
                node_state[c] = 1;
                int cc = find(c);
                union_ones_count[cc]++;
                if (union_ones_count[cc] + 1 == -union_rep[cc]){
                    for (auto &it: union_list[cc]){
                        if (node_state[it] != 1){
                            node_state[it] = 0;
                        }
                    }
                }
            }
            latest_node_representative[c0] = next_node_value;
            union_rep[next_node_value] = -1;
            union_list[next_node_value].push_back(next_node_value);
            next_node_value++;
        }
        else if (input_line[0] == '?') {
            int d0;
            sscanf(input_line, "? %d", &d0);
            int d = latest_node_representative[d0];

            res[query_idx] = (node_state[d] == 1) ? '1' : ((node_state[d] == 0) ? '0' : '?');
            // printf("RES %c\n", res[query_idx]);
            query_idx++;
        }
        // printf("STATE OUTPUT for i=%d\n", i);
        // printf("Num: ");
        // FOR(i,1,n){
        //     printf("%2d ", i);
        // }
        // puts("");
        // printf("Rep: ");
        // FOR(i,1,n){
        //     printf("%2d ", latest_node_representative[i]);
        // }
        // printf("\nUnderlying nodes\n");
        // printf("Num: ");
        // FOR(i,1, next_node_value-1){
        //     printf("%2d ", i);
        // }
        // printf("\nStt: ");
        // FOR(i,1,next_node_value-1){
        //     printf("%2d ", node_state[i]);
        // }
        // printf("\nGrp: ");
        // FOR(i,1,next_node_value-1){
        //     printf("%2d ", find(i));
        // }
        // printf("\nRep: ");
        // FOR(i,1,next_node_value-1){
        //     printf("%2d ", union_rep[i]);
        // }
        // puts("");

    }
    printf("%s\n", res+1);

}