#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, q;
cin >> n >> q;
vector<int> tab(n + 1, 0);
vector<char> results;
for (int i = 0; i < q; i++) {
char a;
cin >> a;
if (a == '?') {
int b;
cin >> b;
if (tab[b] == 1)
results.push_back('1');
else if (tab[b] == 0)
results.push_back('0');
else
results.push_back('?');
} else if (a == '+') {
int m, p;
cin >> m >> p;
if(m==p)
tab[m]=1;
else if (tab[p] == 1) {
tab[m] = 1;
} else if (tab[m] == 1) {
tab[p] = 1;
} else if (tab[p] == 0 && tab[m] == 0) {
tab[p] = 2;
tab[m] = 2;
}
else if(tab[p]==2 && tab[m]==2){
tab[p]=1;
tab[m]=1;
}
else if(tab[p]==0)
tab[p]=2;
else if(tab[m]==0)
tab[m]=2;
} else if (a == '-') {
int b;
cin >> b;
tab[b] = 0;
}
}
for (char result : results) {
cout << result;
}
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 | #include <iostream> #include <vector> using namespace std; int main() { int n, q; cin >> n >> q; vector<int> tab(n + 1, 0); vector<char> results; for (int i = 0; i < q; i++) { char a; cin >> a; if (a == '?') { int b; cin >> b; if (tab[b] == 1) results.push_back('1'); else if (tab[b] == 0) results.push_back('0'); else results.push_back('?'); } else if (a == '+') { int m, p; cin >> m >> p; if(m==p) tab[m]=1; else if (tab[p] == 1) { tab[m] = 1; } else if (tab[m] == 1) { tab[p] = 1; } else if (tab[p] == 0 && tab[m] == 0) { tab[p] = 2; tab[m] = 2; } else if(tab[p]==2 && tab[m]==2){ tab[p]=1; tab[m]=1; } else if(tab[p]==0) tab[p]=2; else if(tab[m]==0) tab[m]=2; } else if (a == '-') { int b; cin >> b; tab[b] = 0; } } for (char result : results) { cout << result; } return 0; } |
English