#include <iostream> #include <vector> using namespace std; vector<vector<int>> village; bool fun(bool a, bool b){ return (a && !b) || (!a && b); } bool isCycle(int start, int whereAmI){ bool isSingleConnection; for (int i = 0; i < village.size(); i++){ isSingleConnection = fun(village[whereAmI][i], village[i][whereAmI]); if ((i == start && isSingleConnection) || (isSingleConnection && isCycle(start, i))){ village[whereAmI][whereAmI] = 1; return true; } } return false; } void handleInsertion(){ int a, b; cin >> a >> b; if (village[a][b] == 1) village[b][a] = 1; else village[a][b] = 1; village[a][a] = 0; village[b][b] = 0; isCycle(a, a); } void handleRemoval(){ int a; cin >> a; village[a][a] = -1; for (int i = 0; i < village.size(); i++){ if (i != a && fun(village[a][i], village[i][a])){ village[a][i] = village[i][a] = 0; village[i][i] = -1; } } } void handleQuery(){ int a; cin >> a; if (village[a][a] == 1) cout << "1"; else if(village[a][a] == -1) cout << "0"; else cout << "?"; } int main() { int countOfVillagers, countOfQueries; cin >> countOfVillagers >> countOfQueries; village.resize(countOfVillagers + 1); for (int i = 0; i < countOfVillagers + 1; i++) { village[i].resize(countOfVillagers + 1, 0); village[i][i] = -1; } char query; for (int i = 0; i < countOfQueries; i++){ cin >> query; switch(query){ case '+': handleInsertion(); break; case '-': handleRemoval(); break; case '?': handleQuery(); break; } } 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 | #include <iostream> #include <vector> using namespace std; vector<vector<int>> village; bool fun(bool a, bool b){ return (a && !b) || (!a && b); } bool isCycle(int start, int whereAmI){ bool isSingleConnection; for (int i = 0; i < village.size(); i++){ isSingleConnection = fun(village[whereAmI][i], village[i][whereAmI]); if ((i == start && isSingleConnection) || (isSingleConnection && isCycle(start, i))){ village[whereAmI][whereAmI] = 1; return true; } } return false; } void handleInsertion(){ int a, b; cin >> a >> b; if (village[a][b] == 1) village[b][a] = 1; else village[a][b] = 1; village[a][a] = 0; village[b][b] = 0; isCycle(a, a); } void handleRemoval(){ int a; cin >> a; village[a][a] = -1; for (int i = 0; i < village.size(); i++){ if (i != a && fun(village[a][i], village[i][a])){ village[a][i] = village[i][a] = 0; village[i][i] = -1; } } } void handleQuery(){ int a; cin >> a; if (village[a][a] == 1) cout << "1"; else if(village[a][a] == -1) cout << "0"; else cout << "?"; } int main() { int countOfVillagers, countOfQueries; cin >> countOfVillagers >> countOfQueries; village.resize(countOfVillagers + 1); for (int i = 0; i < countOfVillagers + 1; i++) { village[i].resize(countOfVillagers + 1, 0); village[i][i] = -1; } char query; for (int i = 0; i < countOfQueries; i++){ cin >> query; switch(query){ case '+': handleInsertion(); break; case '-': handleRemoval(); break; case '?': handleQuery(); break; } } return 0; } |