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
#if defined(EMBE_DEBUG) && !defined(NDEBUG)
#include "embe-debug.hpp"
#else
#define LOG_INDENT(...) do {} while (false)
#define LOG(...) do {} while (false)
#define DUMP(...) do {} while (false)
#endif

#include <algorithm>
#include <cassert>
#include <functional>
#include <iostream>
#include <ranges>
#include <vector>
using namespace std;

namespace {

enum class Maybe {
  no,
  maybe,
  yes,
};

ostream& operator<<(ostream& os, Maybe x) {
  if (x == Maybe::no) os << '0';
  else if (x == Maybe::maybe) os << '?';
  else if (x == Maybe::yes) os << '1';
  else assert(false);

  return os;
}

class Solver {
private:
  vector<int> parent;
  vector<int> size;
  vector<int> people;
  vector<int> computers;
  vector<int> id;

  int alloc()
  {
    int x = parent.size();
    parent.push_back(x);
    size.push_back(1);
    people.push_back(1);
    computers.push_back(0);
    return x;
  }

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

  int merge(int a, int b)
  {
    a = find(a);
    b = find(b);
    if (a == b) return a;
    if (size[a] < size[b]) {
      parent[a] = b;
      people[b] += people[a];
      computers[b] += computers[a];
      return b;
    } else {
      parent[b] = a;
      people[a] += people[b];
      computers[a] += computers[b];
      return a;
    }
  }

public:
  Solver(int n) {
    parent.reserve(n);
    size.reserve(n);
    people.reserve(n);
    computers.reserve(n);
    id.reserve(n);
    for (int v = 0; v < n; ++v) {
      id.push_back(alloc());
    }
  }

  void deliver(int u, int v)
  {
    auto a = find(id[u - 1]);
    auto b = find(id[v - 1]);
    int c = merge(a, b);
    ++computers[c];
    assert(computers[c] <= people[c]);
  }

  void reset(int v)
  {
    auto a = find(id[v - 1]);
    assert(people[a] > 0);
    assert(computers[a] > 0);
    --people[a];
    --computers[a];
    id[v - 1] = alloc();
  }

  Maybe query(int v)
  {
    auto a = find(id[v - 1]);
    if (computers[a] == 0) return Maybe::no;
    if (computers[a] < people[a]) return Maybe::maybe;
    return Maybe::yes;
  }
};

}

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

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

  Solver solver(n);
  for (int i = 0; i < q; ++i) {
    char op;
    cin >> op;
    if (op == '+') {
      int a, b;
      cin >> a >> b;
      solver.deliver(a, b);
    } else if (op == '-') {
      int c;
      cin >> c;
      solver.reset(c);
    } else if (op == '?') {
      int d;
      cin >> d;
      cout << solver.query(d);
    } else {
      assert(false);
    }
  }
  cout << endl;

  return 0;
}