#include <iostream> #include <vector> using namespace std; const int UNDEF = -1; const int NO_COMPUTER = 0; const int HAS_COMPUTER = 1; class Solver { vector<int>repr; // for each node I keep its representant vector<vector<int>>my_component; // for each represenant I keep its nodes vector<int>component_size; //for each representant I keep its component size vector<int>component_fill; //for each representant I keep the number of nodes //in its component that are known to be true vector<int>state; // for each node I keep its state public: int add_node() { // add a single node that surely doesn't have a computer int id = repr.size(); repr.push_back(id); my_component.emplace_back(1, id); component_size.push_back(1); component_fill.push_back(0); state.push_back(NO_COMPUTER); return id; } void set_node(int a) { int x = repr[a]; if(state[a] != HAS_COMPUTER) { state[a] = HAS_COMPUTER; component_fill[x]++; } if(component_fill[x] == component_size[x] - 1) { for(int n : my_component[x]) { if(state[n] == UNDEF) state[n] = NO_COMPUTER; } } } void merge_nodes(int a, int b) { if(repr[a] == repr[b]) { // the nodes belong to the same component int x = repr[a]; component_fill[x] = component_size[x]; for(int n : my_component[x]) state[n] = HAS_COMPUTER; } else { // the nodes are in different components if(state[b] == HAS_COMPUTER) swap(a, b); if(state[a] == HAS_COMPUTER) { //b definitely received a computer int x = repr[b]; for(int n : my_component[x]) { state[n] = HAS_COMPUTER; } component_fill[x] = component_size[x]; // if(component_size[y] == component_fill[x]) { // for(int n : my_component[x]) { // state[n] = HAS_COMPUTER; // } // } else { // // state[b] = HAS_COMPUTER; // component_fill[x]++; // if(component_fill[x] == component_size[x] - 1) { // for(int n : my_component[x]) { // if(state[n] == UNDEF) { // state[n] = NO_COMPUTER; // } // } // } // } } else { state[a] = state[b] = UNDEF; int x = repr[a]; int y = repr[b]; if(component_size[x] < component_size[y]) swap(x, y); component_size[x] += component_size[y]; component_fill[x] += component_fill[y]; for(int n : my_component[y]) { repr[n] = x; my_component[x].push_back(n); } vector<int>().swap(my_component[y]); component_size[y] = component_fill[y] = -1; } } } int query_node(int a) { return state[a]; } void debug() { cout << "number of nodes is " << (int)repr.size() << "\n"; for(int i = 0; i < (int)repr.size(); i++) { cout << i + 1 << ": repr = " << repr[i] + 1 << ", state = " << state[i] << "\n"; if(component_size[i] > 0) { cout << "\tmy_component is "; for(int n : my_component[i]) cout << n + 1 << " "; cout << "\n"; cout << "\tcomponent_size = " << component_size[i] << "\n"; cout << "\tcomponent_fill = " << component_fill[i] << "\n"; } } } }; int main() { ios_base::sync_with_stdio(0); int n, q; cin >> n >> q; vector<int>my_repr(n); Solver s; for(int i = 0; i < n; i++) my_repr[i] = s.add_node(); while(q--) { char c; cin >> c; if(c == '+') { int a, b; cin >> a >> b; a--; b--; s.merge_nodes(my_repr[a], my_repr[b]); } else if(c == '-') { int x; cin >> x; x--; s.set_node(my_repr[x]); my_repr[x] = s.add_node(); } else { int x; cin >> x; x--; int y = s.query_node(my_repr[x]); if(y == HAS_COMPUTER) cout << "1"; else if(y == NO_COMPUTER) cout << "0"; else cout << "?"; } // cout << "\n-----------------\n"; // // cout << "representants:\n"; // for(int i = 0; i < n; i++) // cout << i + 1 << ": " << my_repr[i] + 1 << "\n"; // // s.debug(); // // cout << "\n\n"; } 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | #include <iostream> #include <vector> using namespace std; const int UNDEF = -1; const int NO_COMPUTER = 0; const int HAS_COMPUTER = 1; class Solver { vector<int>repr; // for each node I keep its representant vector<vector<int>>my_component; // for each represenant I keep its nodes vector<int>component_size; //for each representant I keep its component size vector<int>component_fill; //for each representant I keep the number of nodes //in its component that are known to be true vector<int>state; // for each node I keep its state public: int add_node() { // add a single node that surely doesn't have a computer int id = repr.size(); repr.push_back(id); my_component.emplace_back(1, id); component_size.push_back(1); component_fill.push_back(0); state.push_back(NO_COMPUTER); return id; } void set_node(int a) { int x = repr[a]; if(state[a] != HAS_COMPUTER) { state[a] = HAS_COMPUTER; component_fill[x]++; } if(component_fill[x] == component_size[x] - 1) { for(int n : my_component[x]) { if(state[n] == UNDEF) state[n] = NO_COMPUTER; } } } void merge_nodes(int a, int b) { if(repr[a] == repr[b]) { // the nodes belong to the same component int x = repr[a]; component_fill[x] = component_size[x]; for(int n : my_component[x]) state[n] = HAS_COMPUTER; } else { // the nodes are in different components if(state[b] == HAS_COMPUTER) swap(a, b); if(state[a] == HAS_COMPUTER) { //b definitely received a computer int x = repr[b]; for(int n : my_component[x]) { state[n] = HAS_COMPUTER; } component_fill[x] = component_size[x]; // if(component_size[y] == component_fill[x]) { // for(int n : my_component[x]) { // state[n] = HAS_COMPUTER; // } // } else { // // state[b] = HAS_COMPUTER; // component_fill[x]++; // if(component_fill[x] == component_size[x] - 1) { // for(int n : my_component[x]) { // if(state[n] == UNDEF) { // state[n] = NO_COMPUTER; // } // } // } // } } else { state[a] = state[b] = UNDEF; int x = repr[a]; int y = repr[b]; if(component_size[x] < component_size[y]) swap(x, y); component_size[x] += component_size[y]; component_fill[x] += component_fill[y]; for(int n : my_component[y]) { repr[n] = x; my_component[x].push_back(n); } vector<int>().swap(my_component[y]); component_size[y] = component_fill[y] = -1; } } } int query_node(int a) { return state[a]; } void debug() { cout << "number of nodes is " << (int)repr.size() << "\n"; for(int i = 0; i < (int)repr.size(); i++) { cout << i + 1 << ": repr = " << repr[i] + 1 << ", state = " << state[i] << "\n"; if(component_size[i] > 0) { cout << "\tmy_component is "; for(int n : my_component[i]) cout << n + 1 << " "; cout << "\n"; cout << "\tcomponent_size = " << component_size[i] << "\n"; cout << "\tcomponent_fill = " << component_fill[i] << "\n"; } } } }; int main() { ios_base::sync_with_stdio(0); int n, q; cin >> n >> q; vector<int>my_repr(n); Solver s; for(int i = 0; i < n; i++) my_repr[i] = s.add_node(); while(q--) { char c; cin >> c; if(c == '+') { int a, b; cin >> a >> b; a--; b--; s.merge_nodes(my_repr[a], my_repr[b]); } else if(c == '-') { int x; cin >> x; x--; s.set_node(my_repr[x]); my_repr[x] = s.add_node(); } else { int x; cin >> x; x--; int y = s.query_node(my_repr[x]); if(y == HAS_COMPUTER) cout << "1"; else if(y == NO_COMPUTER) cout << "0"; else cout << "?"; } // cout << "\n-----------------\n"; // // cout << "representants:\n"; // for(int i = 0; i < n; i++) // cout << i + 1 << ": " << my_repr[i] + 1 << "\n"; // // s.debug(); // // cout << "\n\n"; } cout << "\n"; return 0; } |