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
#include <iostream>
#include <vector>
#include <set>
using namespace std;
template < typename T >
struct Reuse : vector< T > {
  vector< size_t > unused;
  Reuse(size_t s = 0) : vector< T >(s) {
  }
  size_t assign() {
    size_t result;
    if (unused.size()) {
      result = *unused.rbegin();
      unused.pop_back();
    } else
      this->resize((result = this->size()) + 1);
    return result;
  }
  void cancel(size_t id) {
    unused.push_back(id);
  }
};
struct Graph {
  struct Vertex {
    int group;
    bool hasComputer;
    Vertex() : group(0), hasComputer(false) {
    }
  };
  struct Group {
    int root;
    set< int > v;
    vector< int > g;
    struct
    {
      int users, computers;
    } count;
    void clear() {
      root = 0;
      v.clear();
      g.clear();
      count.users = count.computers = 0;
    }
  };
  vector< Vertex > V;
  Reuse< Group > G;
  Graph(int n) : V(n + 1), G(1) {
  }
  vector< int > gUnused;
  int groupRoot(int gid) {
    vector< int > path;
    while (G[gid].root) {
      path.push_back(gid);
      gid = G[gid].root;
    }
    for (vector< int >::reverse_iterator it = path.rbegin(); it != path.rend(); ++it)
      G[*it].root = gid;
    return gid;
  }
  void groupLeave(int vid, bool takeComputer = false) {
    int gid = groupRoot(V[vid].group);
    if (gid) {
      G[gid].count.computers -= (takeComputer ? 1 : 0);
      G[gid].count.users -= 1;
      G[V[vid].group].v.erase(vid);
      V[vid].hasComputer = takeComputer;
      V[vid].group = 0;
      groupUpdate(gid);
    }
  }
  void groupJoin(int gid, int vid) {
    V[vid].group = gid;
    G[gid].count.users += 1;
    G[gid].v.insert(vid);
  }
  void groupUpdate(int gid) {
    if (!G[gid].count.computers || G[gid].count.users == G[gid].count.computers) {
      bool toSet = G[gid].count.computers != 0;
      vector< int > todo;
      todo.push_back(gid);
      for (int i = 0; i < (int)todo.size(); ++i) {
        gid = todo[i];
        set< int > &v = G[gid].v;
        vector< int > &g = G[gid].g;
        for (set< int >::iterator it = v.begin(); it != v.end(); ++it) {
          V[*it].hasComputer = toSet;
          V[*it].group = 0;
        }
        for (vector< int >::iterator it = g.begin(); it != g.end(); ++it)
          todo.push_back(*it);
        G[gid].clear();
        G.cancel(gid);
      }
    }
  }
  void groupMerge(int ga, int gb) {
    G[gb].root = ga;
    G[ga].count.users += G[gb].count.users;
    G[ga].count.computers += G[gb].count.computers;
    G[ga].g.push_back(gb);
  }
  void provideComputer(int a, int b) {
    if (V[a].hasComputer)
      a = b;
    if (V[b].hasComputer)
      b = a;
    if (a == b) {
      groupLeave(a);
      V[a].hasComputer = true;
      return;
    }
    int ga = groupRoot(V[a].group), gb = groupRoot(V[b].group);
    if (!ga && !gb) {
      int gid = G.assign();
      G[gid].count.computers += 1;
      groupJoin(gid, a);
      groupJoin(gid, b);
    } else if (ga == gb) {
      G[ga].count.computers += 1;
      groupUpdate(ga);
    } else if (ga && gb) {
      G[ga].count.computers += 1;
      groupMerge(ga, gb);
    } else {
      if (ga) {
        swap(a, b);
        swap(ga, gb);
      }
      groupJoin(gb, a);
      G[gb].count.computers += 1;
    }
  }
  void breakDownComputer(int a) {
    groupLeave(a, true);
    V[a].hasComputer = false;
  }
  char query(int a) const {
    return V[a].group ? '?' : ('0' + V[a].hasComputer);
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, p, q;
  cin >> n >> q;
  Graph g(n);
  char *answer = new char[q + 2], *a = answer;
  string cmd;
  while (q-- && cin >> cmd >> p)
    switch (cmd[0]) {
    case '+':
      cin >> n;
      g.provideComputer(p, n);
      break;
    case '-':
      g.breakDownComputer(p);
      break;
    default:
      *a++ = g.query(p);
    }
  *a++ = '\n';
  *a++ = '\0';
  cout << answer;
  delete[] answer;
}