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
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;

using ll = long long;
using ld = long double;

//#define int ll
#define sz(x) ((int)(x).size())

using pii = pair<int,int>;
using tii = tuple<int,int,int>;

using pq = priority_queue<int>;

void merge(pq& L, pq& R) {
  if(sz(L) < sz(R)) swap(L, R);
  while(sz(R))
    L.emplace(R.top()),
    R.pop();
  return;
}
void clear(pq& x) {
  while(!x.empty()) x.pop();
}

namespace DSU {
  vector<int> area, dsu, wavefunction; // fa si faza cu aria idk (?)
  vector<pair<pq, pq>> records;
  void init(int n) {
    dsu.assign(n, 0);
    area.assign(n, 1);
    wavefunction.assign(n, 0);
    records.assign(n, make_pair(pq(), pq()));
    iota(all(dsu), 0);
    for(int i = 0; i < n; i++)
      records[i].first.emplace(i);
  }
  
  int newnode() {
    dsu.emplace_back(sz(dsu));
    wavefunction.emplace_back(0);
    area.emplace_back(1);
    records.emplace_back(make_pair(pq(), pq()));
    records.back().first.emplace(dsu.back());
    return dsu.back();
  }
  
  int f(int x) {
    return x == dsu[x]? x : dsu[x] = f(dsu[x]); 
  }
  
  void unite(int x, int y) { // sunt convins ca un arbore simplu nu poate collapsa decat in urma collapsari tuturor celorlaltor noduri
    // da, am zis celorlaltor
    // bag pula in academicienii astia
    
    auto [a, b] = pii{x, y};
    x = f(x);
    y = f(y);
    if(x == y) {
      wavefunction[x] = 2;
      return;
    }
    
    if(area[x] < area[y]) swap(x, y);
    
    wavefunction[x] = max({1, wavefunction[x], wavefunction[y]});
    merge(records[x].first, records[y].first);
    merge(records[x].second, records[y].second);
    dsu[y] = x;
    area[x] += area[y];
    return;
  }
  
  void collapse(int x) {
    int t = f(x);
    records[t].second.emplace(x);
    
    while(!records[t].second.empty() && records[t].first.top() == records[t].second.top())
      records[t].first.pop(),
      records[t].second.pop();
    
    if(sz(records[t].first) == sz(records[t].second) + 1 && wavefunction[t] != 2) {
      int alone = records[t].first.top();
      
      clear(records[t].first);
      clear(records[t].second);
      
      dsu[alone] = alone;
      wavefunction[alone] = 0;
      area[alone] = 1;
      records[alone] = make_pair(pq(), pq());
      records[alone].first.emplace(alone);
    }
  }
  char query(int x) {
    int a = wavefunction[f(x)];
    return a == 0? '0' : a == 1? '?' : '1';
  }
}

signed main() {
  cin.tie(0) -> sync_with_stdio(0);
  int n, q;
  cin >> n >> q;
  
  vector<int> atrnode(n);
  iota(all(atrnode), 0);
  
  DSU::init(n);
  
  for(int a, b, i = 0; i < q; i++) {
    char ch;
    cin >> ch >> a;
    --a;
    if(ch == '+') {
      cin >> b;
      --b;
      DSU::unite(atrnode[a], atrnode[b]);
    }
    else if(ch == '-') {
      DSU::collapse(atrnode[a]);
      atrnode[a] = DSU::newnode();
    }
    else {
      cout << DSU::query(atrnode[a]);
    }
  }
  cout << '\n';
}

/**
  Anul asta nu se da centroid
  -- Rugaciunile mele
*/