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

enum State {
  ZERO,
  ONE,
  UNKNOWN
};

ostream& operator<< (ostream& out, const State& s) {
  if(s == ZERO) out << "0";
  else if(s == ONE) out << "1";
  else out << "?";
  return out;
}

struct FU {
  vector<int> rep;
  vector<vector<int>> group;
  vector<int> cnt;

  FU(int n): rep(n), group(n), cnt(n, 1) {
    for(int i=0;i<n;i++)
      rep[i] = i, group[i] = {i};
  }

  int find(int x) {
    return (rep[x] == x ? x : rep[x] = find(rep[x]));
  }

  bool merge(int a, int b) {
    a = find(a), b = find(b);
    if(a == b)
      return false;

    if(group[a].size() < group[b].size())
      swap(a, b);

    rep[b] = a;
    cnt[a] += cnt[b];
    for(int i : group[b])
      group[a].push_back(i);
    group[b].clear();

    return true;
  }

  void reset(int x) {
    rep[x] = x;
    group[x] = {x};
  }
};

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

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

  FU f(n+q);
  vector<int> rep(n+q);
  int cnt = n;
  vector<vector<int>> id(n);
  for(int i=0;i<n;i++)
    id[i] = {i}, rep[i] = i;

  vector<State> result(n+q, State::ZERO);

  auto reset = [&](const vector<int>& A, const State& s) {
    for(int i : A) {
      auto t = (result[i] == ZERO ? ZERO : s);
      result[i] = t;

      f.reset(i);
      f.cnt[f.find(i)] = (t == ZERO ? 1 : 0);
    }
  };

  auto touch = [&](int x) {
    if(result[x] == ZERO)
      result[x] = UNKNOWN, f.cnt[f.find(x)]--;
  };

  for(int it=1;it<=q;it++) {
    char t;
    cin >> t;

    if(t == '?') {
      int a;
      cin >> a, a--;

      cout << result[id[a].back()];
    } else if(t == '+') {
      int a, b;
      cin >> a >> b, a--, b--;

      a = id[a].back(), b = id[b].back();

      if(f.merge(a, b) && result[a] != ONE && result[b] != ONE) {
        touch(a), touch(b);
      } else {
        touch(a), touch(b);

        auto A = f.group[f.find(a)];
        auto B = f.group[f.find(b)];
        A.insert(A.end(), B.begin(), B.end());
        reset(A, ONE);
      }
    } else {
      int a;
      cin >> a, a--;

      a = id[a].back();

      if(result[a] == ZERO) continue;

      result[a] = ZERO;

      int r = f.find(a);
      f.cnt[r]++;

      if(f.cnt[r]+1 == f.group[r].size())
        reset(f.group[r], ZERO);
      else {
        int x = cnt++;
        id[rep[a]].push_back(x);
        rep[x] = rep[a];
      }
    }
  }

  cout << "\n";

  return 0;
}