#include <iostream> #include <vector> #include <unordered_set> #include <stack> bool comps[300001]; int group_id[300001]; std::vector<std::unordered_set<int>> groups = { {} }; std::stack<int> av_id; int id_count = 1; int n, q; void join(int a, int b) { if (groups[b].size() > groups[a].size()) std::swap(a, b); while (!groups[b].empty()) { group_id[*groups[b].begin()] = a; groups[a].insert(*groups[b].begin()); groups[b].erase(groups[b].begin()); } } void spread(std::unordered_set<int>& group, int id) { for (auto element : group) { comps[element] = true; group_id[element] = 0; } group.clear(); av_id.push(id); } int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cin >> n >> q; char znak; int a, b; for (int i = 0; i < q; i++) { std::cin >> znak; if (znak == '+') { std::cin >> a >> b; if (a == b) { comps[a] = true; if (group_id[a] != 0) spread(groups[group_id[a]], group_id[a]); } else if (comps[a]) { if (!comps[b]) { comps[b] = true; if (group_id[b] != 0) spread(groups[group_id[b]], group_id[b]); } } else if (comps[b]) { comps[a] = true; if (group_id[a] != 0) spread(groups[group_id[a]], group_id[a]); } else if (group_id[a] == 0) if (group_id[b] == 0) if (av_id.empty()) { group_id[a] = id_count; group_id[b] = id_count; groups.push_back({}); groups[id_count].insert(a); groups[id_count].insert(b); id_count++; } else { group_id[a] = av_id.top(); group_id[b] = av_id.top(); groups[av_id.top()].insert(a); groups[av_id.top()].insert(b); av_id.pop(); } else { group_id[a] = group_id[b]; groups[group_id[b]].insert(a); } else if (group_id[b] == 0) { group_id[b] = group_id[a]; groups[group_id[b]].insert(b); } else if (group_id[a] == group_id[b]) spread(groups[group_id[a]], group_id[a]); else join(group_id[a], group_id[b]); } else if (znak == '?') { std::cin >> a; if (comps[a]) std::cout << 1; else if (group_id[a] != 0) std::cout << '?'; else std::cout << 0; } else { std::cin >> a; comps[a] = false; if (group_id[a] != 0) { groups[group_id[a]].erase(a); if (groups[group_id[a]].size() == 1) { group_id[*groups[group_id[a]].begin()] = 0; groups[group_id[a]].clear(); } group_id[a] = 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 | #include <iostream> #include <vector> #include <unordered_set> #include <stack> bool comps[300001]; int group_id[300001]; std::vector<std::unordered_set<int>> groups = { {} }; std::stack<int> av_id; int id_count = 1; int n, q; void join(int a, int b) { if (groups[b].size() > groups[a].size()) std::swap(a, b); while (!groups[b].empty()) { group_id[*groups[b].begin()] = a; groups[a].insert(*groups[b].begin()); groups[b].erase(groups[b].begin()); } } void spread(std::unordered_set<int>& group, int id) { for (auto element : group) { comps[element] = true; group_id[element] = 0; } group.clear(); av_id.push(id); } int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cin >> n >> q; char znak; int a, b; for (int i = 0; i < q; i++) { std::cin >> znak; if (znak == '+') { std::cin >> a >> b; if (a == b) { comps[a] = true; if (group_id[a] != 0) spread(groups[group_id[a]], group_id[a]); } else if (comps[a]) { if (!comps[b]) { comps[b] = true; if (group_id[b] != 0) spread(groups[group_id[b]], group_id[b]); } } else if (comps[b]) { comps[a] = true; if (group_id[a] != 0) spread(groups[group_id[a]], group_id[a]); } else if (group_id[a] == 0) if (group_id[b] == 0) if (av_id.empty()) { group_id[a] = id_count; group_id[b] = id_count; groups.push_back({}); groups[id_count].insert(a); groups[id_count].insert(b); id_count++; } else { group_id[a] = av_id.top(); group_id[b] = av_id.top(); groups[av_id.top()].insert(a); groups[av_id.top()].insert(b); av_id.pop(); } else { group_id[a] = group_id[b]; groups[group_id[b]].insert(a); } else if (group_id[b] == 0) { group_id[b] = group_id[a]; groups[group_id[b]].insert(b); } else if (group_id[a] == group_id[b]) spread(groups[group_id[a]], group_id[a]); else join(group_id[a], group_id[b]); } else if (znak == '?') { std::cin >> a; if (comps[a]) std::cout << 1; else if (group_id[a] != 0) std::cout << '?'; else std::cout << 0; } else { std::cin >> a; comps[a] = false; if (group_id[a] != 0) { groups[group_id[a]].erase(a); if (groups[group_id[a]].size() == 1) { group_id[*groups[group_id[a]].begin()] = 0; groups[group_id[a]].clear(); } group_id[a] = 0; } } } } |