#include <iostream> #include <vector> #include <cassert> using namespace std; #define DBG false struct M { int node_id; enum { yes, no, unknown } state; }; vector<M> ms; void dump_M(int mid) { M &m = ms[mid]; cout << "M " << m.node_id << " "; switch (m.state) { case M::yes: cout << 1; break; case M::no: cout << 0; break; case M::unknown: cout << '?'; break; default: assert(0); break; } cout << endl; } struct Node { int m_id; int parent; int first_child; int sibling; int rank; int cnt; }; vector<Node> nodes; void make_single(int m_id) { Node n; n.first_child = -1; n.sibling = -1; n.rank = 0; n.m_id = m_id; n.parent = -1; n.cnt = 1; ms[m_id].node_id = nodes.size(); nodes.push_back(n); } void dump_node(int n_id) { Node &n = nodes[n_id]; cout << "node # " << n_id << " parent " << n.parent << endl; } int find_r(int n_id) { if (DBG) { cout << "+find " << n_id << endl; dump_node(n_id); } Node &n = nodes[n_id]; if (n.parent == -1) { if (DBG) { cout << "-find " << n_id << " -> self" << endl; } return n_id; } n.parent = find_r(n.parent); if (DBG) { cout << "-find " << n_id << " -> " << n.parent << endl; } return n.parent; } // return true if already joined bool join(int n1_id, int n2_id) { if (DBG) { cout << "+join " << n1_id << " " << n2_id << endl; } int r1 = find_r(n1_id); int r2 = find_r(n2_id); if (r1 == r2) { if (DBG) { cout << "-join " << n1_id << " " << n2_id << "true" << endl; dump_node(n1_id); dump_node(n2_id); dump_node(r1); } return true; } Node &n1 = nodes[r1]; Node &n2 = nodes[r2]; if (n1.rank < n2.rank) { n1.parent = r2; assert(n1.sibling == -1); n1.sibling = n2.first_child; n2.first_child = r1; n2.cnt += n1.cnt; } else if (n1.rank > n2.rank) { n2.parent = r1; assert(n2.sibling == -1); n2.sibling = n1.first_child; n1.first_child = r2; n1.cnt += n2.cnt; } else if (r1 != r2) { n2.parent = r1; assert(n2.sibling == -1); n2.sibling = n1.first_child; n1.first_child = r2; n1.cnt += n2.cnt; n1.rank++; } if (DBG) { cout << "-join " << n1_id << " " << n2_id << "false" << endl; dump_node(n1_id); dump_node(n2_id); dump_node(r1); dump_node(r2); } return false; } void visit(int n_id, bool owns); void remove(int m_id) { M &m = ms[m_id]; Node &n = nodes[m.node_id]; assert(n.m_id == m_id); if (n.parent == -1 && n.first_child == -1) return; n.m_id = -1; int r = find_r(m.node_id); nodes[r].cnt--; make_single(m_id); if (nodes[r].cnt == 1) { visit(r, false); } } void query() { int m_id; cin >> m_id; if (DBG) { cout << "query " << m_id << " -> "; } switch (ms[m_id].state) { case M::yes: cout << 1; break; case M::no: cout << 0; break; case M::unknown: cout << '?'; break; default: assert(0); break; } if (DBG) { cout << endl; dump_M(m_id); } } void visit(int n_id, bool owns) { Node &n = nodes[n_id]; if (DBG) { cout << "visit\n"; dump_node(n_id); } if (n.first_child != -1) visit(n.first_child, owns); if (n.sibling != -1) visit(n.sibling, owns); if (n.m_id == -1) return; M &m = ms[n.m_id]; if (DBG) { dump_M(n.m_id); } assert(m.state == M::unknown); m.state = owns ? M::yes : M::no; make_single(n.m_id); } void del_comp() { int m_id; cin >> m_id; M &m = ms[m_id]; m.state = M::no; remove(m_id); } void add() { int m1_id, m2_id; cin >> m1_id >> m2_id; M &m1 = ms[m1_id]; M &m2 = ms[m2_id]; if (DBG) { cout << "+add\n"; dump_M(m1_id); dump_M(m2_id); } if (m1.state == M::yes) { if (m2.state == M::unknown) { visit(find_r(m2.node_id), true); } else { assert(m2.state == M::no); m2.state = M::yes; } if (DBG) { dump_M(m1_id); dump_M(m2_id); } return; } else if (m2.state == M::yes) { if (m1.state == M::unknown) { visit(find_r(m1.node_id), true); } else { assert(m1.state == M::no); m1.state = M::yes; } if (DBG) { dump_M(m1_id); dump_M(m2_id); } return; } else { m1.state = M::unknown; m2.state = M::unknown; } if (DBG) { cout << "update states\n"; dump_M(m1_id); dump_M(m2_id); } if (join(m1.node_id, m2.node_id)) { visit(find_r(m1.node_id), true); } } int main() { int n, q; cin >> n >> q; ms.resize(n+1); nodes.reserve(n+q); for (int i = 1; i <= n; ++i) { make_single(i); ms[i].state = M::no; } for (int i = 0; i < q; ++i) { char op; cin >> op; switch (op) { case '+': add(); break; case '-': del_comp(); break; case '?': query(); break; } } 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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | #include <iostream> #include <vector> #include <cassert> using namespace std; #define DBG false struct M { int node_id; enum { yes, no, unknown } state; }; vector<M> ms; void dump_M(int mid) { M &m = ms[mid]; cout << "M " << m.node_id << " "; switch (m.state) { case M::yes: cout << 1; break; case M::no: cout << 0; break; case M::unknown: cout << '?'; break; default: assert(0); break; } cout << endl; } struct Node { int m_id; int parent; int first_child; int sibling; int rank; int cnt; }; vector<Node> nodes; void make_single(int m_id) { Node n; n.first_child = -1; n.sibling = -1; n.rank = 0; n.m_id = m_id; n.parent = -1; n.cnt = 1; ms[m_id].node_id = nodes.size(); nodes.push_back(n); } void dump_node(int n_id) { Node &n = nodes[n_id]; cout << "node # " << n_id << " parent " << n.parent << endl; } int find_r(int n_id) { if (DBG) { cout << "+find " << n_id << endl; dump_node(n_id); } Node &n = nodes[n_id]; if (n.parent == -1) { if (DBG) { cout << "-find " << n_id << " -> self" << endl; } return n_id; } n.parent = find_r(n.parent); if (DBG) { cout << "-find " << n_id << " -> " << n.parent << endl; } return n.parent; } // return true if already joined bool join(int n1_id, int n2_id) { if (DBG) { cout << "+join " << n1_id << " " << n2_id << endl; } int r1 = find_r(n1_id); int r2 = find_r(n2_id); if (r1 == r2) { if (DBG) { cout << "-join " << n1_id << " " << n2_id << "true" << endl; dump_node(n1_id); dump_node(n2_id); dump_node(r1); } return true; } Node &n1 = nodes[r1]; Node &n2 = nodes[r2]; if (n1.rank < n2.rank) { n1.parent = r2; assert(n1.sibling == -1); n1.sibling = n2.first_child; n2.first_child = r1; n2.cnt += n1.cnt; } else if (n1.rank > n2.rank) { n2.parent = r1; assert(n2.sibling == -1); n2.sibling = n1.first_child; n1.first_child = r2; n1.cnt += n2.cnt; } else if (r1 != r2) { n2.parent = r1; assert(n2.sibling == -1); n2.sibling = n1.first_child; n1.first_child = r2; n1.cnt += n2.cnt; n1.rank++; } if (DBG) { cout << "-join " << n1_id << " " << n2_id << "false" << endl; dump_node(n1_id); dump_node(n2_id); dump_node(r1); dump_node(r2); } return false; } void visit(int n_id, bool owns); void remove(int m_id) { M &m = ms[m_id]; Node &n = nodes[m.node_id]; assert(n.m_id == m_id); if (n.parent == -1 && n.first_child == -1) return; n.m_id = -1; int r = find_r(m.node_id); nodes[r].cnt--; make_single(m_id); if (nodes[r].cnt == 1) { visit(r, false); } } void query() { int m_id; cin >> m_id; if (DBG) { cout << "query " << m_id << " -> "; } switch (ms[m_id].state) { case M::yes: cout << 1; break; case M::no: cout << 0; break; case M::unknown: cout << '?'; break; default: assert(0); break; } if (DBG) { cout << endl; dump_M(m_id); } } void visit(int n_id, bool owns) { Node &n = nodes[n_id]; if (DBG) { cout << "visit\n"; dump_node(n_id); } if (n.first_child != -1) visit(n.first_child, owns); if (n.sibling != -1) visit(n.sibling, owns); if (n.m_id == -1) return; M &m = ms[n.m_id]; if (DBG) { dump_M(n.m_id); } assert(m.state == M::unknown); m.state = owns ? M::yes : M::no; make_single(n.m_id); } void del_comp() { int m_id; cin >> m_id; M &m = ms[m_id]; m.state = M::no; remove(m_id); } void add() { int m1_id, m2_id; cin >> m1_id >> m2_id; M &m1 = ms[m1_id]; M &m2 = ms[m2_id]; if (DBG) { cout << "+add\n"; dump_M(m1_id); dump_M(m2_id); } if (m1.state == M::yes) { if (m2.state == M::unknown) { visit(find_r(m2.node_id), true); } else { assert(m2.state == M::no); m2.state = M::yes; } if (DBG) { dump_M(m1_id); dump_M(m2_id); } return; } else if (m2.state == M::yes) { if (m1.state == M::unknown) { visit(find_r(m1.node_id), true); } else { assert(m1.state == M::no); m1.state = M::yes; } if (DBG) { dump_M(m1_id); dump_M(m2_id); } return; } else { m1.state = M::unknown; m2.state = M::unknown; } if (DBG) { cout << "update states\n"; dump_M(m1_id); dump_M(m2_id); } if (join(m1.node_id, m2.node_id)) { visit(find_r(m1.node_id), true); } } int main() { int n, q; cin >> n >> q; ms.resize(n+1); nodes.reserve(n+q); for (int i = 1; i <= n; ++i) { make_single(i); ms[i].state = M::no; } for (int i = 0; i < q; ++i) { char op; cin >> op; switch (op) { case '+': add(); break; case '-': del_comp(); break; case '?': query(); break; } } return 0; } |