#include <bits/stdc++.h> using namespace std; const char ADD = '+'; const char REMOVE = '-'; const char QUERY = '?'; int groupCounter = 0; unordered_map<int, unordered_set<int>> connectedPersonsMap; struct Person { char state = '0'; int groupNumber = 0; }; int main() { // ifstream cin("tests/0b.in"); // ifstream cin("tests4/46.in"); cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false); int n, q, a, b, c, d; char evt; cin >> n; cin >> q; vector<Person> persons(n + 1); for (int i = 0; i < q; i++) { cin >> evt; if (evt == ADD) { cin >> a; cin >> b; if (persons[a].state == '1') { a = b; } else if (persons[b].state == '1') { b = a; } c = persons[a].groupNumber; d = persons[b].groupNumber; if (c == 0 && d == 0) { if (a == b) { persons[a].state = '1'; } else { groupCounter++; connectedPersonsMap[groupCounter].insert(a); connectedPersonsMap[groupCounter].insert(b); persons[a].groupNumber = groupCounter; persons[b].groupNumber = groupCounter; persons[a].state = QUERY; persons[b].state = QUERY; } } else if (c == 0) { persons[a].state = QUERY; persons[a].groupNumber = d; connectedPersonsMap[d].insert(a); } else if (d == 0) { persons[b].state = QUERY; persons[b].groupNumber = c; connectedPersonsMap[c].insert(b); } else if (c == d) { for (int g : connectedPersonsMap[c]) { persons[g].state = '1'; persons[g].groupNumber = 0; } connectedPersonsMap[c].clear(); } else { // c != d if (connectedPersonsMap[c].size() > connectedPersonsMap[d].size()) { connectedPersonsMap[c].insert( connectedPersonsMap[d].begin(), connectedPersonsMap[d].end()); for (int g : connectedPersonsMap[d]) { persons[g].groupNumber = c; } } else { connectedPersonsMap[d].insert( connectedPersonsMap[c].begin(), connectedPersonsMap[c].end()); for (int g : connectedPersonsMap[c]) { persons[g].groupNumber = d; } } } } else if (evt == REMOVE) { cin >> c; connectedPersonsMap[persons[c].groupNumber].erase(c); persons[c].state = '0'; if (connectedPersonsMap[persons[c].groupNumber].size() == 1) { for (int g : connectedPersonsMap[persons[c].groupNumber]) { persons[g].state = '0'; persons[g].groupNumber = 0; } } persons[c].groupNumber = 0; } else { // if(evt == QUERY) cin >> d; cout << persons[d].state; } } cout << endl; // prints 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 | #include <bits/stdc++.h> using namespace std; const char ADD = '+'; const char REMOVE = '-'; const char QUERY = '?'; int groupCounter = 0; unordered_map<int, unordered_set<int>> connectedPersonsMap; struct Person { char state = '0'; int groupNumber = 0; }; int main() { // ifstream cin("tests/0b.in"); // ifstream cin("tests4/46.in"); cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false); int n, q, a, b, c, d; char evt; cin >> n; cin >> q; vector<Person> persons(n + 1); for (int i = 0; i < q; i++) { cin >> evt; if (evt == ADD) { cin >> a; cin >> b; if (persons[a].state == '1') { a = b; } else if (persons[b].state == '1') { b = a; } c = persons[a].groupNumber; d = persons[b].groupNumber; if (c == 0 && d == 0) { if (a == b) { persons[a].state = '1'; } else { groupCounter++; connectedPersonsMap[groupCounter].insert(a); connectedPersonsMap[groupCounter].insert(b); persons[a].groupNumber = groupCounter; persons[b].groupNumber = groupCounter; persons[a].state = QUERY; persons[b].state = QUERY; } } else if (c == 0) { persons[a].state = QUERY; persons[a].groupNumber = d; connectedPersonsMap[d].insert(a); } else if (d == 0) { persons[b].state = QUERY; persons[b].groupNumber = c; connectedPersonsMap[c].insert(b); } else if (c == d) { for (int g : connectedPersonsMap[c]) { persons[g].state = '1'; persons[g].groupNumber = 0; } connectedPersonsMap[c].clear(); } else { // c != d if (connectedPersonsMap[c].size() > connectedPersonsMap[d].size()) { connectedPersonsMap[c].insert( connectedPersonsMap[d].begin(), connectedPersonsMap[d].end()); for (int g : connectedPersonsMap[d]) { persons[g].groupNumber = c; } } else { connectedPersonsMap[d].insert( connectedPersonsMap[c].begin(), connectedPersonsMap[c].end()); for (int g : connectedPersonsMap[c]) { persons[g].groupNumber = d; } } } } else if (evt == REMOVE) { cin >> c; connectedPersonsMap[persons[c].groupNumber].erase(c); persons[c].state = '0'; if (connectedPersonsMap[persons[c].groupNumber].size() == 1) { for (int g : connectedPersonsMap[persons[c].groupNumber]) { persons[g].state = '0'; persons[g].groupNumber = 0; } } persons[c].groupNumber = 0; } else { // if(evt == QUERY) cin >> d; cout << persons[d].state; } } cout << endl; // prints return 0; } |