#include <cstdio> #include <set> #include <vector> #include <deque> typedef unsigned int smallnum; // typedef unsigned char mininum; typedef std::set<smallnum> smallset; typedef std::deque<smallnum> smallqueue; class node { public: smallnum number; int tree_parent; smallset incoming_love; smallset outgoing_love; smallset incoming_hate; smallset outgoing_hate; }; void debug_print_set(const smallset s) { printf("set("); for (smallset::iterator iter = s.begin(); iter != s.end(); iter++) { printf("%u,", *iter); } printf(")\n"); } int main() { // Step 1 - Get data smallnum n, m; scanf("%u %u\n", &n, &m); // Set up nodes structure std::vector<node> nodes(n); for (smallnum i = 0; i < n; i++) { nodes[i].number = i; nodes[i].tree_parent = -2; } for (smallnum i = 0; i < m; i++) { smallnum a, b; char c; scanf("%u %u %c\n", &a, &b, &c); // We readress nodes to k - 1, so that they match array indices switch(c) { case 'T': nodes[a-1].outgoing_love.insert(b-1); nodes[b-1].incoming_love.insert(a-1); break; case 'N': nodes[a-1].incoming_hate.insert(b-1); nodes[b-1].outgoing_hate.insert(a-1); break; } } // Step 2 - select CEO int ceo_idx = -1; for (smallnum i = 0; i < n; i++) { node& n = nodes[i]; if ((n.outgoing_love.size() + n.outgoing_hate.size()) == 0) { ceo_idx = i; break; } } if (ceo_idx == -1) { // Exit if no CEO material exists printf("NIE\n"); return 0; } // Special CEO parent - no parent node& ceo_node = nodes[ceo_idx]; ceo_node.tree_parent = -1; // printf("DEBUG - CEO EXISTS = %d\n", ceo_idx); // Step 2.5 - we need to delete love to CEO for (smallset::iterator i = ceo_node.incoming_love.begin(); i != ceo_node.incoming_love.end(); i++) { node& found_node = nodes[*i]; found_node.outgoing_love.erase(ceo_idx); } // Step 3 - put good nodes in the queue smallqueue nodes_to_process; // Nodes that we can process for (smallnum node_idx = 0; node_idx < n; node_idx++) { node& current_node = nodes[node_idx]; if (current_node.tree_parent != -2) { // Node already done, ignore -- Just in the CEO case so far continue; } // Node has incoming love, ignore for now if (current_node.incoming_love.size() > 0) { continue; } // Node has outgoing love and no incoming love, add it to the queue // printf("DEBUG - PUSH TO QUEUE %u\n", node_idx); nodes_to_process.push_back(node_idx); } // Step 4 - main processing while (!nodes_to_process.empty()) { smallnum node_idx = nodes_to_process.front(); nodes_to_process.pop_front(); // printf("DEBUG - QUEUE PROCESSING %u\n", node_idx); // Step 3.1 - select the node to process now // We process nodes from the "bottom" so only nodes with no incoming love. // Nodes with incoming love, will be processed in their turn. node& current_node = nodes[node_idx]; // Just in case, shouldn't happen if (current_node.tree_parent != -2) { // Node already done, ignore continue; } // No outgoing love, attach to CEO if (current_node.outgoing_love.size() == 0) { current_node.tree_parent = ceo_idx; // printf("DEBUG - ATTACH TO CEO %u\n", node_idx); continue; } // We have a node with outgoing love. // What we need to do is select the node from outgoing love set, // That is not reachable by any edge distance from this set // Before BFS - new bfs distances, all initialized to -1 std::vector<int> bfs_distance(n, -1); // BFS DISTANCE - -1 not visited, not put in the queue // BFS DISTANCE - 0 - put in the queue, reached without going through hate branches // BFS DISTANCE - 1 - put in the queue, reached with going through hate branches once // BFS DISTANCE - 2 - processed and done bfs_distance[node_idx] = 0; // Probably doesn't matter what we put here smallset& original_set = current_node.outgoing_love; // just an alias, no copy smallset candidate_set = current_node.outgoing_love; // set of candidates that are good, a copy // Remove personal hate from the candidate set for (smallset::iterator iter = current_node.incoming_hate.begin(); iter != current_node.incoming_hate.end(); iter++) { candidate_set.erase(*iter); // Generally if that happens, we should just quit } // printf("DEBUG - outgoing love"); // debug_print_set(current_node.outgoing_love); // printf("DEBUG - incoming hate"); // debug_print_set(current_node.incoming_hate); std::deque<smallnum> bfs_queue; // Push original set elements to the queue for (smallset::iterator iter = original_set.begin(); iter != original_set.end(); iter++) { bfs_distance[*iter] = 0; bfs_queue.push_back(*iter); } while (!bfs_queue.empty()) { // printf("DEBUG - candidate set\n"); // debug_print_set(candidate_set); smallnum bfs_candidate = bfs_queue.front(); bfs_queue.pop_front(); smallnum candidate_distance = bfs_distance[bfs_candidate]; if (candidate_distance >= 2) { // Guy already processed continue; } // printf("BFS - process %u\n", bfs_candidate); node& visited_node = nodes[bfs_candidate]; // printf("BFS candidate - outgoing love\n"); // debug_print_set(visited_node.outgoing_love); for(smallset::iterator iter = visited_node.outgoing_love.begin(); iter != visited_node.outgoing_love.end(); iter++) { candidate_set.erase(*iter); int new_item_distance = bfs_distance[*iter]; if (new_item_distance == -1) { bfs_distance[*iter] = candidate_distance; if (candidate_distance == 0) { bfs_queue.push_front(*iter); } else { bfs_queue.push_back(*iter); } } // DO NOTHING // if (new_item_distance == 0) {} // if (new_item_distance == 2) {} if (new_item_distance == 1 && candidate_distance == 0) { bfs_distance[*iter] = candidate_distance; bfs_queue.push_front(*iter); } } // printf("BFS candidate - outgoing hate\n"); // debug_print_set(visited_node.outgoing_hate); // If candidate distance is 1, then don't do that part at all if (candidate_distance < 1) { for(smallset::iterator iter = visited_node.outgoing_hate.begin(); iter != visited_node.outgoing_hate.end(); iter++) { candidate_set.erase(*iter); int new_item_distance = bfs_distance[*iter]; if (new_item_distance == -1) { bfs_distance[*iter] = 1; bfs_queue.push_back(*iter); } } } bfs_distance[bfs_candidate] = 2; } // printf("XXX"); // printf("DEBUG - candidate set\n"); // debug_print_set(candidate_set); if (candidate_set.empty()) { // printf("DEBUG - NO SOLUTION FOUND in BFS\n"); printf("NIE\n"); return 0; } smallnum parent_idx = *candidate_set.begin(); // printf("DEBUG - PARENT SET TO = %u\n", parent_idx); current_node.tree_parent = parent_idx; // Update parent love and hate node& parent = nodes[parent_idx]; parent.incoming_love.erase(node_idx); // We overwrite the arrows // parent.outgoing_love.insert(current_node.outgoing_love.begin(), current_node.outgoing_love.end()); // parent.outgoing_love.erase(parent_idx); // in case it was put there for (smallset::iterator iter = current_node.outgoing_love.begin(); iter != current_node.outgoing_love.end(); iter++) { if (*iter != parent_idx) { node& other_node = nodes[*iter]; parent.outgoing_love.insert(*iter); other_node.incoming_love.erase(node_idx); other_node.incoming_love.insert(parent_idx); } } for (smallset::iterator iter = current_node.incoming_hate.begin(); iter != current_node.incoming_hate.end(); iter++) { if (*iter != parent_idx) { node& other_node = nodes[*iter]; parent.incoming_hate.insert(*iter); other_node.outgoing_hate.erase(node_idx); other_node.outgoing_hate.insert(parent_idx); } } // parent.incoming_hate.insert(current_node.incoming_hate.begin(), current_node.incoming_hate.end()); // parent.incoming_hate.erase(parent_idx); // in case it was put there if ((parent.tree_parent == -2) && (parent.incoming_love.size() == 0)) { // printf("DEBUG - PUSH TO QUEUE %u\n", parent_idx); nodes_to_process.push_back(parent_idx); } } for (smallnum node_idx = 0; node_idx < n; node_idx++) { if (nodes[node_idx].tree_parent == -2) { // Some nodes were not allocated, probably because of cycle in love // printf("DEBUG - NO SOLUTION FOUND\n"); printf("NIE\n"); return 0; } } // printf("================ SOLUTION - TAKTAKTAK ===========\n"); for(smallnum node_idx = 0; node_idx < n; node_idx++) { printf("%u\n", nodes[node_idx].tree_parent + 1); } 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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | #include <cstdio> #include <set> #include <vector> #include <deque> typedef unsigned int smallnum; // typedef unsigned char mininum; typedef std::set<smallnum> smallset; typedef std::deque<smallnum> smallqueue; class node { public: smallnum number; int tree_parent; smallset incoming_love; smallset outgoing_love; smallset incoming_hate; smallset outgoing_hate; }; void debug_print_set(const smallset s) { printf("set("); for (smallset::iterator iter = s.begin(); iter != s.end(); iter++) { printf("%u,", *iter); } printf(")\n"); } int main() { // Step 1 - Get data smallnum n, m; scanf("%u %u\n", &n, &m); // Set up nodes structure std::vector<node> nodes(n); for (smallnum i = 0; i < n; i++) { nodes[i].number = i; nodes[i].tree_parent = -2; } for (smallnum i = 0; i < m; i++) { smallnum a, b; char c; scanf("%u %u %c\n", &a, &b, &c); // We readress nodes to k - 1, so that they match array indices switch(c) { case 'T': nodes[a-1].outgoing_love.insert(b-1); nodes[b-1].incoming_love.insert(a-1); break; case 'N': nodes[a-1].incoming_hate.insert(b-1); nodes[b-1].outgoing_hate.insert(a-1); break; } } // Step 2 - select CEO int ceo_idx = -1; for (smallnum i = 0; i < n; i++) { node& n = nodes[i]; if ((n.outgoing_love.size() + n.outgoing_hate.size()) == 0) { ceo_idx = i; break; } } if (ceo_idx == -1) { // Exit if no CEO material exists printf("NIE\n"); return 0; } // Special CEO parent - no parent node& ceo_node = nodes[ceo_idx]; ceo_node.tree_parent = -1; // printf("DEBUG - CEO EXISTS = %d\n", ceo_idx); // Step 2.5 - we need to delete love to CEO for (smallset::iterator i = ceo_node.incoming_love.begin(); i != ceo_node.incoming_love.end(); i++) { node& found_node = nodes[*i]; found_node.outgoing_love.erase(ceo_idx); } // Step 3 - put good nodes in the queue smallqueue nodes_to_process; // Nodes that we can process for (smallnum node_idx = 0; node_idx < n; node_idx++) { node& current_node = nodes[node_idx]; if (current_node.tree_parent != -2) { // Node already done, ignore -- Just in the CEO case so far continue; } // Node has incoming love, ignore for now if (current_node.incoming_love.size() > 0) { continue; } // Node has outgoing love and no incoming love, add it to the queue // printf("DEBUG - PUSH TO QUEUE %u\n", node_idx); nodes_to_process.push_back(node_idx); } // Step 4 - main processing while (!nodes_to_process.empty()) { smallnum node_idx = nodes_to_process.front(); nodes_to_process.pop_front(); // printf("DEBUG - QUEUE PROCESSING %u\n", node_idx); // Step 3.1 - select the node to process now // We process nodes from the "bottom" so only nodes with no incoming love. // Nodes with incoming love, will be processed in their turn. node& current_node = nodes[node_idx]; // Just in case, shouldn't happen if (current_node.tree_parent != -2) { // Node already done, ignore continue; } // No outgoing love, attach to CEO if (current_node.outgoing_love.size() == 0) { current_node.tree_parent = ceo_idx; // printf("DEBUG - ATTACH TO CEO %u\n", node_idx); continue; } // We have a node with outgoing love. // What we need to do is select the node from outgoing love set, // That is not reachable by any edge distance from this set // Before BFS - new bfs distances, all initialized to -1 std::vector<int> bfs_distance(n, -1); // BFS DISTANCE - -1 not visited, not put in the queue // BFS DISTANCE - 0 - put in the queue, reached without going through hate branches // BFS DISTANCE - 1 - put in the queue, reached with going through hate branches once // BFS DISTANCE - 2 - processed and done bfs_distance[node_idx] = 0; // Probably doesn't matter what we put here smallset& original_set = current_node.outgoing_love; // just an alias, no copy smallset candidate_set = current_node.outgoing_love; // set of candidates that are good, a copy // Remove personal hate from the candidate set for (smallset::iterator iter = current_node.incoming_hate.begin(); iter != current_node.incoming_hate.end(); iter++) { candidate_set.erase(*iter); // Generally if that happens, we should just quit } // printf("DEBUG - outgoing love"); // debug_print_set(current_node.outgoing_love); // printf("DEBUG - incoming hate"); // debug_print_set(current_node.incoming_hate); std::deque<smallnum> bfs_queue; // Push original set elements to the queue for (smallset::iterator iter = original_set.begin(); iter != original_set.end(); iter++) { bfs_distance[*iter] = 0; bfs_queue.push_back(*iter); } while (!bfs_queue.empty()) { // printf("DEBUG - candidate set\n"); // debug_print_set(candidate_set); smallnum bfs_candidate = bfs_queue.front(); bfs_queue.pop_front(); smallnum candidate_distance = bfs_distance[bfs_candidate]; if (candidate_distance >= 2) { // Guy already processed continue; } // printf("BFS - process %u\n", bfs_candidate); node& visited_node = nodes[bfs_candidate]; // printf("BFS candidate - outgoing love\n"); // debug_print_set(visited_node.outgoing_love); for(smallset::iterator iter = visited_node.outgoing_love.begin(); iter != visited_node.outgoing_love.end(); iter++) { candidate_set.erase(*iter); int new_item_distance = bfs_distance[*iter]; if (new_item_distance == -1) { bfs_distance[*iter] = candidate_distance; if (candidate_distance == 0) { bfs_queue.push_front(*iter); } else { bfs_queue.push_back(*iter); } } // DO NOTHING // if (new_item_distance == 0) {} // if (new_item_distance == 2) {} if (new_item_distance == 1 && candidate_distance == 0) { bfs_distance[*iter] = candidate_distance; bfs_queue.push_front(*iter); } } // printf("BFS candidate - outgoing hate\n"); // debug_print_set(visited_node.outgoing_hate); // If candidate distance is 1, then don't do that part at all if (candidate_distance < 1) { for(smallset::iterator iter = visited_node.outgoing_hate.begin(); iter != visited_node.outgoing_hate.end(); iter++) { candidate_set.erase(*iter); int new_item_distance = bfs_distance[*iter]; if (new_item_distance == -1) { bfs_distance[*iter] = 1; bfs_queue.push_back(*iter); } } } bfs_distance[bfs_candidate] = 2; } // printf("XXX"); // printf("DEBUG - candidate set\n"); // debug_print_set(candidate_set); if (candidate_set.empty()) { // printf("DEBUG - NO SOLUTION FOUND in BFS\n"); printf("NIE\n"); return 0; } smallnum parent_idx = *candidate_set.begin(); // printf("DEBUG - PARENT SET TO = %u\n", parent_idx); current_node.tree_parent = parent_idx; // Update parent love and hate node& parent = nodes[parent_idx]; parent.incoming_love.erase(node_idx); // We overwrite the arrows // parent.outgoing_love.insert(current_node.outgoing_love.begin(), current_node.outgoing_love.end()); // parent.outgoing_love.erase(parent_idx); // in case it was put there for (smallset::iterator iter = current_node.outgoing_love.begin(); iter != current_node.outgoing_love.end(); iter++) { if (*iter != parent_idx) { node& other_node = nodes[*iter]; parent.outgoing_love.insert(*iter); other_node.incoming_love.erase(node_idx); other_node.incoming_love.insert(parent_idx); } } for (smallset::iterator iter = current_node.incoming_hate.begin(); iter != current_node.incoming_hate.end(); iter++) { if (*iter != parent_idx) { node& other_node = nodes[*iter]; parent.incoming_hate.insert(*iter); other_node.outgoing_hate.erase(node_idx); other_node.outgoing_hate.insert(parent_idx); } } // parent.incoming_hate.insert(current_node.incoming_hate.begin(), current_node.incoming_hate.end()); // parent.incoming_hate.erase(parent_idx); // in case it was put there if ((parent.tree_parent == -2) && (parent.incoming_love.size() == 0)) { // printf("DEBUG - PUSH TO QUEUE %u\n", parent_idx); nodes_to_process.push_back(parent_idx); } } for (smallnum node_idx = 0; node_idx < n; node_idx++) { if (nodes[node_idx].tree_parent == -2) { // Some nodes were not allocated, probably because of cycle in love // printf("DEBUG - NO SOLUTION FOUND\n"); printf("NIE\n"); return 0; } } // printf("================ SOLUTION - TAKTAKTAK ===========\n"); for(smallnum node_idx = 0; node_idx < n; node_idx++) { printf("%u\n", nodes[node_idx].tree_parent + 1); } return 0; } |