#include <iostream> using namespace std; class find_union { int ID; int depth; find_union* parent; int size; int comp_count; public: find_union* go_up() { find_union* curr = this; while(curr->parent != NULL) { curr = curr->parent; } return curr; } void unionize(find_union* other) { if(equivalent(other)) { go_up()->comp_count += 1; return; } find_union* root1 = go_up(); find_union* root2 = other->go_up(); if(root1->depth > root2->depth) { root2->parent = root1; root1->comp_count += root2->comp_count + 1; root1->size += root2->size; } else { root1->parent = root2; if(root1->depth < root2->depth) { root2->depth += 1; } root2->comp_count += root1->comp_count + 1; root2->size += root1->size; } } bool equivalent(find_union* other) { return find() == other->find(); } int has_comp() { find_union* root = go_up(); //cout << "MY ID: " << ID << ", root ID: " << root->ID << "\n"; //cout << "comp_count: " << root->comp_count << ", size: " << root->size << "\n"; if(root->comp_count == 0) return 0; if(root->comp_count == root->size) return 1; return -1; } int find() { find_union* curr = this; while(curr->parent != NULL) { /* cout << "LOOP\n"; if(curr == curr->parent) { cout << "I AM MY FATHER\n"; } */ curr = curr->parent; } return curr->ID; } void remove_el() { find_union* root = go_up(); root->comp_count -= 1; root->size -= 1; } find_union(int id) { ID = id; depth = 0; parent = NULL; size = 1; comp_count = 0; } }; void add_comp(int a, int b, int* ID, find_union** tab) { if(tab[a] == nullptr) { tab[a] = new find_union(*ID); *ID += 1; } if(tab[b] == nullptr) { tab[b] = new find_union(*ID); *ID += 1; } tab[a]->unionize(tab[b]); } void remove_comp(int a, find_union** tab) { tab[a]->remove_el(); tab[a] = nullptr; } int has_comp(int a, find_union** tab) { //1 - yes, 0 - no, -1 - don't know if(tab[a] == nullptr) return 0; //cout << " [a = " << a + 1<< "] "; return tab[a]->has_comp(); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, q; cin >> n; cin >> q; find_union** tab = new find_union*[n]; for(int i = 0; i < n; ++i) { tab[i] = nullptr; } int curr_ID = 0; char c; int a, b; int ans; for(int i = 0; i < q; ++i) { cin >> c; if(c == '+') { cin >> a; cin >> b; --a; --b; add_comp(a, b, &curr_ID, tab); } else if(c == '-') { cin >> a; --a; remove_comp(a, tab); } else { // '?' cin >> a; --a; ans = has_comp(a, tab); if(ans == -1) cout << "?"; else cout << ans; } } cout << "\n"; 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 | #include <iostream> using namespace std; class find_union { int ID; int depth; find_union* parent; int size; int comp_count; public: find_union* go_up() { find_union* curr = this; while(curr->parent != NULL) { curr = curr->parent; } return curr; } void unionize(find_union* other) { if(equivalent(other)) { go_up()->comp_count += 1; return; } find_union* root1 = go_up(); find_union* root2 = other->go_up(); if(root1->depth > root2->depth) { root2->parent = root1; root1->comp_count += root2->comp_count + 1; root1->size += root2->size; } else { root1->parent = root2; if(root1->depth < root2->depth) { root2->depth += 1; } root2->comp_count += root1->comp_count + 1; root2->size += root1->size; } } bool equivalent(find_union* other) { return find() == other->find(); } int has_comp() { find_union* root = go_up(); //cout << "MY ID: " << ID << ", root ID: " << root->ID << "\n"; //cout << "comp_count: " << root->comp_count << ", size: " << root->size << "\n"; if(root->comp_count == 0) return 0; if(root->comp_count == root->size) return 1; return -1; } int find() { find_union* curr = this; while(curr->parent != NULL) { /* cout << "LOOP\n"; if(curr == curr->parent) { cout << "I AM MY FATHER\n"; } */ curr = curr->parent; } return curr->ID; } void remove_el() { find_union* root = go_up(); root->comp_count -= 1; root->size -= 1; } find_union(int id) { ID = id; depth = 0; parent = NULL; size = 1; comp_count = 0; } }; void add_comp(int a, int b, int* ID, find_union** tab) { if(tab[a] == nullptr) { tab[a] = new find_union(*ID); *ID += 1; } if(tab[b] == nullptr) { tab[b] = new find_union(*ID); *ID += 1; } tab[a]->unionize(tab[b]); } void remove_comp(int a, find_union** tab) { tab[a]->remove_el(); tab[a] = nullptr; } int has_comp(int a, find_union** tab) { //1 - yes, 0 - no, -1 - don't know if(tab[a] == nullptr) return 0; //cout << " [a = " << a + 1<< "] "; return tab[a]->has_comp(); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, q; cin >> n; cin >> q; find_union** tab = new find_union*[n]; for(int i = 0; i < n; ++i) { tab[i] = nullptr; } int curr_ID = 0; char c; int a, b; int ans; for(int i = 0; i < q; ++i) { cin >> c; if(c == '+') { cin >> a; cin >> b; --a; --b; add_comp(a, b, &curr_ID, tab); } else if(c == '-') { cin >> a; --a; remove_comp(a, tab); } else { // '?' cin >> a; --a; ans = has_comp(a, tab); if(ans == -1) cout << "?"; else cout << ans; } } cout << "\n"; return 0; } |