#include<cstdio> #define N 300005 class node { public: node* parent; int rank; bool kompletny; int size; void setKompletny() { kompletny = true; } void increaseSize() { //printf("x"); size++; } void decreaseSize() { size--; } }; node* tab[N]; static node* find(node* start) { node* current = start; while (current->parent != current) { current = current->parent; } node* current2 = start; while (current2->parent != current2) { node* next = current2->parent; current2->parent = current; current2 = next; } return current; } static node* makeSet() { auto result = new node(); result->parent = result; result->kompletny = 0; result->rank = 0; result->size = 0; return result; } static node* makeSetKompletny() { node* result = makeSet(); result->setKompletny(); return result; } static void unionSet(node* x, node* y) { bool shouldSetKompletny = x->kompletny || y->kompletny; if (x->rank > y->rank) { y->parent = x; x->size += y->size; } else { x->parent = y; y->size += x->size; if (x->rank == y->rank) { y->rank++; } } if (shouldSetKompletny) { x->setKompletny(); y->setKompletny(); } } int main() { int n, q; scanf("%d %d\n", &n, &q); for (int i = 0; i < q; i++) { char c = getc(stdin); if (c == '?') { int d; scanf("%d\n", &d); if (tab[d] == NULL) { printf("0"); } else { auto f = find(tab[d]); if (f->kompletny) { printf("1"); } else { //printf("(%d)", f->size); if (f->size == 1) { printf("0"); } else { printf("?"); } } } } else if (c == '+') { int a, b; scanf("%d %d\n", &a, &b); if (a == b) { if (tab[a] == NULL) { tab[a] = makeSetKompletny(); } else { auto f = find(tab[a]); f->setKompletny(); } } else { if (tab[a] == NULL && tab[b] == NULL) { auto s = makeSet(); tab[a] = s; tab[b] = s; //printf("<%d,%d>", a, b); s->increaseSize(); s->increaseSize(); } else if (tab[a] == NULL && tab[b] != NULL) { auto f = find(tab[b]); tab[a] = f; //printf("<%d>", a); f->increaseSize(); } else if (tab[a] != NULL && tab[b] == NULL) { auto f = find(tab[a]); tab[b] = f; //printf("<%d>", b); f->increaseSize(); } else { auto f1 = find(tab[a]); auto f2 = find(tab[b]); if (f1 == f2) { f1->setKompletny(); } else { unionSet(f1, f2); } } } } else if (c == '-') { int c; scanf("%d\n", &c); auto f = find(tab[c]); f->decreaseSize(); tab[c] = NULL; } } return 0; }
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 | #include<cstdio> #define N 300005 class node { public: node* parent; int rank; bool kompletny; int size; void setKompletny() { kompletny = true; } void increaseSize() { //printf("x"); size++; } void decreaseSize() { size--; } }; node* tab[N]; static node* find(node* start) { node* current = start; while (current->parent != current) { current = current->parent; } node* current2 = start; while (current2->parent != current2) { node* next = current2->parent; current2->parent = current; current2 = next; } return current; } static node* makeSet() { auto result = new node(); result->parent = result; result->kompletny = 0; result->rank = 0; result->size = 0; return result; } static node* makeSetKompletny() { node* result = makeSet(); result->setKompletny(); return result; } static void unionSet(node* x, node* y) { bool shouldSetKompletny = x->kompletny || y->kompletny; if (x->rank > y->rank) { y->parent = x; x->size += y->size; } else { x->parent = y; y->size += x->size; if (x->rank == y->rank) { y->rank++; } } if (shouldSetKompletny) { x->setKompletny(); y->setKompletny(); } } int main() { int n, q; scanf("%d %d\n", &n, &q); for (int i = 0; i < q; i++) { char c = getc(stdin); if (c == '?') { int d; scanf("%d\n", &d); if (tab[d] == NULL) { printf("0"); } else { auto f = find(tab[d]); if (f->kompletny) { printf("1"); } else { //printf("(%d)", f->size); if (f->size == 1) { printf("0"); } else { printf("?"); } } } } else if (c == '+') { int a, b; scanf("%d %d\n", &a, &b); if (a == b) { if (tab[a] == NULL) { tab[a] = makeSetKompletny(); } else { auto f = find(tab[a]); f->setKompletny(); } } else { if (tab[a] == NULL && tab[b] == NULL) { auto s = makeSet(); tab[a] = s; tab[b] = s; //printf("<%d,%d>", a, b); s->increaseSize(); s->increaseSize(); } else if (tab[a] == NULL && tab[b] != NULL) { auto f = find(tab[b]); tab[a] = f; //printf("<%d>", a); f->increaseSize(); } else if (tab[a] != NULL && tab[b] == NULL) { auto f = find(tab[a]); tab[b] = f; //printf("<%d>", b); f->increaseSize(); } else { auto f1 = find(tab[a]); auto f2 = find(tab[b]); if (f1 == f2) { f1->setKompletny(); } else { unionSet(f1, f2); } } } } else if (c == '-') { int c; scanf("%d\n", &c); auto f = find(tab[c]); f->decreaseSize(); tab[c] = NULL; } } return 0; } |