#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/rope> using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef unsigned long long ull; typedef unsigned int uint; typedef vector<int> VI; typedef vector<vector<int>> VVI; typedef vector<long long> VL; typedef vector<vector<long long>> VVL; typedef vector<char> VC; typedef vector<vector<char>> VVC; typedef vector<bool> VB; typedef vector<vector<bool>> VVB; typedef pair<int, int> PII; typedef pair<long long, long long> PLL; typedef vector<pair<int, int>> VPI; typedef vector<pair<long long, long long>> VPL; #define LINE '\n' #define SPACE ' ' #define PB push_back #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define ALL(x) x.begin(), x.end() #define RALL(x) x.rbegin(), x.rend() #define HAS 1 #define NOT 0 #define MAYBE 2 void func(VI vec, set<int> setted) { for (auto it : setted) vec[it] = HAS; } void solve() { int n, q; cin >> n >> q; VI vec(n + 1, 0); set<int> setted; FOR(i, 0, q) { char c; cin >> c; if (c == '+') { int a, b; cin >> a >> b; if (a == b) { if (vec[a] == MAYBE) { vec[a] = HAS; func(vec, setted); } else { vec[a] = HAS; } } else { // a != b if (vec[a] == vec[b]) { if (vec[a] == MAYBE) { vec[a] = HAS; vec[b] = HAS; func(vec, setted); } else { vec[a] = MAYBE; vec[b] = MAYBE; setted.insert(a); setted.insert(b); } } else { // vec[a] != vec[b] if (vec[a] == HAS) { vec[b] = HAS; } else if (vec[b] == HAS) { vec[a] = HAS; } else { // vec is maybe or not vec[a] = MAYBE; vec[b] = MAYBE; setted.insert(a); setted.insert(b); } } } } else if(c == '-') { int a; cin >> a; if(vec[a] == HAS) vec[a] = NOT; else if(vec[a] == MAYBE) { func(vec, setted); vec[a] = NOT; } } else { int a; cin >> a; if(vec[a] == NOT) cout << 0; else if(vec[a] == HAS) cout << 1; else cout << '?'; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; // cin >> t; while (t--) solve(); }
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 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/rope> using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef unsigned long long ull; typedef unsigned int uint; typedef vector<int> VI; typedef vector<vector<int>> VVI; typedef vector<long long> VL; typedef vector<vector<long long>> VVL; typedef vector<char> VC; typedef vector<vector<char>> VVC; typedef vector<bool> VB; typedef vector<vector<bool>> VVB; typedef pair<int, int> PII; typedef pair<long long, long long> PLL; typedef vector<pair<int, int>> VPI; typedef vector<pair<long long, long long>> VPL; #define LINE '\n' #define SPACE ' ' #define PB push_back #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define ALL(x) x.begin(), x.end() #define RALL(x) x.rbegin(), x.rend() #define HAS 1 #define NOT 0 #define MAYBE 2 void func(VI vec, set<int> setted) { for (auto it : setted) vec[it] = HAS; } void solve() { int n, q; cin >> n >> q; VI vec(n + 1, 0); set<int> setted; FOR(i, 0, q) { char c; cin >> c; if (c == '+') { int a, b; cin >> a >> b; if (a == b) { if (vec[a] == MAYBE) { vec[a] = HAS; func(vec, setted); } else { vec[a] = HAS; } } else { // a != b if (vec[a] == vec[b]) { if (vec[a] == MAYBE) { vec[a] = HAS; vec[b] = HAS; func(vec, setted); } else { vec[a] = MAYBE; vec[b] = MAYBE; setted.insert(a); setted.insert(b); } } else { // vec[a] != vec[b] if (vec[a] == HAS) { vec[b] = HAS; } else if (vec[b] == HAS) { vec[a] = HAS; } else { // vec is maybe or not vec[a] = MAYBE; vec[b] = MAYBE; setted.insert(a); setted.insert(b); } } } } else if(c == '-') { int a; cin >> a; if(vec[a] == HAS) vec[a] = NOT; else if(vec[a] == MAYBE) { func(vec, setted); vec[a] = NOT; } } else { int a; cin >> a; if(vec[a] == NOT) cout << 0; else if(vec[a] == HAS) cout << 1; else cout << '?'; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; // cin >> t; while (t--) solve(); } |