#include <iostream> #include <map> #include <vector> #include <algorithm> using namespace std; /* 0 - na pewno nie ma 1 - może ma może nie ma 2 - na pewno ma */ int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n, q; char op; int a, b, c, d; map<int, int> mInhabitant; vector<pair<int, int>> vPeers; cin >> n >> q; while (q--) { cin >> op; switch (op) { case '+': { cin >> a >> b; if (mInhabitant[a] != 2) mInhabitant[a] = 1; else mInhabitant[b] = 2; if (mInhabitant[b] != 2) mInhabitant[b] = 1; else mInhabitant[a] = 2; if (a == b) { mInhabitant[a] = 2; } else { for (auto it : vPeers) { if (it.first == a) mInhabitant[it.second] = 2; if (it.first == b) mInhabitant[it.second] = 2; if (it.second == a) mInhabitant[it.first] = 2; if (it.second == b) mInhabitant[it.first] = 2; } vPeers.push_back(make_pair(a, b)); } break; } case '-': { vector<int> idx; cin >> c; mInhabitant[c] = 0; for (int i = 0; i < vPeers.size(); i++) { if (vPeers[i].first == c || vPeers[i].second == c) idx.push_back(i); } for (int i = idx.size() - 1; i >= 0; i--) vPeers.erase(vPeers.begin() + idx[i]); break; } case '?': { cin >> d; if (mInhabitant[d] == 0) cout << "0"; else if (mInhabitant[d] == 1) cout << "?"; else if (mInhabitant[d] == 2) cout << "1"; break; } } } 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 82 83 84 85 86 87 88 89 90 91 92 93 94 | #include <iostream> #include <map> #include <vector> #include <algorithm> using namespace std; /* 0 - na pewno nie ma 1 - może ma może nie ma 2 - na pewno ma */ int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n, q; char op; int a, b, c, d; map<int, int> mInhabitant; vector<pair<int, int>> vPeers; cin >> n >> q; while (q--) { cin >> op; switch (op) { case '+': { cin >> a >> b; if (mInhabitant[a] != 2) mInhabitant[a] = 1; else mInhabitant[b] = 2; if (mInhabitant[b] != 2) mInhabitant[b] = 1; else mInhabitant[a] = 2; if (a == b) { mInhabitant[a] = 2; } else { for (auto it : vPeers) { if (it.first == a) mInhabitant[it.second] = 2; if (it.first == b) mInhabitant[it.second] = 2; if (it.second == a) mInhabitant[it.first] = 2; if (it.second == b) mInhabitant[it.first] = 2; } vPeers.push_back(make_pair(a, b)); } break; } case '-': { vector<int> idx; cin >> c; mInhabitant[c] = 0; for (int i = 0; i < vPeers.size(); i++) { if (vPeers[i].first == c || vPeers[i].second == c) idx.push_back(i); } for (int i = idx.size() - 1; i >= 0; i--) vPeers.erase(vPeers.begin() + idx[i]); break; } case '?': { cin >> d; if (mInhabitant[d] == 0) cout << "0"; else if (mInhabitant[d] == 1) cout << "?"; else if (mInhabitant[d] == 2) cout << "1"; break; } } } cout << endl; return 0; } |