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
#include <bits/stdc++.h>

using namespace std;

struct Child {
    int v, time;
};

struct Node {
    int rank = 0;
    int parent = -1;
    int time = -1;
    vector<Child> children;
    int current = 0;
};

vector<int> status;

struct DSU {
    vector<Node> graph;
    vector<int> last;
    int time = 0;

    explicit DSU(int size) {
        graph.resize(size);
        last.resize(size, -1);
    }

    int find(int v) {
        if (graph[v].time < last[graph[v].parent]) {
            graph[v].parent = -1;
            graph[v].time = -1;
        }
        if (graph[v].parent == -1)
            return v;

        return find(graph[v].parent);
    }

    void onion(int v, int w) {
        int vRoot = find(v);
        int wRoot = find(w);

        if (graph[vRoot].rank > graph[wRoot].rank) {
            graph[wRoot].parent = vRoot;
            graph[wRoot].time = time;
            graph[vRoot].children.push_back({wRoot, time});
        } else if (graph[wRoot].rank > graph[vRoot].rank) {
            graph[vRoot].parent = wRoot;
            graph[vRoot].time = time;
            graph[wRoot].children.push_back({vRoot, time});
        } else {
            graph[vRoot].parent = wRoot;
            graph[vRoot].time = time;
            graph[wRoot].children.push_back({vRoot, time});
            graph[wRoot].rank++;
        }
        time++;
    }

    void resetNode(int v) {
        vector<int> toOnion;
        toOnion.reserve(graph[v].children.size());
        for (auto &child : graph[v].children) {
            if (child.time > last[child.v])
                toOnion.push_back(child.v);
        }
        int vRoot = find(v);
        if (vRoot != v)
            toOnion.push_back(vRoot);

        graph[v].rank = 0;
        graph[v].parent = -1;
        graph[v].time = -1;
        graph[v].children.clear();
        graph[v].current = 0;
        last[v] = time;
        time++;

        if (toOnion.empty())
            return;

        int root = toOnion[0];
        for (int w : toOnion)
            if (graph[w].rank > graph[root].rank)
                root = w;

        for (int w : toOnion)
            if (w != root)
                onion(root, w);
    }

    bool isSingle(int v) {
        find(v);
        if (graph[v].parent != -1)
            return false;

        for ( ; graph[v].current < graph[v].children.size(); graph[v].current++) {
            auto &child = graph[v].children[graph[v].current];
            if (child.time > last[child.v])
                return false;
        }

        graph[v].children.clear();
        graph[v].current = 0;
        return true;
    }

    void resetTree(int root) {
        status[root] = 1;
        graph[root].rank = 0;
        graph[root].parent = -1;
        graph[root].time = -1;
        for (auto &child : graph[root].children)
            if (child.time > last[child.v])
                resetTree(child.v);
        graph[root].children.clear();
        graph[root].current = 0;
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int n, q;
    cin >> n >> q;

    DSU dsu(n);
    status.assign(n, 0);
    while (q--) {
        char type;
        cin >> type;

        if (type == '+') {
            int i, j;
            cin >> i >> j;
            i--, j--;

            if (status[i] == 1) {
                int jRoot = dsu.find(j);
                dsu.resetTree(jRoot);
            } else if (status[j] == 1) {
                int iRoot = dsu.find(i);
                dsu.resetTree(iRoot);
            } else {
                status[i] = status[j] = -1;

                int iRoot = dsu.find(i);
                int jRoot = dsu.find(j);

                if (iRoot == jRoot)
                    dsu.resetTree(iRoot);
                else
                    dsu.onion(iRoot, jRoot);
            }
        } else if (type == '-') {
            int i;
            cin >> i;
            i--;

            if (status[i] == -1) {
                dsu.resetNode(i);
            }
            status[i] = 0;
        } else if (type == '?') {
            int i;
            cin >> i;
            i--;

            if (dsu.isSingle(i) and status[i] == -1)
                status[i] = 0;

            cout << (status[i] != -1 ? to_string(status[i]) : "?");
        }
    }
    cout << "\n";

    return 0;
}