#include "message.h" #include "kollib.h" #include <iostream> #include <string> #include <unordered_set> #include <unordered_map> #include <queue> using namespace std; int main() { int queries = NumberOfQueries(), students = NumberOfStudents(), nodes = NumberOfNodes(); unordered_set<int> uniqueStudents; for (int i = 1; i <= queries; ++i) { uniqueStudents.insert(QueryFrom(i)); uniqueStudents.insert(QueryTo(i)); } // If there is only one unique student or no queries - no need to compute int uniqueStudentsSize = uniqueStudents.size(); if (uniqueStudentsSize <= 1) { if (MyNodeId() == 0) { for (int i = 1; i <= queries; ++i) { cout << 0 << endl; } } return 0; } // Prepare queue of available workers queue<int> workers; for (int i = 1; i < nodes; ++i) { workers.push(i); } // Node 0 is task manager.. if (MyNodeId() == 0) { // Step 0. Prepare graph data structure unordered_map<int, int> numberToIdx; unordered_map<int, int>::const_iterator found; int size = uniqueStudentsSize, pointer = 0, counter = size * 2; int* numbers = new int[size](); int nextIdxs[size]; int prevIdxs[size]; int nextDistances[size]; int prevDistances[size]; // Step 1. Collect topology of whole ring for(unordered_set<int>::iterator it = uniqueStudents.begin(); it != uniqueStudents.end(); ++it) { if (workers.empty()) { int worker = Receive(-1), from = GetInt(worker), to = GetInt(worker), distance = GetInt(worker), fromIdx, toIdx; counter -= 1; found = numberToIdx.find(from); if (found == numberToIdx.end()) { fromIdx = pointer++; numberToIdx[from] = fromIdx; } else { fromIdx = found->second; } found = numberToIdx.find(to); if (found == numberToIdx.end()) { toIdx = pointer++; numberToIdx[to] = toIdx; } else { toIdx = found->second; } if (numbers[fromIdx] == 0) { numbers[fromIdx] = from; nextIdxs[fromIdx] = toIdx; nextDistances[fromIdx] = distance; } else if (nextIdxs[fromIdx] != toIdx || nextDistances[fromIdx] != distance) { prevIdxs[fromIdx] = toIdx; prevDistances[fromIdx] = distance; } if (numbers[toIdx] == 0) { numbers[toIdx] = to; nextIdxs[toIdx] = fromIdx; nextDistances[toIdx] = distance; } else if (nextIdxs[toIdx] != fromIdx || nextDistances[toIdx] != distance) { prevIdxs[toIdx] = fromIdx; prevDistances[toIdx] = distance; } // Go back to work! workers.push(worker); } int worker = workers.front(); workers.pop(); PutInt(worker, *it); PutChar(worker, 'L'); Send(worker); if (workers.empty()) { int worker = Receive(-1), from = GetInt(worker), to = GetInt(worker), distance = GetInt(worker), fromIdx, toIdx; counter -= 1; found = numberToIdx.find(from); if (found == numberToIdx.end()) { fromIdx = pointer++; numberToIdx[from] = fromIdx; } else { fromIdx = found->second; } found = numberToIdx.find(to); if (found == numberToIdx.end()) { toIdx = pointer++; numberToIdx[to] = toIdx; } else { toIdx = found->second; } if (numbers[fromIdx] == 0) { numbers[fromIdx] = from; nextIdxs[fromIdx] = toIdx; nextDistances[fromIdx] = distance; } else if (nextIdxs[fromIdx] != toIdx || nextDistances[fromIdx] != distance) { prevIdxs[fromIdx] = toIdx; prevDistances[fromIdx] = distance; } if (numbers[toIdx] == 0) { numbers[toIdx] = to; nextIdxs[toIdx] = fromIdx; nextDistances[toIdx] = distance; } else if (nextIdxs[toIdx] != fromIdx || nextDistances[toIdx] != distance) { prevIdxs[toIdx] = fromIdx; prevDistances[toIdx] = distance; } // Go back to work! workers.push(worker); } worker = workers.front(); workers.pop(); PutInt(worker, *it); PutChar(worker, 'R'); Send(worker); } // Step 2. Inform remaining worker about end of work while (!workers.empty()) { int worker = workers.front(); workers.pop(); PutInt(worker, -1); Send(worker); } // Step 3. Wait for all workers to finish while (counter > 0) { int worker = Receive(-1), from = GetInt(worker), to = GetInt(worker), distance = GetInt(worker), fromIdx, toIdx; counter -= 1; found = numberToIdx.find(from); if (found == numberToIdx.end()) { fromIdx = pointer++; numberToIdx[from] = fromIdx; } else { fromIdx = found->second; } found = numberToIdx.find(to); if (found == numberToIdx.end()) { toIdx = pointer++; numberToIdx[to] = toIdx; } else { toIdx = found->second; } if (numbers[fromIdx] == 0) { numbers[fromIdx] = from; nextIdxs[fromIdx] = toIdx; nextDistances[fromIdx] = distance; } else if (nextIdxs[fromIdx] != toIdx || nextDistances[fromIdx] != distance) { prevIdxs[fromIdx] = toIdx; prevDistances[fromIdx] = distance; } if (numbers[toIdx] == 0) { numbers[toIdx] = to; nextIdxs[toIdx] = fromIdx; nextDistances[toIdx] = distance; } else if (nextIdxs[toIdx] != fromIdx || nextDistances[toIdx] != distance) { prevIdxs[toIdx] = fromIdx; prevDistances[toIdx] = distance; } // End of work! PutInt(worker, -1); Send(worker); } // Step 4. Calculate cumulative distances int distanceSumsIdxs[uniqueStudentsSize]; int distanceSums[uniqueStudentsSize + 1]; int idx = 0, curr = 0, next = nextIdxs[curr], distance = nextDistances[curr], prev = curr, first; distanceSumsIdxs[idx] = 0; distanceSums[idx] = 0; idx += 1; while (true) { distanceSums[idx] = distanceSums[idx - 1] + distance; if (next == 0) { break; } distanceSumsIdxs[next] = idx; curr = next; first = prevIdxs[curr]; if (first != prev) { next = first; distance = prevDistances[curr]; } else { next = nextIdxs[curr]; distance = nextDistances[curr]; } prev = curr; idx += 1; } // Step 5. Calculate shortest distances based on cumulative sums for (int i = 1; i <= queries; ++i) { int from = QueryFrom(i), to = QueryTo(i); if (from == to) { cout << 0 << endl; } else { int fromIdx, toIdx; found = numberToIdx.find(from); if (found == numberToIdx.end()) { //cout << "Something is wrong.." << endl; return 1; } else { fromIdx = found->second; } found = numberToIdx.find(to); if (found == numberToIdx.end()) { //cout << "Something is wrong.." << endl; return 1; } else { toIdx = found->second; } int fromSumIdx = distanceSumsIdxs[fromIdx], toSumIdx = distanceSumsIdxs[toIdx], d1, d2; if (fromSumIdx < toSumIdx) { d1 = distanceSums[toSumIdx] - distanceSums[fromSumIdx]; d2 = students - d1; } else { d1 = distanceSums[fromSumIdx] - distanceSums[toSumIdx]; d2 = students - d1; } cout << (d1 < d2 ? d1 : d2) << endl; } } } else { unordered_set<int>::iterator end = uniqueStudents.end(); while (true) { Receive(0); int student = GetInt(0); if (student == -1) { // End of work! return 0; } char direction = GetChar(0); int first = FirstNeighbor(student), second = SecondNeighbor(student), next = direction == 'L' ? (first < second ? first : second) : (first > second ? first : second), prev = student, curr = 0, distance = 1; while (true) { if (uniqueStudents.find(next) != end) { PutInt(0, student); PutInt(0, next); PutInt(0, distance); Send(0); break; } distance += 1; curr = next; first = FirstNeighbor(curr); next = first != prev ? first : SecondNeighbor(curr); prev = curr; } } } 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 309 310 311 312 313 314 315 | #include "message.h" #include "kollib.h" #include <iostream> #include <string> #include <unordered_set> #include <unordered_map> #include <queue> using namespace std; int main() { int queries = NumberOfQueries(), students = NumberOfStudents(), nodes = NumberOfNodes(); unordered_set<int> uniqueStudents; for (int i = 1; i <= queries; ++i) { uniqueStudents.insert(QueryFrom(i)); uniqueStudents.insert(QueryTo(i)); } // If there is only one unique student or no queries - no need to compute int uniqueStudentsSize = uniqueStudents.size(); if (uniqueStudentsSize <= 1) { if (MyNodeId() == 0) { for (int i = 1; i <= queries; ++i) { cout << 0 << endl; } } return 0; } // Prepare queue of available workers queue<int> workers; for (int i = 1; i < nodes; ++i) { workers.push(i); } // Node 0 is task manager.. if (MyNodeId() == 0) { // Step 0. Prepare graph data structure unordered_map<int, int> numberToIdx; unordered_map<int, int>::const_iterator found; int size = uniqueStudentsSize, pointer = 0, counter = size * 2; int* numbers = new int[size](); int nextIdxs[size]; int prevIdxs[size]; int nextDistances[size]; int prevDistances[size]; // Step 1. Collect topology of whole ring for(unordered_set<int>::iterator it = uniqueStudents.begin(); it != uniqueStudents.end(); ++it) { if (workers.empty()) { int worker = Receive(-1), from = GetInt(worker), to = GetInt(worker), distance = GetInt(worker), fromIdx, toIdx; counter -= 1; found = numberToIdx.find(from); if (found == numberToIdx.end()) { fromIdx = pointer++; numberToIdx[from] = fromIdx; } else { fromIdx = found->second; } found = numberToIdx.find(to); if (found == numberToIdx.end()) { toIdx = pointer++; numberToIdx[to] = toIdx; } else { toIdx = found->second; } if (numbers[fromIdx] == 0) { numbers[fromIdx] = from; nextIdxs[fromIdx] = toIdx; nextDistances[fromIdx] = distance; } else if (nextIdxs[fromIdx] != toIdx || nextDistances[fromIdx] != distance) { prevIdxs[fromIdx] = toIdx; prevDistances[fromIdx] = distance; } if (numbers[toIdx] == 0) { numbers[toIdx] = to; nextIdxs[toIdx] = fromIdx; nextDistances[toIdx] = distance; } else if (nextIdxs[toIdx] != fromIdx || nextDistances[toIdx] != distance) { prevIdxs[toIdx] = fromIdx; prevDistances[toIdx] = distance; } // Go back to work! workers.push(worker); } int worker = workers.front(); workers.pop(); PutInt(worker, *it); PutChar(worker, 'L'); Send(worker); if (workers.empty()) { int worker = Receive(-1), from = GetInt(worker), to = GetInt(worker), distance = GetInt(worker), fromIdx, toIdx; counter -= 1; found = numberToIdx.find(from); if (found == numberToIdx.end()) { fromIdx = pointer++; numberToIdx[from] = fromIdx; } else { fromIdx = found->second; } found = numberToIdx.find(to); if (found == numberToIdx.end()) { toIdx = pointer++; numberToIdx[to] = toIdx; } else { toIdx = found->second; } if (numbers[fromIdx] == 0) { numbers[fromIdx] = from; nextIdxs[fromIdx] = toIdx; nextDistances[fromIdx] = distance; } else if (nextIdxs[fromIdx] != toIdx || nextDistances[fromIdx] != distance) { prevIdxs[fromIdx] = toIdx; prevDistances[fromIdx] = distance; } if (numbers[toIdx] == 0) { numbers[toIdx] = to; nextIdxs[toIdx] = fromIdx; nextDistances[toIdx] = distance; } else if (nextIdxs[toIdx] != fromIdx || nextDistances[toIdx] != distance) { prevIdxs[toIdx] = fromIdx; prevDistances[toIdx] = distance; } // Go back to work! workers.push(worker); } worker = workers.front(); workers.pop(); PutInt(worker, *it); PutChar(worker, 'R'); Send(worker); } // Step 2. Inform remaining worker about end of work while (!workers.empty()) { int worker = workers.front(); workers.pop(); PutInt(worker, -1); Send(worker); } // Step 3. Wait for all workers to finish while (counter > 0) { int worker = Receive(-1), from = GetInt(worker), to = GetInt(worker), distance = GetInt(worker), fromIdx, toIdx; counter -= 1; found = numberToIdx.find(from); if (found == numberToIdx.end()) { fromIdx = pointer++; numberToIdx[from] = fromIdx; } else { fromIdx = found->second; } found = numberToIdx.find(to); if (found == numberToIdx.end()) { toIdx = pointer++; numberToIdx[to] = toIdx; } else { toIdx = found->second; } if (numbers[fromIdx] == 0) { numbers[fromIdx] = from; nextIdxs[fromIdx] = toIdx; nextDistances[fromIdx] = distance; } else if (nextIdxs[fromIdx] != toIdx || nextDistances[fromIdx] != distance) { prevIdxs[fromIdx] = toIdx; prevDistances[fromIdx] = distance; } if (numbers[toIdx] == 0) { numbers[toIdx] = to; nextIdxs[toIdx] = fromIdx; nextDistances[toIdx] = distance; } else if (nextIdxs[toIdx] != fromIdx || nextDistances[toIdx] != distance) { prevIdxs[toIdx] = fromIdx; prevDistances[toIdx] = distance; } // End of work! PutInt(worker, -1); Send(worker); } // Step 4. Calculate cumulative distances int distanceSumsIdxs[uniqueStudentsSize]; int distanceSums[uniqueStudentsSize + 1]; int idx = 0, curr = 0, next = nextIdxs[curr], distance = nextDistances[curr], prev = curr, first; distanceSumsIdxs[idx] = 0; distanceSums[idx] = 0; idx += 1; while (true) { distanceSums[idx] = distanceSums[idx - 1] + distance; if (next == 0) { break; } distanceSumsIdxs[next] = idx; curr = next; first = prevIdxs[curr]; if (first != prev) { next = first; distance = prevDistances[curr]; } else { next = nextIdxs[curr]; distance = nextDistances[curr]; } prev = curr; idx += 1; } // Step 5. Calculate shortest distances based on cumulative sums for (int i = 1; i <= queries; ++i) { int from = QueryFrom(i), to = QueryTo(i); if (from == to) { cout << 0 << endl; } else { int fromIdx, toIdx; found = numberToIdx.find(from); if (found == numberToIdx.end()) { //cout << "Something is wrong.." << endl; return 1; } else { fromIdx = found->second; } found = numberToIdx.find(to); if (found == numberToIdx.end()) { //cout << "Something is wrong.." << endl; return 1; } else { toIdx = found->second; } int fromSumIdx = distanceSumsIdxs[fromIdx], toSumIdx = distanceSumsIdxs[toIdx], d1, d2; if (fromSumIdx < toSumIdx) { d1 = distanceSums[toSumIdx] - distanceSums[fromSumIdx]; d2 = students - d1; } else { d1 = distanceSums[fromSumIdx] - distanceSums[toSumIdx]; d2 = students - d1; } cout << (d1 < d2 ? d1 : d2) << endl; } } } else { unordered_set<int>::iterator end = uniqueStudents.end(); while (true) { Receive(0); int student = GetInt(0); if (student == -1) { // End of work! return 0; } char direction = GetChar(0); int first = FirstNeighbor(student), second = SecondNeighbor(student), next = direction == 'L' ? (first < second ? first : second) : (first > second ? first : second), prev = student, curr = 0, distance = 1; while (true) { if (uniqueStudents.find(next) != end) { PutInt(0, student); PutInt(0, next); PutInt(0, distance); Send(0); break; } distance += 1; curr = next; first = FirstNeighbor(curr); next = first != prev ? first : SecondNeighbor(curr); prev = curr; } } } return 0; } |