#include <iostream>
#include <vector>
#include <unordered_set>
#include <unordered_map>
using namespace std;
class ComputerTracker {
private:
unordered_map<int, int> computerStatus; // 1: ma komputer, 0: nie ma, -1: nieznany
unordered_set<int> certainComputers; // Mieszkańcy, którzy na pewno mają komputer
unordered_set<int> noComputers; // Mieszkańcy, którzy na pewno nie mają komputera
public:
ComputerTracker(int n) {
for (int i = 1; i <= n; ++i) {
computerStatus[i] = -1; // Początkowo status każdego mieszkańca jest nieznany
}
}
void addComputer(int a, int b) {
if (a == b || certainComputers.find(a) != certainComputers.end() || noComputers.find(b) != noComputers.end()) {
certainComputers.insert(a);
computerStatus[a] = 1;
} else if (certainComputers.find(b) != certainComputers.end() || noComputers.find(a) != noComputers.end()) {
certainComputers.insert(b);
computerStatus[b] = 1;
} else {
computerStatus[a] = computerStatus[b] = 0; // Obydwaj kandydaci mogą mieć komputer
}
}
void removeComputer(int c) {
certainComputers.erase(c); // Usuwamy z listy pewniaków
noComputers.insert(c); // Dodajemy do listy tych, co na pewno nie mają
computerStatus[c] = 0; // Aktualizujemy status na "nie ma komputera"
}
char query(int d) {
if (certainComputers.find(d) != certainComputers.end()) {
return '1'; // Na pewno ma komputer
} else if (noComputers.find(d) != noComputers.end()) {
return '0'; // Na pewno nie ma komputera
} else {
return '?'; // Nie wiadomo
}
}
};
int main() {
int n, q;
cin >> n >> q;
ComputerTracker tracker(n);
vector<char> answers;
for (int i = 0; i < q; ++i) {
char type;
cin >> type;
if (type == '+') {
int a, b;
cin >> a >> b;
tracker.addComputer(a, b);
} else if (type == '-') {
int c;
cin >> c;
tracker.removeComputer(c);
} else if (type == '?') {
int d;
cin >> d;
answers.push_back(tracker.query(d));
}
}
for (char answer : answers) {
cout << answer;
}
cout << endl;
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 | #include <iostream> #include <vector> #include <unordered_set> #include <unordered_map> using namespace std; class ComputerTracker { private: unordered_map<int, int> computerStatus; // 1: ma komputer, 0: nie ma, -1: nieznany unordered_set<int> certainComputers; // Mieszkańcy, którzy na pewno mają komputer unordered_set<int> noComputers; // Mieszkańcy, którzy na pewno nie mają komputera public: ComputerTracker(int n) { for (int i = 1; i <= n; ++i) { computerStatus[i] = -1; // Początkowo status każdego mieszkańca jest nieznany } } void addComputer(int a, int b) { if (a == b || certainComputers.find(a) != certainComputers.end() || noComputers.find(b) != noComputers.end()) { certainComputers.insert(a); computerStatus[a] = 1; } else if (certainComputers.find(b) != certainComputers.end() || noComputers.find(a) != noComputers.end()) { certainComputers.insert(b); computerStatus[b] = 1; } else { computerStatus[a] = computerStatus[b] = 0; // Obydwaj kandydaci mogą mieć komputer } } void removeComputer(int c) { certainComputers.erase(c); // Usuwamy z listy pewniaków noComputers.insert(c); // Dodajemy do listy tych, co na pewno nie mają computerStatus[c] = 0; // Aktualizujemy status na "nie ma komputera" } char query(int d) { if (certainComputers.find(d) != certainComputers.end()) { return '1'; // Na pewno ma komputer } else if (noComputers.find(d) != noComputers.end()) { return '0'; // Na pewno nie ma komputera } else { return '?'; // Nie wiadomo } } }; int main() { int n, q; cin >> n >> q; ComputerTracker tracker(n); vector<char> answers; for (int i = 0; i < q; ++i) { char type; cin >> type; if (type == '+') { int a, b; cin >> a >> b; tracker.addComputer(a, b); } else if (type == '-') { int c; cin >> c; tracker.removeComputer(c); } else if (type == '?') { int d; cin >> d; answers.push_back(tracker.query(d)); } } for (char answer : answers) { cout << answer; } cout << endl; return 0; } |
English