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

#define loop(i, a, b) for(int i = a; i <= b; i++)
#define sz(x) int(x.size())
#define eb emplace_back

struct DsuNode {
  enum struct Type {
    PLUS, MINUS, UNKNOWN
  } type;
  int ptr, size;
  DsuNode(Type t, int p, int s) : type(t), ptr(p), size(s) {}
};

struct Dsu {
  vector<DsuNode> nodes;
  vector<int> current_root;

  Dsu(int n) {
    current_root.resize(n);
    loop(i, 0, n-1) {
      current_root[i] = new_node();
    }
  }

  int new_node() {
    nodes.eb(DsuNode::Type::MINUS, sz(nodes), 1);
    return sz(nodes)-1;
  }

  int find(int x) {
    if(nodes[x].ptr == x) return x;
    else return nodes[x].ptr = find(nodes[x].ptr);
  }

  void join(int x, int y) {
    x = find(current_root[x]), y = find(current_root[y]);
    if(nodes[x].size > nodes[y].size) swap(x, y);
    if(x == y) {
      nodes[x].type = DsuNode::Type::PLUS;
    }
    else {
      auto t1 = nodes[x].type, t2 = nodes[y].type;
      nodes[x].ptr = y;
      nodes[y].size += nodes[x].size;
      nodes[x].size = 0;
      if(t1 == DsuNode::Type::PLUS || t2 == DsuNode::Type::PLUS) {
        nodes[y].type = DsuNode::Type::PLUS;
      }
      else {
        nodes[y].type = DsuNode::Type::UNKNOWN;
      }
    }
  }

  void del(int x) {
    int x_rep = find(current_root[x]);
    if(--nodes[x_rep].size == 1 && nodes[x_rep].type == DsuNode::Type::UNKNOWN) {
      nodes[x_rep].type = DsuNode::Type::MINUS;
    }
    current_root[x] = new_node();
  }

};

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n, q; cin >> n >> q;

  Dsu dsu(n+1);

  loop(i, 0, q-1) {
    char t; cin >> t;
    if(t == '+') {
      int a, b; cin >> a >> b;
      dsu.join(a, b);
    }
    else if(t == '-') {
      int x; cin >> x;
      dsu.del(x);
    }
    else {
      int x; cin >> x;
      auto type = dsu.nodes[dsu.find(dsu.current_root[x])].type;
      if (type == DsuNode::Type::PLUS) cout << '1';
      else if(type == DsuNode::Type::MINUS) cout << '0';
      else cout << '?';
    }
  }
  cout << '\n';

}