#include <vector> #include <string> #include <list> #include <unordered_set> #include <unordered_map> #include <iostream> class Solution { public: Solution(int aUsersCount, int aEventsCount) : usersCount{aUsersCount} , eventsCount{aEventsCount} , users(usersCount + 1, 0) {} void run() { std::string result; for (int eventId = 0; eventId < eventsCount; eventId++) { processEvent(result); } std::cout << result << "\n"; } char getAnswerForUser(int userId) { const int userStatus = users[userId]; return (userStatus == -1) ? '?' : ((userStatus == 1) ? '1' : '0'); } void processEvent(std::string& result) { char op; int userId1; std::cin >> op >> userId1; if (op == '?') { result.push_back(getAnswerForUser(userId1)); } else if (op == '+') { int userId2; std::cin >> userId2; addComputer(userId1, userId2); } else if (op == '-') { removeComputer(userId1); } } void addComputer(int userId1, int userId2) { if (userId1 == userId2) { addComputer(userId1); } else { const int userId1Status = users[userId1]; const int userId2Status = users[userId2]; if (userId1Status == -1 && userId2Status == -1) { mergeSharedGroups(userId1, userId2); } else if (userId1Status == 1) { addComputer(userId2); } else if (userId2Status == 1) { addComputer(userId1); } else if (userId1Status == 0 && userId2Status == 0) { createNewSharedGroup(userId1, userId2); } else if (userId1Status == 0) { addUserToSharedGroup(userId2, userId1); } else if (userId2Status == 0) { addUserToSharedGroup(userId1, userId2); } } } void addComputer(int userId) { if (users[userId] == -1) { removeSharedGroup(userId); } else if (users[userId] == 0) { users[userId] = 1; } } void removeComputer(int userId) { if (users[userId] == -1) { removeUserFromSharedGroup(userId); } users[userId] = 0; } void addUserToSharedGroup(int userInExistingGroup, int newUser) { auto it = userToSharedGroup[userInExistingGroup]; userToSharedGroup[newUser] = it; users[newUser] = -1; it->insert(newUser); } void mergeSharedGroups(int userId1, int userId2) { auto sharedGroupIt1 = userToSharedGroup[userId1]; auto sharedGroupIt2 = userToSharedGroup[userId2]; if (sharedGroupIt1 == sharedGroupIt2) { removeSharedGroup(userId1); } else { if (sharedGroupIt1->size() > sharedGroupIt2->size()) { for (auto id : (*sharedGroupIt2)){ userToSharedGroup[id] = sharedGroupIt1; } sharedGroupIt1->insert(sharedGroupIt2->begin(), sharedGroupIt2->end()); sharedGroups.erase(sharedGroupIt2); } else { for (auto id : (*sharedGroupIt1)) { userToSharedGroup[id] = sharedGroupIt2; } sharedGroupIt2->insert(sharedGroupIt1->begin(), sharedGroupIt1->end()); sharedGroups.erase(sharedGroupIt1); } } } void removeUserFromSharedGroup(int userId) { auto sharedGroupIt = userToSharedGroup[userId]; sharedGroupIt->erase(userId); userToSharedGroup.erase(userId); if (sharedGroupIt->size() == 1) { int remainingId = *sharedGroupIt->begin(); users[remainingId] = 0; userToSharedGroup.erase(remainingId); sharedGroups.erase(sharedGroupIt); } } void removeSharedGroup(int userId) { auto sharedGroupIt = userToSharedGroup[userId]; for (auto id : (*sharedGroupIt)) { users[id] = 1; userToSharedGroup.erase(id); } sharedGroups.erase(sharedGroupIt); } void createNewSharedGroup(int userId1, int userId2) { auto it = sharedGroups.insert(sharedGroups.end(), std::unordered_set<int>()); it->insert(userId1); it->insert(userId2); userToSharedGroup[userId1] = it; userToSharedGroup[userId2] = it; users[userId1] = -1; users[userId2] = -1; } int usersCount; int eventsCount; std::vector<int> users; std::list<std::unordered_set<int>> sharedGroups; std::unordered_map<int, std::list<std::unordered_set<int>>::iterator> userToSharedGroup; }; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int usersCount, eventsCount; std::cin >> usersCount >> eventsCount; Solution solution(usersCount, eventsCount); solution.run(); 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 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 147 148 149 150 151 152 153 154 155 156 157 158 | #include <vector> #include <string> #include <list> #include <unordered_set> #include <unordered_map> #include <iostream> class Solution { public: Solution(int aUsersCount, int aEventsCount) : usersCount{aUsersCount} , eventsCount{aEventsCount} , users(usersCount + 1, 0) {} void run() { std::string result; for (int eventId = 0; eventId < eventsCount; eventId++) { processEvent(result); } std::cout << result << "\n"; } char getAnswerForUser(int userId) { const int userStatus = users[userId]; return (userStatus == -1) ? '?' : ((userStatus == 1) ? '1' : '0'); } void processEvent(std::string& result) { char op; int userId1; std::cin >> op >> userId1; if (op == '?') { result.push_back(getAnswerForUser(userId1)); } else if (op == '+') { int userId2; std::cin >> userId2; addComputer(userId1, userId2); } else if (op == '-') { removeComputer(userId1); } } void addComputer(int userId1, int userId2) { if (userId1 == userId2) { addComputer(userId1); } else { const int userId1Status = users[userId1]; const int userId2Status = users[userId2]; if (userId1Status == -1 && userId2Status == -1) { mergeSharedGroups(userId1, userId2); } else if (userId1Status == 1) { addComputer(userId2); } else if (userId2Status == 1) { addComputer(userId1); } else if (userId1Status == 0 && userId2Status == 0) { createNewSharedGroup(userId1, userId2); } else if (userId1Status == 0) { addUserToSharedGroup(userId2, userId1); } else if (userId2Status == 0) { addUserToSharedGroup(userId1, userId2); } } } void addComputer(int userId) { if (users[userId] == -1) { removeSharedGroup(userId); } else if (users[userId] == 0) { users[userId] = 1; } } void removeComputer(int userId) { if (users[userId] == -1) { removeUserFromSharedGroup(userId); } users[userId] = 0; } void addUserToSharedGroup(int userInExistingGroup, int newUser) { auto it = userToSharedGroup[userInExistingGroup]; userToSharedGroup[newUser] = it; users[newUser] = -1; it->insert(newUser); } void mergeSharedGroups(int userId1, int userId2) { auto sharedGroupIt1 = userToSharedGroup[userId1]; auto sharedGroupIt2 = userToSharedGroup[userId2]; if (sharedGroupIt1 == sharedGroupIt2) { removeSharedGroup(userId1); } else { if (sharedGroupIt1->size() > sharedGroupIt2->size()) { for (auto id : (*sharedGroupIt2)){ userToSharedGroup[id] = sharedGroupIt1; } sharedGroupIt1->insert(sharedGroupIt2->begin(), sharedGroupIt2->end()); sharedGroups.erase(sharedGroupIt2); } else { for (auto id : (*sharedGroupIt1)) { userToSharedGroup[id] = sharedGroupIt2; } sharedGroupIt2->insert(sharedGroupIt1->begin(), sharedGroupIt1->end()); sharedGroups.erase(sharedGroupIt1); } } } void removeUserFromSharedGroup(int userId) { auto sharedGroupIt = userToSharedGroup[userId]; sharedGroupIt->erase(userId); userToSharedGroup.erase(userId); if (sharedGroupIt->size() == 1) { int remainingId = *sharedGroupIt->begin(); users[remainingId] = 0; userToSharedGroup.erase(remainingId); sharedGroups.erase(sharedGroupIt); } } void removeSharedGroup(int userId) { auto sharedGroupIt = userToSharedGroup[userId]; for (auto id : (*sharedGroupIt)) { users[id] = 1; userToSharedGroup.erase(id); } sharedGroups.erase(sharedGroupIt); } void createNewSharedGroup(int userId1, int userId2) { auto it = sharedGroups.insert(sharedGroups.end(), std::unordered_set<int>()); it->insert(userId1); it->insert(userId2); userToSharedGroup[userId1] = it; userToSharedGroup[userId2] = it; users[userId1] = -1; users[userId2] = -1; } int usersCount; int eventsCount; std::vector<int> users; std::list<std::unordered_set<int>> sharedGroups; std::unordered_map<int, std::list<std::unordered_set<int>>::iterator> userToSharedGroup; }; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int usersCount, eventsCount; std::cin >> usersCount >> eventsCount; Solution solution(usersCount, eventsCount); solution.run(); return 0; } |