// PA2024 runda 1A - https://sio2.mimuw.edu.pl/c/pa-2024-1/p/mod/ //-std=c++20 #include<iostream> #include <cstddef> #include <algorithm> #include <vector> #include<set> using I = u_int64_t; enum TB { F, T, U }; struct Subgraph { std::set<I> elems; void add(I x) { elems.insert(x); } void remove(I x) { elems.erase(x); } bool is_empty() { return elems.empty(); } }; struct Mod { I n; //<=300K I q; // <= 1M std::vector<TB> state; std::vector<Subgraph *> vertex_subgraph; void run() { std::cin >> n >> q; state.resize(n, F); vertex_subgraph.resize(n, nullptr); for (int i = 0; i < q; ++i) { next_line(); } } void process_query(I x) { switch (state[x]) { case T: std::cout << '1'; break; case F : std::cout << '0'; break; case U: std::cout << '?'; break; } } void merge_subgraphs(I x, I y) { auto *target = vertex_subgraph[x]; auto *source = vertex_subgraph[y]; if (target->elems.size() < source->elems.size()) std::swap(target, source); for (const auto &item: source->elems) { target->add(item); vertex_subgraph[item] = target; } delete source; } void process_add(I x, I y) { if (state[x] == T && state[y] == T) { impossible_state(x, y); } else if (state[x] == F && state[y] == T) { state[x] = T; } else if (state[x] == U && state[y] == T) { true_subgraph(x); } else if (state[x] == T && state[y] == U) { true_subgraph(y); } else if (state[x] == F && state[y] == U) { add_to_subraph(y, x); } else if (state[x] == U && state[y] == U) { if (vertex_subgraph[x] == vertex_subgraph[y]) { true_subgraph(x); } else { merge_subgraphs(x, y); } } else if (state[x] == T && state[y] == F) { state[y] = T; } else if (state[x] == F && state[y] == F) { if (x == y) { state[x] = T; } else { new_subgraph(x, y); } } else if (state[x] == U && state[y] == F) { add_to_subraph(x, y); } } void impossible_state(I x, I y) { std::cerr << "Tried to add new laptop, but both of them already have them " << x << " " << y; } void new_subgraph(I x, I y) { auto *s = new Subgraph; s->add(x); s->add(y); vertex_subgraph[x] = s; vertex_subgraph[y] = s; state[x] = U; state[y] = U; } void add_to_subraph(I old, I new_i) { state[new_i] = U; vertex_subgraph[new_i] = vertex_subgraph[old]; vertex_subgraph[new_i]->add(new_i); } void true_subgraph(I x) { Subgraph *subgraph = vertex_subgraph[x]; for (const auto &item: subgraph->elems) { state[item] = T; vertex_subgraph[item] = nullptr; } delete subgraph; } void process_delete(I x) { if (vertex_subgraph[x]) { vertex_subgraph[x]->remove(x); if (vertex_subgraph[x]->is_empty()) { delete vertex_subgraph[x]; vertex_subgraph[x] = nullptr; } else if (vertex_subgraph[x]->elems.size() == 1) { I leftover = *vertex_subgraph[x]->elems.begin(); state[leftover] = F; vertex_subgraph[leftover] = nullptr; delete vertex_subgraph[x]; } } state[x] = F; } void next_line() { char command; I x, y; std::cin >> command; std::cin >> x; x--; // user indexes from 1 switch (command) { case '?': process_query(x); break; case '+': std::cin >> y; y--; process_add(x, y); break; case '-': process_delete(x); break; } } }; //19:50 - 21:10, 9:35 - 10:05, 11:00-14:00 int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); Mod mod; mod.run(); }
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 | // PA2024 runda 1A - https://sio2.mimuw.edu.pl/c/pa-2024-1/p/mod/ //-std=c++20 #include<iostream> #include <cstddef> #include <algorithm> #include <vector> #include<set> using I = u_int64_t; enum TB { F, T, U }; struct Subgraph { std::set<I> elems; void add(I x) { elems.insert(x); } void remove(I x) { elems.erase(x); } bool is_empty() { return elems.empty(); } }; struct Mod { I n; //<=300K I q; // <= 1M std::vector<TB> state; std::vector<Subgraph *> vertex_subgraph; void run() { std::cin >> n >> q; state.resize(n, F); vertex_subgraph.resize(n, nullptr); for (int i = 0; i < q; ++i) { next_line(); } } void process_query(I x) { switch (state[x]) { case T: std::cout << '1'; break; case F : std::cout << '0'; break; case U: std::cout << '?'; break; } } void merge_subgraphs(I x, I y) { auto *target = vertex_subgraph[x]; auto *source = vertex_subgraph[y]; if (target->elems.size() < source->elems.size()) std::swap(target, source); for (const auto &item: source->elems) { target->add(item); vertex_subgraph[item] = target; } delete source; } void process_add(I x, I y) { if (state[x] == T && state[y] == T) { impossible_state(x, y); } else if (state[x] == F && state[y] == T) { state[x] = T; } else if (state[x] == U && state[y] == T) { true_subgraph(x); } else if (state[x] == T && state[y] == U) { true_subgraph(y); } else if (state[x] == F && state[y] == U) { add_to_subraph(y, x); } else if (state[x] == U && state[y] == U) { if (vertex_subgraph[x] == vertex_subgraph[y]) { true_subgraph(x); } else { merge_subgraphs(x, y); } } else if (state[x] == T && state[y] == F) { state[y] = T; } else if (state[x] == F && state[y] == F) { if (x == y) { state[x] = T; } else { new_subgraph(x, y); } } else if (state[x] == U && state[y] == F) { add_to_subraph(x, y); } } void impossible_state(I x, I y) { std::cerr << "Tried to add new laptop, but both of them already have them " << x << " " << y; } void new_subgraph(I x, I y) { auto *s = new Subgraph; s->add(x); s->add(y); vertex_subgraph[x] = s; vertex_subgraph[y] = s; state[x] = U; state[y] = U; } void add_to_subraph(I old, I new_i) { state[new_i] = U; vertex_subgraph[new_i] = vertex_subgraph[old]; vertex_subgraph[new_i]->add(new_i); } void true_subgraph(I x) { Subgraph *subgraph = vertex_subgraph[x]; for (const auto &item: subgraph->elems) { state[item] = T; vertex_subgraph[item] = nullptr; } delete subgraph; } void process_delete(I x) { if (vertex_subgraph[x]) { vertex_subgraph[x]->remove(x); if (vertex_subgraph[x]->is_empty()) { delete vertex_subgraph[x]; vertex_subgraph[x] = nullptr; } else if (vertex_subgraph[x]->elems.size() == 1) { I leftover = *vertex_subgraph[x]->elems.begin(); state[leftover] = F; vertex_subgraph[leftover] = nullptr; delete vertex_subgraph[x]; } } state[x] = F; } void next_line() { char command; I x, y; std::cin >> command; std::cin >> x; x--; // user indexes from 1 switch (command) { case '?': process_query(x); break; case '+': std::cin >> y; y--; process_add(x, y); break; case '-': process_delete(x); break; } } }; //19:50 - 21:10, 9:35 - 10:05, 11:00-14:00 int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); Mod mod; mod.run(); } |