#include<iostream> #include<istream> #include<vector> #include<stack> #include<cassert> #include<string> #include<cstdio> using namespace std; class Node { public: Node(): _broken(false), _top_parent(NULL), _tree_size(0), _has_loop(false), _id(_get_next_id()) {}; bool is_broken() { return _broken; } bool has_loop() { return get_top_parent()->_has_loop; } int get_id() { return get_top_parent()->_id; } Node* get_top_parent() { if (_top_parent == NULL) return this; Node* grandparent = _top_parent->get_top_parent(); _top_parent = grandparent; return grandparent; } int get_size() { return get_top_parent()->_tree_size; } void break_computer() { _broken = true; get_top_parent()->_tree_size -= 1; } void mark_loop() { get_top_parent()->_has_loop = true; } void union_with(Node* other) { // This method should be run only for roots. assert(this->_top_parent == NULL); assert(other->_top_parent == NULL); if (this->_tree_size >= other->_tree_size) { this->_tree_size += other->_tree_size; this->_tree_size++; this->_has_loop |= other->_has_loop; other->_top_parent = this; } else { other->_tree_size += this->_tree_size; other->_tree_size++; other->_has_loop |= this->_has_loop; this->_top_parent = other; } } private: bool _broken; Node *_top_parent; int _tree_size; int _has_loop; int _id; static int _next_id; static int _get_next_id() { int t = _next_id; _next_id++; return t; } }; int Node::_next_id = 1; int n; // Contains the most fresh node for the town. vector<Node*> actual; void link(int a, int b) { // TODO(kles): process case when a and b belongs to the same tree. if(actual[a]->is_broken()) { actual[a] = new Node(); } if (actual[b]->is_broken()) { actual[b] = new Node(); } if (actual[a]->get_id() == actual[b]->get_id()) { actual[a]->mark_loop(); } else { actual[a]->get_top_parent()->union_with(actual[b]->get_top_parent()); } } char query(int a) { Node* node = actual[a]; if (node == NULL || node->is_broken()) return '0'; if (node->get_top_parent()->has_loop()) return '1'; if (node->get_top_parent()->get_size() == 0) return '0'; return '?'; } int main() { //std::ios_base::sync_with_stdio(false); //std::cin.tie(NULL); scanf("%d", &n); actual.reserve(n+1); for (int i = 0; i < n + 1; i++) { actual.push_back(new Node()); } int q, a, b; char c; scanf("%d", &q); for(int i = 0; i < q; i++) { scanf(" %c", &c); //printf("%c\n", c); if (c == '+') { scanf("%d%d", &a, &b); // cin >> a >> b; link(a, b); } else if (c == '-') { scanf("%d", &a); actual[a]->break_computer(); } else if (c == '?') { scanf("%d", &a); printf("%c", query(a)); } } //cout << "\n"; printf("\n"); }
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 | #include<iostream> #include<istream> #include<vector> #include<stack> #include<cassert> #include<string> #include<cstdio> using namespace std; class Node { public: Node(): _broken(false), _top_parent(NULL), _tree_size(0), _has_loop(false), _id(_get_next_id()) {}; bool is_broken() { return _broken; } bool has_loop() { return get_top_parent()->_has_loop; } int get_id() { return get_top_parent()->_id; } Node* get_top_parent() { if (_top_parent == NULL) return this; Node* grandparent = _top_parent->get_top_parent(); _top_parent = grandparent; return grandparent; } int get_size() { return get_top_parent()->_tree_size; } void break_computer() { _broken = true; get_top_parent()->_tree_size -= 1; } void mark_loop() { get_top_parent()->_has_loop = true; } void union_with(Node* other) { // This method should be run only for roots. assert(this->_top_parent == NULL); assert(other->_top_parent == NULL); if (this->_tree_size >= other->_tree_size) { this->_tree_size += other->_tree_size; this->_tree_size++; this->_has_loop |= other->_has_loop; other->_top_parent = this; } else { other->_tree_size += this->_tree_size; other->_tree_size++; other->_has_loop |= this->_has_loop; this->_top_parent = other; } } private: bool _broken; Node *_top_parent; int _tree_size; int _has_loop; int _id; static int _next_id; static int _get_next_id() { int t = _next_id; _next_id++; return t; } }; int Node::_next_id = 1; int n; // Contains the most fresh node for the town. vector<Node*> actual; void link(int a, int b) { // TODO(kles): process case when a and b belongs to the same tree. if(actual[a]->is_broken()) { actual[a] = new Node(); } if (actual[b]->is_broken()) { actual[b] = new Node(); } if (actual[a]->get_id() == actual[b]->get_id()) { actual[a]->mark_loop(); } else { actual[a]->get_top_parent()->union_with(actual[b]->get_top_parent()); } } char query(int a) { Node* node = actual[a]; if (node == NULL || node->is_broken()) return '0'; if (node->get_top_parent()->has_loop()) return '1'; if (node->get_top_parent()->get_size() == 0) return '0'; return '?'; } int main() { //std::ios_base::sync_with_stdio(false); //std::cin.tie(NULL); scanf("%d", &n); actual.reserve(n+1); for (int i = 0; i < n + 1; i++) { actual.push_back(new Node()); } int q, a, b; char c; scanf("%d", &q); for(int i = 0; i < q; i++) { scanf(" %c", &c); //printf("%c\n", c); if (c == '+') { scanf("%d%d", &a, &b); // cin >> a >> b; link(a, b); } else if (c == '-') { scanf("%d", &a); actual[a]->break_computer(); } else if (c == '?') { scanf("%d", &a); printf("%c", query(a)); } } //cout << "\n"; printf("\n"); } |