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
#include <bits/stdc++.h>
using namespace std;
int n, q, a, b, cnt;
char c;
int rep[300005];
bool has[1300005];
int sajz[1300005];
queue <int> sas[300005];
int f(int x)
{
    if (rep[x] == x) return x;
    rep[x] = f(rep[x]);
    return rep[x];
}
void u(int x, int y)
{
    sajz[f(y)] += sajz[f(x)];
    rep[f(x)] = rep[f(y)];
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> q;
    cnt = n + 1;
    for (int i = 1; i <= n; ++i) rep[i] = i, sajz[i] = 1;
    for (int i = n + 1; i <= q + 1; ++i) rep[i] = i;
    while (q--)
    {
        cin >> c;
        if (c == '+')
        {
            cin >> a >> b;
            if (f(a) == f(b))
            {
                has[f(a)] = true;
                continue;
            }
            if (sajz[f(a)] == 1 && sajz[f(b)] == 1)
            {
                u(a, cnt);
                u(b, cnt);
                ++cnt;
            }
            else
            {
                if (sajz[f(a)] == 1)
                {
                    u(a, b);
                }
                else
                {
                    u(b, a);
                }
            }
            sas[a].push(b);
            sas[b].push(a);
        }
        if (c == '?')
        {
            cin >> a;
            if (has[f(a)])
            {
                cout << "1";
            }
            else if (sajz[f(a)] > 1)
            {
                cout << "?";
            }
            else
            {
                cout << "0";
            }
        }
        if (c == '-')
        {
            cin >> a;
            while (!sas[a].empty())
            {
                if (f(sas[a].front()) != f(a))
                {
                    sas[a].pop();
                    continue;
                }
                else
                {
                    rep[f(sas[a].front())] = f(a);
                    sas[a].pop();
                }
            }
            --sajz[f(rep[a])];
            rep[a] = a;
        }
    }
}