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
196
197
198
199
200
201
202
203
204
205
206
207
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 2137
#endif

// https://github.com/kth-competitive-programming/kactl/blob/main/content/graph/LinkCutTree.h

struct Node { // Splay tree. Root's pp contains tree's parent.
	Node *p = 0, *pp = 0, *c[2];
	bool flip = 0;
	Node() { c[0] = c[1] = 0; fix(); }
	void fix() {
		if (c[0]) c[0]->p = this;
		if (c[1]) c[1]->p = this;
		// (+ update sum of subtree elements etc. if wanted)
	}
	void pushFlip() {
		if (!flip) return;
		flip = 0; swap(c[0], c[1]);
		if (c[0]) c[0]->flip ^= 1;
		if (c[1]) c[1]->flip ^= 1;
	}
	int up() { return p ? p->c[1] == this : -1; }
	void rot(int i, int b) {
		int h = i ^ b;
		Node *x = c[i], *y = b == 2 ? x : x->c[h], *z = b ? y : x;
		if ((y->p = p)) p->c[up()] = y;
		c[i] = z->c[i ^ 1];
		if (b < 2) {
			x->c[h] = y->c[h ^ 1];
			y->c[h ^ 1] = x;
		}
		z->c[i ^ 1] = this;
		fix(); x->fix(); y->fix();
		if (p) p->fix();
		swap(pp, y->pp);
	}
	void splay() { /// Splay this up to the root. Always finishes without flip set.
		for (pushFlip(); p; ) {
			if (p->p) p->p->pushFlip();
			p->pushFlip(); pushFlip();
			int c1 = up(), c2 = p->up();
			if (c2 == -1) p->rot(c1, 2);
			else p->p->rot(c2, c1 != c2);
		}
	}
	Node* first() { /// Return the min element of the subtree rooted at this, splayed to the top.
		pushFlip();
		return c[0] ? c[0]->first() : (splay(), this);
	}
};

struct LinkCut {
	vector<Node> node;
	LinkCut(int N) : node(N) {}

	void link(int u, int v) { // add an edge (u, v)
		assert(!connected(u, v));
		makeRoot(&node[u]);
		node[u].pp = &node[v];
	}
	void cut(int u, int v) { // remove an edge (u, v)
		Node *x = &node[u], *top = &node[v];
		makeRoot(top); x->splay();
		assert(top == (x->pp ?: x->c[0]));
		if (x->pp) x->pp = 0;
		else {
			x->c[0] = top->p = 0;
			x->fix();
		}
	}
	bool connected(int u, int v) { // are u, v in the same tree?
		Node* nu = access(&node[u])->first();
		return nu == access(&node[v])->first();
	}
	void makeRoot(Node* u) { /// Move u to root of represented tree.
		access(u);
		u->splay();
		if(u->c[0]) {
			u->c[0]->p = 0;
			u->c[0]->flip ^= 1;
			u->c[0]->pp = u;
			u->c[0] = 0;
			u->fix();
		}
	}
	Node* access(Node* u) { /// Move u to root aux tree. Return the root of the root aux tree.
		u->splay();
		while (Node* pp = u->pp) {
			pp->splay(); u->pp = 0;
			if (pp->c[1]) {
				pp->c[1]->p = 0; pp->c[1]->pp = pp; }
			pp->c[1] = u; pp->fix(); u = pp;
		}
		return u;
	}
};

const int N = 300300;
int n, q;
set<int> adj[N];
LinkCut lct(0);
char typ[N];

vector<int> id;
void dfs(int u, int p) {
  id.push_back(u);
  for (int v : adj[u]) {
    if (v != p) {
      dfs(v, u);
    }
  }
}

void napraw(int a) {
  // iter po ludziach w tym
  // ustawiamy im 1, usuwamy wszystkie kraw
  // zeby uzyskac numer noda wystarczy odjac lct.node.begin() bo to tablica
  id.clear();
  dfs(a, -1);
  for (int i : id) {
    for (int j : adj[i]) {
      adj[j].erase(i);
      lct.cut(i, j);
    }
    adj[i].clear();
    typ[i] = '1';
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> q;
  lct = LinkCut(n);
  for (int i = 0; i < n; i++) {
    typ[i] = '0';
  }
  string ans;
  ans.reserve(q);
  for (int it = 0; it < q; it++) {
    char c;
    cin >> c;
    if (c == '+') {
      int a, b;
      cin >> a >> b;
      a--; b--;
      if (lct.connected(a, b)) {
        napraw(a);
      } else {
        if (typ[a] == '1') {
          napraw(b);
        } else if (typ[b] == '1') {
          napraw(a);
        } else {
          lct.link(a, b);
          adj[a].insert(b);
          adj[b].insert(a);
          typ[a] = typ[b] = '?';
        }
      }
    } else if (c == '-') {
      int a;
      cin >> a;
      a--;
      if (ssize(adj[a]) > 1) {
        // nie lisc
        for (int b : adj[a]) {
          adj[b].erase(a);
          lct.cut(a, b);
        }
        int bb = *adj[a].begin();
        for (int b : adj[a]) {
          if (b != bb) {
            lct.link(b, bb);
            adj[b].insert(bb);
            adj[bb].insert(b);
          }
        }
        adj[a].clear();
        typ[a] = '0';
      } else if (ssize(adj[a]) == 1) {
        // lisc
        typ[a] = '0';
        int b = *adj[a].begin();
        if (ssize(adj[b]) == 1) {
          typ[b] = '0';
        }
        adj[a].clear();
        adj[b].erase(a);
        lct.cut(a, b);
      } else {
        typ[a] = '0';
      }
    } else {
      int a;
      cin >> a;
      ans += typ[a - 1];
    }
  }
  cout << ans << '\n';
}