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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Autor: Nicolas Wochnik
#include <iostream>
#include <unordered_map>
#include <vector>

class UnionFind {
private:
  std::vector<size_t> indices_map;
  std::unordered_map<size_t, size_t> rev_map;

  std::vector<size_t> parents;
  std::vector<std::vector<size_t>> childs;

public:
  std::vector<char> hard_values;
  std::vector<int> tree_sizes;

public:
  UnionFind(size_t size)
      : indices_map(size), rev_map(size), parents(size), childs(size),
        hard_values(size, '0'), tree_sizes(size, 1) {
    for (size_t i = 0; i < size; ++i) {
      indices_map[i] = i;
      rev_map[i] = i;
      parents[i] = i;
      childs[i] = {};
    }
  }

  void join(size_t x, size_t y) {
    auto a = indices_map[x];
    auto b = indices_map[y];

    auto a_leader = get_leader(a);
    auto b_leader = get_leader(b);

    if (tree_sizes[a] > tree_sizes[b]) {
      parents[b_leader] = a_leader;
      childs[a_leader].push_back(b_leader);
    } else {
      parents[a_leader] = b_leader;
      childs[b_leader].push_back(a_leader);
    }

    tree_sizes[get_leader(a_leader)] =
        tree_sizes[a_leader] + tree_sizes[b_leader];

    hard_values[x] = hard_values[y] = '?';
  }

  bool are_connected(size_t x, size_t y) {
    auto a = indices_map[x];
    auto b = indices_map[y];

    return get_leader(a) == get_leader(b);
  }

  void disjoin(size_t x) {
    tree_sizes[get_leader(indices_map[x])]--;

    rev_map.erase(indices_map[x]);

    size_t a = parents.size();
    indices_map[x] = a;
    rev_map[a] = x;

    parents.push_back(a);
    childs.push_back({});
    tree_sizes.push_back(1);

    hard_values[x] = '0';
  }

  void break_group(size_t x, char hard_value) {
    break_group_mapped(get_leader(indices_map[x]), hard_value);
  }

  void debug() {
    for (size_t i = 0; i < indices_map.size(); ++i) {
      std::cout << get_leader(indices_map[i]) << " ";
    }
    std::cout << "\n";
  }

  size_t get_leader_from_unmapped(size_t x) {
    return get_leader(indices_map[x]);
  }

private:
  size_t get_leader(size_t a) {
    size_t leader = a;

    while (leader != parents[leader]) {
      leader = parents[leader];
    }

    while (a != parents[a]) {
      size_t next_a = parents[a];
      parents[a] = leader;
      a = next_a;
    }

    return a;
  }

  void break_group_mapped(size_t a, char hard_value) {
    std::vector<size_t> stack;
    stack.push_back(a);

    while (!stack.empty()) {
      auto node = stack.back();
      stack.pop_back();

      for (auto child : childs[node]) {
        stack.push_back(child);
      }

      if (rev_map.count(node)) {
        hard_values[rev_map[node]] = hard_value;
      }

      parents[node] = node;
      childs[node] = {};
      tree_sizes[node] = 1;
    }
  }
};

void add_relation(UnionFind &relations, size_t x, size_t y) {
  if (relations.are_connected(x, y)) {
    relations.break_group(x, '1');
    return;
  }

  if (relations.hard_values[x] == '1') {
    relations.break_group(y, '1');
    return;
  }

  if (relations.hard_values[y] == '1') {
    relations.break_group(x, '1');
    return;
  }

  relations.join(x, y);
}

void remove_computer(UnionFind &relations, size_t x) {
  if (relations.tree_sizes[relations.get_leader_from_unmapped(x)] == 2) {
    relations.break_group(x, '0');
    return;
  }

  relations.disjoin(x);
}

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

  size_t size, queries;
  std::cin >> size >> queries;

  UnionFind relations(size);

  for (size_t i = 0; i < queries; i++) {
    char query;
    std::cin >> query;

    int val1, val2;

    switch (query) {
    case '?':
      std::cin >> val1;
      std::cout << relations.hard_values[val1 - 1];
      break;
    case '+':
      std::cin >> val1 >> val2;
      add_relation(relations, val1 - 1, val2 - 1);
      break;
    case '-':
      std::cin >> val1;
      remove_computer(relations, val1 - 1);
      break;
    }

    // relations.debug();
  }

  std::cout << "\n";

  return 0;
}