#include "message.h" #include "skup.h" #include <cstdio> #include <map> #include <vector> #include <algorithm> #include <set> using namespace std; char matrix[109][109]; int nodes; int me; void work(){ // Roughly 10ms. for(volatile int i=0; i<5000000; i++); } void work4ms(){ // Roughly 4ms. for(volatile int i=0; i<2000000; i++); } void ping(int who, int value = 1){ PutChar(who, value); Send(who); } char getping(int who){ who = Receive(who); return GetChar(who); } int main() { nodes = NumberOfNodes(); me = MyNodeId(); for(int master=0; master<3; master++){ if(me == master){ work(); // Broadcast whatever. for(int i=0; i<nodes; i++){ if(i == me) continue; ping(i); } // Receive confirmation. for(int i=0; i<nodes; i++){ if(i == me) continue; getping(i); } } else{ // Broadcast 1, except to master. for(int i=0; i<nodes; i++){ if(i == me || i == master) continue; ping(i); } // Got 1 or 0 (broken connection). for(int i=0; i<nodes; i++){ if(i == me || i == master) continue; matrix[i][me] = getping(i); } // Ping those who have connection to me. for(int i=0; i<nodes; i++){ if(i == me || i == master) continue; if(matrix[i][me]){ ping(i); } } // Receive pings until master interferes. while(1){ int who = Receive(-1); GetChar(who); if(who == master) break; matrix[me][who] = 1; } // Allow master to continue. ping(master); work(); } } vector<int> successors, preceeders; for(int i=0; i<nodes; i++){ if(i == me) continue; if(matrix[me][i]) successors.push_back(i); if(matrix[i][me]) preceeders.push_back(i); } /* printf("Guy %d\n", me); printf("Can message: "); for(auto i: successors){ printf("%2d ", i); } printf("\nCan receive from: "); for(auto i: preceeders){ printf("%2d ", i); } printf("\n"); */ set<int> unpinged_parents(preceeders.begin(), preceeders.end()); set<int> unvisited; int my_state = 0; // 0 - unvisited, 1 - open, 2 - closed. int who_called; int who_i_called; int master = 3; #define printf(...) #define fflush(...) // Begin the rev-dfs if I'm the master. if(me == master){ for(int i=0; i<nodes; i++){ unvisited.insert(i); } my_state = 1; who_called = -1; who_i_called = -1; if(unpinged_parents.empty()){ printf("[M] no children left\n"); fflush(stdout); ping(me); } else{ auto it = unpinged_parents.begin(); who_i_called = *it; ping(*it); printf("[M] pinging child %d\n", *it); fflush(stdout); unpinged_parents.erase(it); } } int master_mode = 0; vector<int> post; while (1) { int who = Receive(-1); int c = GetChar(who); if(me == master){ int first_mode = master_mode; do { if(unvisited.count(who)){ unvisited.erase(who); post.push_back(who); printf("[M%d] got visit-ping from %d\n", master_mode, who); fflush(stdout); if(who == master){ printf("[M%d] Master mode detached\n", master_mode); fflush(stdout); master_mode = 1; who_i_called = master; } } else if(who == who_i_called){ printf("[M%d] child %d got back\n", master_mode, who); fflush(stdout); if(master_mode == 0){ if(unpinged_parents.empty()){ printf("[M%d] no children left, pinging self\n", master_mode); fflush(stdout); ping(me); } else{ auto it = unpinged_parents.begin(); who_i_called = *it; ping(*it, 1); printf("[M%d] pinging child %d\n", master_mode, *it); fflush(stdout); unpinged_parents.erase(it); } } else{ first_mode = master_mode; if(unvisited.empty()){ printf("[M%d] no unvisited left, end\n", master_mode); fflush(stdout); master_mode = 2; break; } else{ auto it = unvisited.begin(); who_i_called = *it; ping(*it, 1); printf("[M%d] pinging unvisited %d\n", master_mode, *it); fflush(stdout); } } } else{ printf("[M%d] This shouldn't happen - got from %d\n", master_mode, who); fflush(stdout); exit(100); } } while(master_mode != first_mode); if(master_mode == 2) break; continue; } switch(my_state){ case 2: { if(who == master && !c){ printf("[%d] woken by master\n", me); fflush(stdout); my_state = 3; break; } printf("[%d] already closed; ping back %d\n", me, who); fflush(stdout); ping(who); } break; case 0: printf("[%d] opened by %d\n", me, who); fflush(stdout); who_called = who; who_i_called = who; my_state = 1; case 1: if(who == who_i_called){ while(1){ if(unpinged_parents.empty()){ printf("[%d] no parents to ping, calling master and closing to %d\n", me, who_called); fflush(stdout); ping(master); work4ms(); ping(who_called); my_state = 2; } else{ auto it = unpinged_parents.begin(); int x = *it; unpinged_parents.erase(x); if(x == who_called) continue; if(x == master) continue; printf("[%d] pinging child %d\n", me, x); fflush(stdout); ping(x); who_i_called = x; } break; } } else{ printf("[%d] cycle jump; ping back %d\n", me, who); fflush(stdout); ping(who); } break; } if(my_state == 3) break; } int root = -1; if(me == master){ for(int i=0; i<nodes; i++){ if(i == me) continue; printf("[M] Waking %d\n", i); fflush(stdout); ping(i, 0); } root = post.back(); printf("[M] Initializing rev-dfs: %d\n", root); fflush(stdout); ping(root, 0); ping(root, 0); printf("Post-order:\n"); for(auto p: post){ printf("%d ", p); } printf("\n"); } work(); size_t ii = 0; int parent = -1; vector<char> got(nodes); map<int, vector<int>> iknow; got[me] = 1; iknow[me].resize(NumberOfCompanies()); for(int i=0; i<NumberOfCompanies(); i++){ iknow[me].push_back(GetShareCost(i)); } int mastersinrow = 0; bool imroot = false; int last = -1; int sameinrow = 0; while(1){ int who = Receive(-1); int c = GetChar(who); if(last == who){ sameinrow++; } else{ sameinrow = 0; } last = who; if(sameinrow == 2){ return 0; } if(who == master && c == 0){ mastersinrow++; } if(mastersinrow == 2){ printf("IMROOT\n"); fflush(stdout); imroot = true; mastersinrow = 3; continue; } if(c==0){ if(parent == -1){ printf("[%d] rev-dfs'd by %d\n", me, who); fflush(stdout); parent = who; } else{ printf("[%d] rev-dfs'd by %d, but has parent already, so send to %d\n", me, who, who); fflush(stdout); PutChar(who, 1); for(int i=0; i<nodes; i++){ PutChar(who, 0); } Send(who); continue; } } else{ vector<int> rd; for(int i=0; i<nodes; i++){ int g = GetChar(who); rd.push_back(g); got[i] |= g; } for(int i=0; i<nodes; i++){ if(rd[i]){ printf("[%d] received data about %d\n", me, i); fflush(stdout); Receive(who); int sz = GetInt(who); iknow[i].resize(sz); for(int j=0; j<sz; j++){ iknow[i][j] = GetInt(who); } } } printf("[%d] got 1 from child %d, now seen: ", me, who); for(int i=0; i<nodes; i++){ if(got[i]){ printf("%d ", i); } } printf("\n"); fflush(stdout); if(imroot && iknow.size() == size_t(nodes)){ printf("Hell yeah\n"); fflush(stdout); break; } } if(ii == preceeders.size()){ printf("[%d] all children done, ping-1 %d\n", me, parent); fflush(stdout); PutChar(parent, 1); for(int i=0; i<nodes; i++){ PutChar(parent, got[i]); } Send(parent); for(int i=0; i<nodes; i++){ if(got[i]){ PutInt(parent, iknow[i].size()); for(auto k: iknow[i]){ PutInt(parent, k); } Send(parent); } } } else{ int ch = preceeders[ii++]; printf("[%d] reving %d\n", me, ch); fflush(stdout); ping(ch, 0); } } for(int i=0; i<nodes; i++){ ping(i, 0); ping(i, 0); ping(i, 0); } vector<int> ev; for(auto kv: iknow){ for(auto v: kv.second){ ev.push_back(v); } } sort(ev.rbegin(), ev.rend()); unsigned long long sum = 0; for(size_t i=0; i<ev.size(); i++){ sum += (i+1)*(unsigned long long)ev[i]; } #undef printf printf("%llu\n", sum); }
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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | #include "message.h" #include "skup.h" #include <cstdio> #include <map> #include <vector> #include <algorithm> #include <set> using namespace std; char matrix[109][109]; int nodes; int me; void work(){ // Roughly 10ms. for(volatile int i=0; i<5000000; i++); } void work4ms(){ // Roughly 4ms. for(volatile int i=0; i<2000000; i++); } void ping(int who, int value = 1){ PutChar(who, value); Send(who); } char getping(int who){ who = Receive(who); return GetChar(who); } int main() { nodes = NumberOfNodes(); me = MyNodeId(); for(int master=0; master<3; master++){ if(me == master){ work(); // Broadcast whatever. for(int i=0; i<nodes; i++){ if(i == me) continue; ping(i); } // Receive confirmation. for(int i=0; i<nodes; i++){ if(i == me) continue; getping(i); } } else{ // Broadcast 1, except to master. for(int i=0; i<nodes; i++){ if(i == me || i == master) continue; ping(i); } // Got 1 or 0 (broken connection). for(int i=0; i<nodes; i++){ if(i == me || i == master) continue; matrix[i][me] = getping(i); } // Ping those who have connection to me. for(int i=0; i<nodes; i++){ if(i == me || i == master) continue; if(matrix[i][me]){ ping(i); } } // Receive pings until master interferes. while(1){ int who = Receive(-1); GetChar(who); if(who == master) break; matrix[me][who] = 1; } // Allow master to continue. ping(master); work(); } } vector<int> successors, preceeders; for(int i=0; i<nodes; i++){ if(i == me) continue; if(matrix[me][i]) successors.push_back(i); if(matrix[i][me]) preceeders.push_back(i); } /* printf("Guy %d\n", me); printf("Can message: "); for(auto i: successors){ printf("%2d ", i); } printf("\nCan receive from: "); for(auto i: preceeders){ printf("%2d ", i); } printf("\n"); */ set<int> unpinged_parents(preceeders.begin(), preceeders.end()); set<int> unvisited; int my_state = 0; // 0 - unvisited, 1 - open, 2 - closed. int who_called; int who_i_called; int master = 3; #define printf(...) #define fflush(...) // Begin the rev-dfs if I'm the master. if(me == master){ for(int i=0; i<nodes; i++){ unvisited.insert(i); } my_state = 1; who_called = -1; who_i_called = -1; if(unpinged_parents.empty()){ printf("[M] no children left\n"); fflush(stdout); ping(me); } else{ auto it = unpinged_parents.begin(); who_i_called = *it; ping(*it); printf("[M] pinging child %d\n", *it); fflush(stdout); unpinged_parents.erase(it); } } int master_mode = 0; vector<int> post; while (1) { int who = Receive(-1); int c = GetChar(who); if(me == master){ int first_mode = master_mode; do { if(unvisited.count(who)){ unvisited.erase(who); post.push_back(who); printf("[M%d] got visit-ping from %d\n", master_mode, who); fflush(stdout); if(who == master){ printf("[M%d] Master mode detached\n", master_mode); fflush(stdout); master_mode = 1; who_i_called = master; } } else if(who == who_i_called){ printf("[M%d] child %d got back\n", master_mode, who); fflush(stdout); if(master_mode == 0){ if(unpinged_parents.empty()){ printf("[M%d] no children left, pinging self\n", master_mode); fflush(stdout); ping(me); } else{ auto it = unpinged_parents.begin(); who_i_called = *it; ping(*it, 1); printf("[M%d] pinging child %d\n", master_mode, *it); fflush(stdout); unpinged_parents.erase(it); } } else{ first_mode = master_mode; if(unvisited.empty()){ printf("[M%d] no unvisited left, end\n", master_mode); fflush(stdout); master_mode = 2; break; } else{ auto it = unvisited.begin(); who_i_called = *it; ping(*it, 1); printf("[M%d] pinging unvisited %d\n", master_mode, *it); fflush(stdout); } } } else{ printf("[M%d] This shouldn't happen - got from %d\n", master_mode, who); fflush(stdout); exit(100); } } while(master_mode != first_mode); if(master_mode == 2) break; continue; } switch(my_state){ case 2: { if(who == master && !c){ printf("[%d] woken by master\n", me); fflush(stdout); my_state = 3; break; } printf("[%d] already closed; ping back %d\n", me, who); fflush(stdout); ping(who); } break; case 0: printf("[%d] opened by %d\n", me, who); fflush(stdout); who_called = who; who_i_called = who; my_state = 1; case 1: if(who == who_i_called){ while(1){ if(unpinged_parents.empty()){ printf("[%d] no parents to ping, calling master and closing to %d\n", me, who_called); fflush(stdout); ping(master); work4ms(); ping(who_called); my_state = 2; } else{ auto it = unpinged_parents.begin(); int x = *it; unpinged_parents.erase(x); if(x == who_called) continue; if(x == master) continue; printf("[%d] pinging child %d\n", me, x); fflush(stdout); ping(x); who_i_called = x; } break; } } else{ printf("[%d] cycle jump; ping back %d\n", me, who); fflush(stdout); ping(who); } break; } if(my_state == 3) break; } int root = -1; if(me == master){ for(int i=0; i<nodes; i++){ if(i == me) continue; printf("[M] Waking %d\n", i); fflush(stdout); ping(i, 0); } root = post.back(); printf("[M] Initializing rev-dfs: %d\n", root); fflush(stdout); ping(root, 0); ping(root, 0); printf("Post-order:\n"); for(auto p: post){ printf("%d ", p); } printf("\n"); } work(); size_t ii = 0; int parent = -1; vector<char> got(nodes); map<int, vector<int>> iknow; got[me] = 1; iknow[me].resize(NumberOfCompanies()); for(int i=0; i<NumberOfCompanies(); i++){ iknow[me].push_back(GetShareCost(i)); } int mastersinrow = 0; bool imroot = false; int last = -1; int sameinrow = 0; while(1){ int who = Receive(-1); int c = GetChar(who); if(last == who){ sameinrow++; } else{ sameinrow = 0; } last = who; if(sameinrow == 2){ return 0; } if(who == master && c == 0){ mastersinrow++; } if(mastersinrow == 2){ printf("IMROOT\n"); fflush(stdout); imroot = true; mastersinrow = 3; continue; } if(c==0){ if(parent == -1){ printf("[%d] rev-dfs'd by %d\n", me, who); fflush(stdout); parent = who; } else{ printf("[%d] rev-dfs'd by %d, but has parent already, so send to %d\n", me, who, who); fflush(stdout); PutChar(who, 1); for(int i=0; i<nodes; i++){ PutChar(who, 0); } Send(who); continue; } } else{ vector<int> rd; for(int i=0; i<nodes; i++){ int g = GetChar(who); rd.push_back(g); got[i] |= g; } for(int i=0; i<nodes; i++){ if(rd[i]){ printf("[%d] received data about %d\n", me, i); fflush(stdout); Receive(who); int sz = GetInt(who); iknow[i].resize(sz); for(int j=0; j<sz; j++){ iknow[i][j] = GetInt(who); } } } printf("[%d] got 1 from child %d, now seen: ", me, who); for(int i=0; i<nodes; i++){ if(got[i]){ printf("%d ", i); } } printf("\n"); fflush(stdout); if(imroot && iknow.size() == size_t(nodes)){ printf("Hell yeah\n"); fflush(stdout); break; } } if(ii == preceeders.size()){ printf("[%d] all children done, ping-1 %d\n", me, parent); fflush(stdout); PutChar(parent, 1); for(int i=0; i<nodes; i++){ PutChar(parent, got[i]); } Send(parent); for(int i=0; i<nodes; i++){ if(got[i]){ PutInt(parent, iknow[i].size()); for(auto k: iknow[i]){ PutInt(parent, k); } Send(parent); } } } else{ int ch = preceeders[ii++]; printf("[%d] reving %d\n", me, ch); fflush(stdout); ping(ch, 0); } } for(int i=0; i<nodes; i++){ ping(i, 0); ping(i, 0); ping(i, 0); } vector<int> ev; for(auto kv: iknow){ for(auto v: kv.second){ ev.push_back(v); } } sort(ev.rbegin(), ev.rend()); unsigned long long sum = 0; for(size_t i=0; i<ev.size(); i++){ sum += (i+1)*(unsigned long long)ev[i]; } #undef printf printf("%llu\n", sum); } |