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
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <stack>
#include <string>
#include <cstring>
#include <cassert>
#include <limits.h>

using namespace std;

typedef long long LL;
typedef long double LD;

typedef vector<int> VI;
typedef pair<int,int> PII;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(VAR(i,a);i<=(b);++i)
#define FORD(i,a,b) for(VAR(i,a);i>=(b);--i)
#define FORE(a,b) for(VAR(a,(b).begin());a!=(b).end();++a)
#define VAR(a,b) __typeof(b) a=(b)
#define SIZE(a) ((int)((a).size()))
#define ALL(x) (x).begin(),(x).end()
#define CLR(x,a) memset(x,a,sizeof(x))
#define ZERO(x) memset(x,0,sizeof(x))
#define S(t,i) scanf("%" ## t, &i)
#define SI(i) scanf("%d", &i)

const int INF=INT_MAX;
const int MAXN=1300000+5;

int n, q;
int parent[MAXN];
int set_rank[MAXN];
list<int> set_vertices[MAXN];
vector<int> find_parents;
char knowledge[MAXN];
int nodes_map[MAXN];
int sizes[MAXN];

int find_set(int v) {
    find_parents.clear();
    while (v != parent[v]) {
        find_parents.push_back(v);
        v = parent[v];
    }
    FORE(p, find_parents) {
        parent[*p] = v;
    }
    return v;
}

void make_set(int v) {
    parent[v] = v;
    set_rank[v] = 0;
    set_vertices[v].push_back(v);
    knowledge[v] = '0';
    sizes[v] = 1;
}

void union_sets(int a, int b) {
    if (set_rank[a] < set_rank[b]) {
            swap(a, b);
    }
    parent[b] = a;
    if (set_rank[a] == set_rank[b]) {
            set_rank[a]++;
    }

    set_vertices[a].splice(set_vertices[a].begin(), set_vertices[b]);
    sizes[a] += sizes[b];
}

void clear_set(int sa, char kv) {
    FORE(sv, set_vertices[sa]) {
        knowledge[*sv] = kv;
        parent[*sv] = *sv;
        set_rank[*sv] = 0;
        if (*sv != sa) {
            set_vertices[*sv].push_back(*sv);
        }
        sizes[*sv] = 1;
    }
    set_vertices[sa].clear();
    set_vertices[sa].push_back(sa);
}

int main() {
    ios_base::sync_with_stdio(0);
    find_parents.reserve(MAXN);

    SI(n);
    FOR(i, 1, n) {
        make_set(i);
        nodes_map[i] = i;
    }

    SI(q);
    REP(i, q) {
        char c;
        scanf(" %c", &c);
        if (c=='+') {
            int a, b;
            SI(a);
            SI(b);
            a = nodes_map[a];
            b = nodes_map[b];

            if (a==b) {
                clear_set(find_set(a), '1');
            } else if ((knowledge[a] == '0' || knowledge[a] == '?') && (knowledge[b] == '0' || knowledge[b] == '?')) {
                knowledge[a] = '?';
                knowledge[b] = '?';
                int sa = find_set(a);
                int sb = find_set(b);
                if (sa == sb) {
                    clear_set(sa, '1');
                } else {
                    union_sets(sa, sb);
                }
            } else if ((knowledge[a] == '0' || knowledge[a] == '?') && (knowledge[b] == '1')) {
                clear_set(find_set(a), '1');
            } else if ((knowledge[b] == '0' || knowledge[b] == '?') && (knowledge[a] == '1')) {
                clear_set(find_set(b), '1');
            }
        } else if (c=='-') {
            int c;
            SI(c);
            int a = nodes_map[c];
            int sa = find_set(a);
            sizes[sa]--;
            if (sizes[sa] == 1) {
                clear_set(sa, '0');
            }
            nodes_map[c] = ++n;
            c = nodes_map[c];

            make_set(c);
        } else if (c=='?') {
            int d;
            SI(d);
            d = nodes_map[d];
            printf("%c", knowledge[d]);
        }

    }
    return 0;
}