#include <cstdio> #include <cstdlib> #include <string> #include <vector> #include <algorithm> #include <map> #include <set> #include <queue> #include <time.h> #include "message.h" #include "kollib.h" using namespace std; struct special_dude { int idx_; int offset_; int dir_; special_dude() : idx_(0), offset_(0), dir_(0) {} special_dude(int idx, int offset, int dir) : idx_(idx), offset_(offset), dir_(dir) {} }; struct task_results { int id1_, id2_; int len1_, len2_; vector<special_dude> dudes; }; set<int> is_in_query; vector<pair<int, int> > queries; int main() { // Read all queries. for (int i = 1; i <= NumberOfQueries(); i++) { int from = QueryFrom(i); int to = QueryTo(i); is_in_query.insert(from); is_in_query.insert(to); queries.push_back(make_pair(from, to)); } // Handle special case for n < nodes - 1. if (NumberOfStudents() < NumberOfNodes() - 1) { if (MyNodeId() != 0) { return 0; } map<int, int> offsets; int curr = 1; int prev = -1; for (int i = 0; ; i++) { int next = FirstNeighbor(curr); if (next == prev) { next = SecondNeighbor(curr); } offsets[curr] = i; prev = curr; curr = next; if (curr == 1) { break; } } for (int i = 0; i < queries.size(); i++) { int a = queries[i].first; int b = queries[i].second; //printf("a: %d, b: %d\n", a, b); printf("%d\n", min(abs(offsets[a] - offsets[b]), NumberOfStudents() - abs(offsets[a] - offsets[b]))); } return 0; } // Normal processing: master. if (MyNodeId() == 0) { srand(time(NULL)); // Randomly choose starting points for all tasks. map<int, bool> already_chosen; vector<int> start_ids; start_ids.push_back(-1); for (int i = 0; i < NumberOfNodes() - 1; i++) { int random_idx; do { random_idx = 1 + (rand() % NumberOfStudents()); } while (already_chosen.find(random_idx) != already_chosen.end()); start_ids.push_back(random_idx); already_chosen[random_idx] = true; } // Send information about all starting points to all tasks. for (int i = 1; i < NumberOfNodes(); i++) { for (int j = 1; j < NumberOfNodes(); j++) { PutInt(i, start_ids[j]); } Send(i); } // Wait for all the tasks to respond with their results. map<int, task_results> results; for (int i = 0; i < NumberOfNodes() - 1; i++) { int task = Receive(-1); task_results tr; tr.id1_ = GetInt(task); tr.len1_ = GetInt(task); tr.id2_ = GetInt(task); tr.len2_ = GetInt(task); //printf("task %d responded: it hit %d (len %d) and %d (len %d) (started from %d)\n", task, tr.id1_, tr.len1_, tr.id2_, tr.len2_, start_ids[task]); int targets_found = GetInt(task); for (int j = 0; j < targets_found; j++) { special_dude dd; dd.idx_ = GetInt(task); dd.offset_ = GetInt(task); dd.dir_ = GetInt(task); tr.dudes.push_back(dd); //printf("requested dude %d at offset %d in direction %d\n", dd.idx_, dd.offset_, dd.dir_); } results[task] = tr; } // Calculate offsets for all requested dudes. map<int, int> offsets; int curr = 1; int prev = -1; int base_offset = 0; int curr_dir = 0; do { int next = results[curr].id1_; curr_dir = 0; if (next == prev) { next = results[curr].id2_; curr_dir = 1; } //printf("curr: %d, base offset: %d, curr dir: %d\n", curr, base_offset, curr_dir); for (int i = 0; i < results[curr].dudes.size(); i++) { if (results[curr].dudes[i].dir_ == curr_dir) { offsets[results[curr].dudes[i].idx_] = base_offset + results[curr].dudes[i].offset_; //printf("assigning %d the index %d, as the sum of %d and %d\n", // results[curr].dudes[i].idx_, offsets[results[curr].dudes[i].idx_], // base_offset, results[curr].dudes[i].offset_); } } prev = curr; curr = next; if (results[curr].id1_ == prev) { base_offset += results[curr].len1_ + 1; } else { base_offset += results[curr].len2_ + 1; } } while (start_ids[curr] != start_ids[1]); for (int i = 0; i < queries.size(); i++) { int a = queries[i].first; int b = queries[i].second; printf("%d\n", min(abs(offsets[a] - offsets[b]), NumberOfStudents() - abs(offsets[a] - offsets[b]))); } //for (map<int, int>::iterator it = offsets.begin(); it != offsets.end(); it++) { // printf("offset[%d] = %d\n", it->first, it->second); //} } else /* Normal processing: tasks */ { // Receive information about all start ids from the master. int my_start_id; map<int, int> start_ids; int i; Receive(0); for (i = 1; i <= NumberOfNodes() - 1; i++) { int id = GetInt(0); start_ids[id] = i; if (i == MyNodeId()) { my_start_id = id; } } //printf("[%d] received, my start id: %d\n", MyNodeId(), my_start_id); fflush(stdout); // Get the two of initial neighbours. int first = FirstNeighbor(my_start_id); int second = SecondNeighbor(my_start_id); // Explore the first neighbour until another starting point is found. vector<special_dude> special_dudes; int curr = first; int prev = my_start_id; int end_ids[2], end_tasks[2], end_lens[2]; if (is_in_query.find(my_start_id) != is_in_query.end()) { special_dudes.push_back(special_dude(my_start_id, 0, 0)); special_dudes.push_back(special_dude(my_start_id, 0, 1)); } for (i = 1; start_ids.find(curr) == start_ids.end(); i++) { //printf("[%d] first curr: %d\n", MyNodeId(), curr); fflush(stdout); int next = FirstNeighbor(curr); if (next == prev) { next = SecondNeighbor(curr); } if (is_in_query.find(curr) != is_in_query.end()) { special_dudes.push_back(special_dude(curr, i, 0)); } prev = curr; curr = next; } end_ids[0] = curr; end_tasks[0] = start_ids[curr]; end_lens[0] = i - 1; // Explore the second neighbour until another starting point is found. curr = second; prev = my_start_id; for (i = 1; start_ids.find(curr) == start_ids.end(); i++) { //printf("[%d] second curr: %d\n", MyNodeId(), curr); fflush(stdout); int next = FirstNeighbor(curr); if (next == prev) { next = SecondNeighbor(curr); } if (is_in_query.find(curr) != is_in_query.end()) { special_dudes.push_back(special_dude(curr, i, 1)); } prev = curr; curr = next; } end_ids[1] = curr; end_tasks[1] = start_ids[curr]; end_lens[1] = i - 1; //printf("[%d] done, sending...\n", MyNodeId()); fflush(stdout); // Send information to the master. PutInt(0, end_tasks[0]); PutInt(0, end_lens[0]); PutInt(0, end_tasks[1]); PutInt(0, end_lens[1]); PutInt(0, special_dudes.size()); for (i = 0; i < special_dudes.size(); i++) { PutInt(0, special_dudes[i].idx_); PutInt(0, special_dudes[i].offset_); PutInt(0, special_dudes[i].dir_); } Send(0); //printf("[%d] sent.\n", MyNodeId()); fflush(stdout); } 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 | #include <cstdio> #include <cstdlib> #include <string> #include <vector> #include <algorithm> #include <map> #include <set> #include <queue> #include <time.h> #include "message.h" #include "kollib.h" using namespace std; struct special_dude { int idx_; int offset_; int dir_; special_dude() : idx_(0), offset_(0), dir_(0) {} special_dude(int idx, int offset, int dir) : idx_(idx), offset_(offset), dir_(dir) {} }; struct task_results { int id1_, id2_; int len1_, len2_; vector<special_dude> dudes; }; set<int> is_in_query; vector<pair<int, int> > queries; int main() { // Read all queries. for (int i = 1; i <= NumberOfQueries(); i++) { int from = QueryFrom(i); int to = QueryTo(i); is_in_query.insert(from); is_in_query.insert(to); queries.push_back(make_pair(from, to)); } // Handle special case for n < nodes - 1. if (NumberOfStudents() < NumberOfNodes() - 1) { if (MyNodeId() != 0) { return 0; } map<int, int> offsets; int curr = 1; int prev = -1; for (int i = 0; ; i++) { int next = FirstNeighbor(curr); if (next == prev) { next = SecondNeighbor(curr); } offsets[curr] = i; prev = curr; curr = next; if (curr == 1) { break; } } for (int i = 0; i < queries.size(); i++) { int a = queries[i].first; int b = queries[i].second; //printf("a: %d, b: %d\n", a, b); printf("%d\n", min(abs(offsets[a] - offsets[b]), NumberOfStudents() - abs(offsets[a] - offsets[b]))); } return 0; } // Normal processing: master. if (MyNodeId() == 0) { srand(time(NULL)); // Randomly choose starting points for all tasks. map<int, bool> already_chosen; vector<int> start_ids; start_ids.push_back(-1); for (int i = 0; i < NumberOfNodes() - 1; i++) { int random_idx; do { random_idx = 1 + (rand() % NumberOfStudents()); } while (already_chosen.find(random_idx) != already_chosen.end()); start_ids.push_back(random_idx); already_chosen[random_idx] = true; } // Send information about all starting points to all tasks. for (int i = 1; i < NumberOfNodes(); i++) { for (int j = 1; j < NumberOfNodes(); j++) { PutInt(i, start_ids[j]); } Send(i); } // Wait for all the tasks to respond with their results. map<int, task_results> results; for (int i = 0; i < NumberOfNodes() - 1; i++) { int task = Receive(-1); task_results tr; tr.id1_ = GetInt(task); tr.len1_ = GetInt(task); tr.id2_ = GetInt(task); tr.len2_ = GetInt(task); //printf("task %d responded: it hit %d (len %d) and %d (len %d) (started from %d)\n", task, tr.id1_, tr.len1_, tr.id2_, tr.len2_, start_ids[task]); int targets_found = GetInt(task); for (int j = 0; j < targets_found; j++) { special_dude dd; dd.idx_ = GetInt(task); dd.offset_ = GetInt(task); dd.dir_ = GetInt(task); tr.dudes.push_back(dd); //printf("requested dude %d at offset %d in direction %d\n", dd.idx_, dd.offset_, dd.dir_); } results[task] = tr; } // Calculate offsets for all requested dudes. map<int, int> offsets; int curr = 1; int prev = -1; int base_offset = 0; int curr_dir = 0; do { int next = results[curr].id1_; curr_dir = 0; if (next == prev) { next = results[curr].id2_; curr_dir = 1; } //printf("curr: %d, base offset: %d, curr dir: %d\n", curr, base_offset, curr_dir); for (int i = 0; i < results[curr].dudes.size(); i++) { if (results[curr].dudes[i].dir_ == curr_dir) { offsets[results[curr].dudes[i].idx_] = base_offset + results[curr].dudes[i].offset_; //printf("assigning %d the index %d, as the sum of %d and %d\n", // results[curr].dudes[i].idx_, offsets[results[curr].dudes[i].idx_], // base_offset, results[curr].dudes[i].offset_); } } prev = curr; curr = next; if (results[curr].id1_ == prev) { base_offset += results[curr].len1_ + 1; } else { base_offset += results[curr].len2_ + 1; } } while (start_ids[curr] != start_ids[1]); for (int i = 0; i < queries.size(); i++) { int a = queries[i].first; int b = queries[i].second; printf("%d\n", min(abs(offsets[a] - offsets[b]), NumberOfStudents() - abs(offsets[a] - offsets[b]))); } //for (map<int, int>::iterator it = offsets.begin(); it != offsets.end(); it++) { // printf("offset[%d] = %d\n", it->first, it->second); //} } else /* Normal processing: tasks */ { // Receive information about all start ids from the master. int my_start_id; map<int, int> start_ids; int i; Receive(0); for (i = 1; i <= NumberOfNodes() - 1; i++) { int id = GetInt(0); start_ids[id] = i; if (i == MyNodeId()) { my_start_id = id; } } //printf("[%d] received, my start id: %d\n", MyNodeId(), my_start_id); fflush(stdout); // Get the two of initial neighbours. int first = FirstNeighbor(my_start_id); int second = SecondNeighbor(my_start_id); // Explore the first neighbour until another starting point is found. vector<special_dude> special_dudes; int curr = first; int prev = my_start_id; int end_ids[2], end_tasks[2], end_lens[2]; if (is_in_query.find(my_start_id) != is_in_query.end()) { special_dudes.push_back(special_dude(my_start_id, 0, 0)); special_dudes.push_back(special_dude(my_start_id, 0, 1)); } for (i = 1; start_ids.find(curr) == start_ids.end(); i++) { //printf("[%d] first curr: %d\n", MyNodeId(), curr); fflush(stdout); int next = FirstNeighbor(curr); if (next == prev) { next = SecondNeighbor(curr); } if (is_in_query.find(curr) != is_in_query.end()) { special_dudes.push_back(special_dude(curr, i, 0)); } prev = curr; curr = next; } end_ids[0] = curr; end_tasks[0] = start_ids[curr]; end_lens[0] = i - 1; // Explore the second neighbour until another starting point is found. curr = second; prev = my_start_id; for (i = 1; start_ids.find(curr) == start_ids.end(); i++) { //printf("[%d] second curr: %d\n", MyNodeId(), curr); fflush(stdout); int next = FirstNeighbor(curr); if (next == prev) { next = SecondNeighbor(curr); } if (is_in_query.find(curr) != is_in_query.end()) { special_dudes.push_back(special_dude(curr, i, 1)); } prev = curr; curr = next; } end_ids[1] = curr; end_tasks[1] = start_ids[curr]; end_lens[1] = i - 1; //printf("[%d] done, sending...\n", MyNodeId()); fflush(stdout); // Send information to the master. PutInt(0, end_tasks[0]); PutInt(0, end_lens[0]); PutInt(0, end_tasks[1]); PutInt(0, end_lens[1]); PutInt(0, special_dudes.size()); for (i = 0; i < special_dudes.size(); i++) { PutInt(0, special_dudes[i].idx_); PutInt(0, special_dudes[i].offset_); PutInt(0, special_dudes[i].dir_); } Send(0); //printf("[%d] sent.\n", MyNodeId()); fflush(stdout); } return 0; } |