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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* -----------------------
Autor: Tomasz Boguslawski
-------------------------- */
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<sstream>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include <fstream>
#include<math.h>

#define LL long long
#define FOR(x, b, e) for(LL x = b; x <= (e); x++)
#define FORS(x, b, e, s) for(LL x = b; x <= (e); x+=s)
#define FORD(x, b, e) for(LL x = b; x >= (e); x--)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define MIN(a,b) ((a>b)?b:a)
#define MAX(a,b) ((a>b)?a:b)

using namespace std;

class Node
{
    public:
    Node() { state=2; treeSize=1; realSize=1; parent=this; }
    int state; // 0: unknown, 1-full, 2-empty
    LL treeSize; // only valid for roots - size of the tree
    LL realSize; // only valid for roots - number of valid nodes in the tree
    Node* parent;
};

Node* nodes[301000];
int n, q;

Node* findRootFor(LL k)
{
    Node* node = nodes[k];
    while (node->parent!=node) node=node->parent;
    Node* root = node;
    node = nodes[k];
    // compress path:
    Node* next;
    while (node->parent!=node)
    {
        next = node->parent;
        node->parent = root;
        node = next;
    }
    return root;
}

void mergeSets(Node* r1, Node* r2)
{
    if (r1->treeSize<r2->treeSize) { Node* pom=r1; r1=r2; r2=pom; }
    // r1 is a root of the bigger tree
    r2->parent=r1;
    r1->treeSize+=r2->treeSize;
    r1->realSize+=r2->realSize;
}


void handleNewComp(LL k1, LL k2)
{
    Node* r1 = findRootFor(k1);
    Node* r2 = findRootFor(k2);
    if (r1->state==0)
    {
        if (r2->state==0)
        {
            if (r1==r2) r1->state=1;
            else mergeSets(r1,r2);
        }
        else if (r2->state==1)
        {
            r1->state=1;
        }
        else // r2->state==2
        {
            r2->state=0;
            mergeSets(r1,r2);
        }
    }
    else if (r1->state==1)
    {
        if (r2->state==0)
        {
            r2->state=1;
        }
        else if (r2->state==1)
        {
            // impossible
        }
        else // r2->state==2
        {
            r2->state=1;
        }
    }
    else // r1->state==2
    {
        if (r2->state==0)
        {
            r1->state=0;
            mergeSets(r1,r2);
        }
        else if (r2->state==1)
        {
            r1->state=1;
        }
        else // r2->state==2
        {
            if (r1==r2) { r1->state=1; }
            else
            {
                r1->state=0; r2->state=0;
                mergeSets(r1,r2);
            }
        }
    }
}

void handleCheck(LL k)
{
    Node* r = findRootFor(k);
    if (r->state==0) cout << '?';
    else if (r->state==1) cout << '1';
    else cout << '0';
}

void handleRemove(LL k)
{
    Node* r = findRootFor(k);
    if (r->state==0)
    {
        nodes[k]=new Node();
        r->realSize--;
        if (r->realSize==1) r->state=2;
    }
    else if (r->state==1)
    {
        nodes[k]=new Node();
        r->realSize--;
    }
    else // r->state==2
    {
        // cout << "impossible remove from state 2\n";
    }
}

void readData()
{
    cin >> n;
    cin >> q;
    FOR(i,1,n) nodes[i]=new Node();
    char ch; LL k1,k2;
    FOR(i,1,q)
    {
        cin >> ch;
        while(ch!='+'&&ch!='-'&&ch!='?') cin >> ch;
        if (ch=='+')
        {
            cin >> k1; cin >> k2;
            handleNewComp(k1,k2);
        }
        else if (ch=='?')
        {
            cin >> k1;
            handleCheck(k1);
        }
        else if (ch=='-')
        {
            cin >> k1;
            handleRemove(k1);
        }
    }
}

/// MAIN
int main(int argc, char* argv[])
{
    // magic formula, which makes streams work faster:
	ios_base::sync_with_stdio(0);
	readData();
	cout << "\n";
	return 0;
}