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
#include <cstdio>
#include <unordered_set>

const char NO = '0';
const char YES = '1';
const char QUESTION = '?';

struct Group {
  int computers;
  std::unordered_set<int> members;
};

struct State {
  char value;
  Group *group;

  State() : value(NO), group(nullptr) {}
};

void clear_group(State **elems, Group *group, char target) {
  for (int m : group->members) {
    elems[m]->value = target;
    elems[m]->group = nullptr;
  }
  delete group;
}

void check_group(State **elems, Group *group) {
  if ((int)group->members.size() == group->computers) {
    clear_group(elems, group, YES);
  } else if (group->computers == 0) {
    clear_group(elems, group, NO);
  }
}

char get(State **elems, int query) { return elems[query]->value; }

void simple_add(State **elems, int which) {
  State *elem = elems[which];
  if (elem->value == YES) {
    throw "wtf4";
  } else if (elem->value == NO) {
    elem->value = YES;
  } else if (elem->value == QUESTION) {
    elem->value = YES;
    elem->group->members.erase(which);
    check_group(elems, elem->group);
  } else {
    throw "WTF5";
  }
}

void add_to_group(State **elems, int to_add, int to_group) {
  State *elem = elems[to_add];
  elem->value = QUESTION;
  elem->group = elems[to_group]->group;
  elem->group->members.insert(to_add);
  elem->group->computers += 1;
  check_group(elems, elem->group);
}

void create_group(State **elems, int a, int b) {
  Group *group = new Group();
  group->computers = 1;
  group->members.insert(a);
  group->members.insert(b);
  elems[a]->value = elems[b]->value = QUESTION;
  elems[a]->group = elems[b]->group = group;
}

void merge_groups(State **elems, int a, int b) {
  if (elems[a]->group == elems[b]->group) {
    elems[a]->group->computers += 1;
  } else {
    Group *smaller = elems[a]->group->members.size() < elems[b]->group->members.size() ? elems[a]->group : elems[b]->group;
    Group *bigger = elems[a]->group == smaller ? elems[b]->group : elems[a]->group;
    for (int m : smaller->members) {
      bigger->members.insert(m);
      elems[m]->group = bigger;
    }
    bigger->computers += 1 + smaller->computers;
  }
  check_group(elems, elems[a]->group);
}

void add(State **elems, int a, int b) {
  if (a == b || elems[b]->value == YES) {
    simple_add(elems, a);
  } else if (elems[a]->value == YES) {
    simple_add(elems, b);
  } else if (elems[a]->value == NO && elems[b]->value == QUESTION) {
    add_to_group(elems, a, b);
  } else if (elems[a]->value == QUESTION && elems[b]->value == NO) {
    add_to_group(elems, b, a);
  } else if (elems[a]->value == NO && elems[b]->value == NO) {
    create_group(elems, a, b);
  } else if (elems[a]->value == QUESTION && elems[b]->value == QUESTION) {
    merge_groups(elems, a, b);
  } else {
    throw "madafaka";
  }
}

void remove(State **elems, int which) {
  State *elem = elems[which];
  if (elem->value == NO) {
    throw "wtf";
  } else if (elem->value == YES) {
    elem->value = NO;
  } else if (elem->value == QUESTION) {
    elem->value = NO;
    elem->group->computers -= 1;
    elem->group->members.erase(which);
    check_group(elems, elem->group);
  } else {
    throw "wtf2";
  }
}

int main() {
  int N, Q;
  scanf("%d%d", &N, &Q);
  State **elems = new State *[N];
  for (int i = 0; i < N; ++i) {
    elems[i] = new State();
  }

  char line[1000];
  char *output = new char[1000100];

  int a, b;
  int ans = 0;
  fgets(line, 1000, stdin);
  for (int i = 0; i < Q; ++i) {
    fgets(line, 1000, stdin);
    if (line[0] == '?') {
      sscanf(line + 2, "%d", &a);
      output[ans++] = get(elems, a - 1);
      // printf("%c",get(elems, a-1));
      // fflush(stdout);
    } else if (line[0] == '-') {
      sscanf(line + 2, "%d", &a);
      remove(elems, a - 1);
    } else if (line[0] == '+') {
      sscanf(line + 2, "%d%d", &a, &b);
      add(elems, a - 1, b - 1);
    }
  }
  output[ans] = '\0';
  puts(output);
  return 0;
}