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
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

int newColor = 1;

map<int, pair<vector<int>, bool>> colormap;
vector<int> nodes;
inline void append(vector<int>& v1, vector<int>& v2, int color)
{
    for (auto& it : v2)
    {
        nodes[it] = color;
        v1.push_back(it);
    }
    v2 = vector<int>();
}
inline void connect(int n, int m)
{
    if (n == m)
    {
        if (nodes[m] == 0)
        {
            nodes[m] = newColor;
            ++newColor;
        }
        colormap[nodes[m]].second = true;
        return;
    }

    if (nodes[n] == 0 || nodes[m] == 0)
    {
        if (nodes[m] == 0 && nodes[n] == 0)
        {
            nodes[m] = newColor;
            nodes[n] = newColor;
            colormap[newColor].first = { m, n };
            ++newColor;
            return;
        }
        else if (nodes[n] == 0)
        {
            nodes[n] = nodes[m];
            colormap[nodes[m]].first.push_back(n);
            return;
        }
        nodes[m] = nodes[n];
        colormap[nodes[n]].first.push_back(m);
        return;
    }
    //wyzej ok
    else if (nodes[n] != nodes[m])
    {
        //capture
        pair<vector<int>, bool>& pr = colormap[nodes[n]];
        pair<vector<int>, bool>& pl = colormap[nodes[m]];
        if (pr.first.size() < pl.first.size())
        {
            append(pl.first, pr.first, nodes[m]);
            if (pr.second)
                pl.second = true;
            return;
        }
        else
        {
            append(pr.first, pl.first, nodes[n]);
            if (pl.second)
                pr.second = true;
            return;
        }
    }
    else
    {
        colormap[nodes[m]].second = true;
    }
}




int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    int m, q; cin >> m >> q;
    nodes = vector<int>(m + 1, 0);

    colormap[0].second = false;
    char command;
    int arg1, arg2;

    for (int i = 0; i < q; ++i)
    {
        cin >> command;
        switch (command)
        {
        case '+':
        {
            cin >> arg1 >> arg2;
            connect(arg1, arg2);
            continue;
        }
        case '-':
        {
            cin >> arg1;
            pair<vector<int>, bool>& colorpair = colormap[nodes[arg1]];
            colorpair.first.erase(std::remove(colorpair.first.begin(), colorpair.first.end(), arg1), colorpair.first.end());
            nodes[arg1] = 0;
            continue;
        }
        case '?':
        {
            cin >> arg1;
            if (nodes[arg1] == 0)
            {
                cout << "0";
                break;
            }
            pair<vector<int>, bool>& colorpair = colormap[nodes[arg1]];
            if (colorpair.second)
                cout << "1";
            else if (!colorpair.second && colorpair.first.size() == 1)
                cout << "0";
            else
                cout << "?";
            continue;
        }
        }
    }

}