#include <iostream> #include <cstring> int main() { int n = 0, q = 0; std::cin >> n >> q; int count = 0, countUnknown = 0; char tab[n]; memset(tab, '0', n); while (q-- > 0) { char znak; std::cin >> znak; switch (znak) { case '+': { int a = 0, b = 0; std::cin >> a >> b; ++count; if (a == b) { if (tab[a] == '0') ++countUnknown; tab[a] = '1'; } else if (tab[a] == '1') { if (tab[b] == '0') ++countUnknown; tab[b] = '1'; } else if (tab[b] == '1') { if (tab[a] == '0') ++countUnknown; tab[a] = '1'; } else { if (tab[a] == '0') { ++countUnknown; tab[a] = '?'; } if (tab[b] == '0') { ++countUnknown; tab[b] = '?'; } } break; } case '-': { int a = 0; std::cin >> a; --count; --countUnknown; tab[a] = '0'; break; } case '?': { int a = 0; std::cin >> a; std::cout << tab[a]; break; } } if (count == countUnknown) { for (int i = 0; i < n; ++i) { if (tab[i] == '?') tab[i] = '1'; } } } }
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 | #include <iostream> #include <cstring> int main() { int n = 0, q = 0; std::cin >> n >> q; int count = 0, countUnknown = 0; char tab[n]; memset(tab, '0', n); while (q-- > 0) { char znak; std::cin >> znak; switch (znak) { case '+': { int a = 0, b = 0; std::cin >> a >> b; ++count; if (a == b) { if (tab[a] == '0') ++countUnknown; tab[a] = '1'; } else if (tab[a] == '1') { if (tab[b] == '0') ++countUnknown; tab[b] = '1'; } else if (tab[b] == '1') { if (tab[a] == '0') ++countUnknown; tab[a] = '1'; } else { if (tab[a] == '0') { ++countUnknown; tab[a] = '?'; } if (tab[b] == '0') { ++countUnknown; tab[b] = '?'; } } break; } case '-': { int a = 0; std::cin >> a; --count; --countUnknown; tab[a] = '0'; break; } case '?': { int a = 0; std::cin >> a; std::cout << tab[a]; break; } } if (count == countUnknown) { for (int i = 0; i < n; ++i) { if (tab[i] == '?') tab[i] = '1'; } } } } |