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

#define DEBUG(x) cout << #x << " = " << x << '\n';

using namespace std;

const int MAXQ = 1'000'000;
const int MAXN = 300'000;
const int MAXSZ = MAXQ + MAXN + 5;
int parent[MAXSZ], edges[MAXSZ], name[MAXSZ], sz[MAXSZ];

int n, q;

void init() {
  for (int i = 1; i <= n; ++i)
    parent[i] = i;
  for (int i = 1; i <= n; ++i)
    name[i] = i;
  for (int i = 1; i <= n; ++i)
    sz[i] = 1;
}

int _find(int x) {
  if (parent[x] != x)
    parent[x] = _find(parent[x]);
  return parent[x];
}
int find(const int x) { return _find(name[x]); }

void onion(int x, int y) {
  if (sz[x] < sz[y])
    swap(x, y);

  parent[y] = x;
  sz[x] += sz[y];
  edges[x] += edges[y] + 1;
}

void add_edge(int x, int y) {
  x = find(x), y = find(y);
  if (x != y)
    onion(x, y);
  else
    ++edges[x];
}

char query(int x) {
  x = find(x);
  if (edges[x] == sz[x])
    return '1';
  if (edges[x] == 0)
    return '0';
  return '?';
}

void split(const int x) {
  const int r = find(x);
  --edges[r], --sz[r];

  name[x] = ++n;
  sz[n] = 1;
  parent[n] = n;
}

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

  cin >> n >> q;

  init();

  for (int i = 0; i < q; ++i) {
    char c;
    int x, y;
    cin >> c;

    switch (c) {
    case '?':
      cin >> x;
      cout << query(x);
      break;
    case '+':
      cin >> x >> y;
      add_edge(x, y);
      break;
    default:
      cin >> x;
      split(x);
    }
  }
  cout << '\n';
}