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

using namespace std;

int find(int w, vector<int> &R)
{
    if(w != R[w])
    {
        R[w] = find(R[w], R);
        return R[w];
    }
    return w;
}
void un(int w, int w2, vector<int> &R, vector<int> &G, vector<int> &W, vector<int>&S)
{
    int x1 = find(w, R);
    int x2 = find(w2, R);

    if(x1 != x2)
    {
        if(G[x1] >= G[x2])
        {
            R[x2] = x1;
            G[x1] = max(G[x1], G[x2] + 1);
            W[x1] += W[x2];
            if(S[x2] == 1)
            {
                S[x1] = 1;
            }
            if(S[x1] == 0)
            {
                S[x1] = 2;
            }
        }
        else
        {
            R[x1] = x2;
            G[x2] = max(G[x2], G[x1] + 1);
            W[x2] += W[x1];
            if(S[x1] == 1)
            {
                S[x2] = 1;
            }
            if(S[x2] == 0)
            {
                S[x2] = 2;
            }
        }
    }
    else
    {
        S[x1] = 1;
    }
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    int q;
    cin >> n;
    cin >> q;

    char a;
    int x;
    int x2;

    vector<int> G(2 * n + 1, 1); // glebokosc
    vector<int> W(2 * n + 1, 1); // wielkosc
    vector<int> S(2 * n + 1, 0); // 0 - nie ma, 1 - ma, 2 - ?
    vector<int> R(2 * n + 1, 0); // rodzic
    int licznik_wierzcholkow = 2 * n + 1;
    for(int i = 0; i <= n; i++)
    {
        R[i] = i + n;
    }
    for(int i = n + 1; i <= 2 * n; i++)
    {
        R[i] = i;
    }
    int korzen;
    for(int p = 0; p < q; p++)
    {
        cin >> a;
        cin >> x;
        korzen = find(x, R);
        if(a == '+')
        {
            cin >> x2;
            un(x, x2, R, G, W, S);
        }
        
        if(a == '?')
        {
            int stan = S[korzen];
            if(stan == 0)
            {
                cout<<"0";
            }
            if(stan == 1)
            {
                cout<<"1";
            }
            if(stan == 2)
            {
                cout<<"?";
            }
        }
        if(a == '-')
        {
            R[x] = licznik_wierzcholkow;
            R.push_back(licznik_wierzcholkow);
            S.push_back(0);
            G.push_back(1);
            W.push_back(1);
            licznik_wierzcholkow++;          
            W[korzen]--;
          //  cout<<endl<<W[korzen]<<" "<<S[korzen]<<endl;
            if(W[korzen] == 1 && S[korzen] == 2)
            {
                S[korzen] = 0;
            }
        }
    }
    
    return 0;
}