// #define DEBUG #ifdef DEBUG #define D(...) __VA_ARGS__ #else #define D(...) do {} while(0) #define NDEBUG #endif #include <iostream> #include <random> #include <map> #include <set> #include <array> #include <assert.h> #include "message.h" #include "kollib.h" using namespace std; map<int, int> checkpoints; set<int> targets; map<int, int> found_targets; struct Neighbour { int neighbour; int distance; }; struct Node { array<Neighbour,2> neighbours; int distance; int visited; int dir; }; vector<Node> nodes; struct TargetDesc { int node; int distance; }; map<int,vector<TargetDesc>> target_descs; typedef int (*StepFunction)(int); struct SidewaysGoer { int pos, ppos, pppos, distance, direction, neighbour; StepFunction primary, secondary; SidewaysGoer(int start, int direction) : pos (start) , ppos (0) , pppos (0) , distance (0) , direction(direction) , neighbour(0) , primary (direction == 1 ? FirstNeighbor : SecondNeighbor) , secondary(direction == -1 ? FirstNeighbor : SecondNeighbor) { assert(direction == -1 || direction == 1); check_targets(); } bool arrived() { return neighbour != 0; } void check_checkpoints() { auto cp = checkpoints.find(pos); if(cp != checkpoints.end()) { D(cerr << "arrived at checkpoint of node " << cp->second << endl); neighbour = cp->second; } } void check_targets() { auto src = targets.find(pos); if(src != targets.end()) { D(cerr << "found target " << pos << endl); found_targets[pos] = distance; } } void go() { pppos = ppos; ppos = pos; pos = primary(ppos); assert(pos != ppos); if(pos == pppos) { pos = secondary(ppos); swap(primary, secondary); } D(cerr << "goer " << direction << "\t" << pppos << " " << ppos << " " << pos << endl); distance += direction; check_targets(); check_checkpoints(); } }; int main(int argc, char **argv) { ios_base::sync_with_stdio(false); int seed; int me = MyNodeId(); int n_nodes = NumberOfNodes(); int n_students = NumberOfStudents(); int n_queries = NumberOfQueries(); if(me == 0) { random_device rd; mt19937 mt(rd()); uniform_int_distribution<int> d(1, n_students); int i; for(i=1; i<n_nodes; i++) { int first_s = d(mt); D(cerr << "node " << i << " maybe will have " << first_s << endl); bool first = true, awarded = false; int s; for(s = first_s; !awarded && (s != first_s || first); s=(s%n_students)+1) { first = false; auto &cp = checkpoints[s]; if(cp == 0) { D(cerr << "node " << i << " has got " << s << endl); cp = i; awarded = true; } } if(!awarded) { // this will happen only if there are more worker nodes than students break; } } int active_worker_nodes = i - 1; // remember how many nodes are active assert(active_worker_nodes <= n_students); // and assert the above assumption for(int i=1; i<n_nodes; i++) { PutInt(i, checkpoints.size()); for(auto &cp: checkpoints) { PutInt(i, cp.first); PutInt(i, cp.second); } Send(i); } nodes.resize(active_worker_nodes + 1); D(cerr << "active worker nodes: " << active_worker_nodes << endl); for(int i=0; i<active_worker_nodes; i++) { int node = Receive(-1); auto &N = nodes[node].neighbours; assert(N[0].neighbour == 0); assert(N[1].neighbour == 0); N[0].neighbour = GetInt(node); N[0].distance = GetInt(node); N[1].neighbour = GetInt(node); N[1].distance = GetInt(node); D(cerr << "from " << node << " got " << N[0].neighbour << " " << N[0].distance << " " << N[1].neighbour << " " << N[1].distance << endl); int S; S = GetInt(node); for(int j=0; j<S; j++) { int pos = GetInt(node); int distance = GetInt(node); target_descs[pos].push_back({node, distance}); } } D(cerr << "got data back from all active nodes" << endl); int cur_i=1; nodes[cur_i].dir = false; nodes[cur_i].visited = 1; do { D(cerr << "I'm at " << cur_i << endl); auto &cur = nodes[cur_i]; assert(cur.visited == 1); cur.visited = 2; auto &next_n = cur.neighbours[cur.dir]; int next_i = next_n.neighbour; assert(next_i != 0); D(cerr << "going to " << next_i << endl); auto &next = nodes[next_i]; if(next.neighbours[!cur.dir].neighbour == cur_i) { next.dir = cur.dir; assert(next.neighbours[!cur.dir].distance == -next_n.distance); } else if(next.neighbours[cur.dir].neighbour == cur_i) { next.dir = !cur.dir; assert(next.neighbours[cur.dir].distance == next_n.distance); } else { abort(); } int next_distance = cur.distance + (cur.dir ? -next_n.distance : next_n.distance); if(next_i == 1) { D(cerr << "got back to first node, finishing enumeration" << endl); assert(next.dir == 0); assert(next_distance == n_students); } else { assert(next.visited == 0); next.visited = 1; next.distance = next_distance; } cur_i = next_i; } while(cur_i != 1); map<int,int> distance_to_node; for(int i=1; i<active_worker_nodes+1; i++) { assert(nodes[i].visited == 2); D(cerr << "node " << i << " distance: " << nodes[i].distance << endl); distance_to_node[nodes[i].distance] = i; } #ifdef DEBUG for(auto &t: target_descs) { auto &S = t.second; int pd; for(int j=0; j<S.size(); j++) { int d = nodes[S[0].node].distance + (nodes[S[0].node].dir ? -S[0].distance : S[0].distance); if(j > 1) { assert(pd == d); } pd = d; } } #endif for(int i=1; i<=n_queries; i++) { auto &S = target_descs[QueryFrom(i)]; auto &D = target_descs[QueryTo(i)]; assert(!S.empty()); assert(!D.empty()); int sd = nodes[S[0].node].distance + (nodes[S[0].node].dir ? -S[0].distance : S[0].distance); int dd = nodes[D[0].node].distance + (nodes[D[0].node].dir ? -D[0].distance : D[0].distance); int diff = (sd - dd + n_students) % n_students; diff = min(diff, n_students - diff); cout << diff << endl; } } else { Receive(0); int n_checkpoints; n_checkpoints = GetInt(0); int my_start = 0; for(int i=0; i<n_checkpoints; i++) { int v, n; v = GetInt(0); n = GetInt(0); checkpoints[v] = n; if(n == me) { my_start = v; } } if(my_start == 0) { D(cerr << "nothing to do for me" << endl); return 0; } for(int i=NumberOfQueries(); i>0; i--) { targets.insert(QueryFrom(i)); targets.insert(QueryTo(i)); } D(cerr << "starting from " << my_start << endl); SidewaysGoer a(my_start, 1), b(my_start, -1); while(!a.arrived() && !b.arrived()) { a.go(); b.go(); } while(!a.arrived()) { a.go(); } while(!b.arrived()) { b.go(); } assert(a.distance > 0); assert(b.distance < 0); PutInt(0, a.neighbour); PutInt(0, a.distance); PutInt(0, b.neighbour); PutInt(0, b.distance); PutInt(0, found_targets.size()); for(auto &s: found_targets) { PutInt(0, s.first); // position PutInt(0, s.second); // distance } Send(0); } 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 316 317 318 319 320 321 322 323 324 325 | // #define DEBUG #ifdef DEBUG #define D(...) __VA_ARGS__ #else #define D(...) do {} while(0) #define NDEBUG #endif #include <iostream> #include <random> #include <map> #include <set> #include <array> #include <assert.h> #include "message.h" #include "kollib.h" using namespace std; map<int, int> checkpoints; set<int> targets; map<int, int> found_targets; struct Neighbour { int neighbour; int distance; }; struct Node { array<Neighbour,2> neighbours; int distance; int visited; int dir; }; vector<Node> nodes; struct TargetDesc { int node; int distance; }; map<int,vector<TargetDesc>> target_descs; typedef int (*StepFunction)(int); struct SidewaysGoer { int pos, ppos, pppos, distance, direction, neighbour; StepFunction primary, secondary; SidewaysGoer(int start, int direction) : pos (start) , ppos (0) , pppos (0) , distance (0) , direction(direction) , neighbour(0) , primary (direction == 1 ? FirstNeighbor : SecondNeighbor) , secondary(direction == -1 ? FirstNeighbor : SecondNeighbor) { assert(direction == -1 || direction == 1); check_targets(); } bool arrived() { return neighbour != 0; } void check_checkpoints() { auto cp = checkpoints.find(pos); if(cp != checkpoints.end()) { D(cerr << "arrived at checkpoint of node " << cp->second << endl); neighbour = cp->second; } } void check_targets() { auto src = targets.find(pos); if(src != targets.end()) { D(cerr << "found target " << pos << endl); found_targets[pos] = distance; } } void go() { pppos = ppos; ppos = pos; pos = primary(ppos); assert(pos != ppos); if(pos == pppos) { pos = secondary(ppos); swap(primary, secondary); } D(cerr << "goer " << direction << "\t" << pppos << " " << ppos << " " << pos << endl); distance += direction; check_targets(); check_checkpoints(); } }; int main(int argc, char **argv) { ios_base::sync_with_stdio(false); int seed; int me = MyNodeId(); int n_nodes = NumberOfNodes(); int n_students = NumberOfStudents(); int n_queries = NumberOfQueries(); if(me == 0) { random_device rd; mt19937 mt(rd()); uniform_int_distribution<int> d(1, n_students); int i; for(i=1; i<n_nodes; i++) { int first_s = d(mt); D(cerr << "node " << i << " maybe will have " << first_s << endl); bool first = true, awarded = false; int s; for(s = first_s; !awarded && (s != first_s || first); s=(s%n_students)+1) { first = false; auto &cp = checkpoints[s]; if(cp == 0) { D(cerr << "node " << i << " has got " << s << endl); cp = i; awarded = true; } } if(!awarded) { // this will happen only if there are more worker nodes than students break; } } int active_worker_nodes = i - 1; // remember how many nodes are active assert(active_worker_nodes <= n_students); // and assert the above assumption for(int i=1; i<n_nodes; i++) { PutInt(i, checkpoints.size()); for(auto &cp: checkpoints) { PutInt(i, cp.first); PutInt(i, cp.second); } Send(i); } nodes.resize(active_worker_nodes + 1); D(cerr << "active worker nodes: " << active_worker_nodes << endl); for(int i=0; i<active_worker_nodes; i++) { int node = Receive(-1); auto &N = nodes[node].neighbours; assert(N[0].neighbour == 0); assert(N[1].neighbour == 0); N[0].neighbour = GetInt(node); N[0].distance = GetInt(node); N[1].neighbour = GetInt(node); N[1].distance = GetInt(node); D(cerr << "from " << node << " got " << N[0].neighbour << " " << N[0].distance << " " << N[1].neighbour << " " << N[1].distance << endl); int S; S = GetInt(node); for(int j=0; j<S; j++) { int pos = GetInt(node); int distance = GetInt(node); target_descs[pos].push_back({node, distance}); } } D(cerr << "got data back from all active nodes" << endl); int cur_i=1; nodes[cur_i].dir = false; nodes[cur_i].visited = 1; do { D(cerr << "I'm at " << cur_i << endl); auto &cur = nodes[cur_i]; assert(cur.visited == 1); cur.visited = 2; auto &next_n = cur.neighbours[cur.dir]; int next_i = next_n.neighbour; assert(next_i != 0); D(cerr << "going to " << next_i << endl); auto &next = nodes[next_i]; if(next.neighbours[!cur.dir].neighbour == cur_i) { next.dir = cur.dir; assert(next.neighbours[!cur.dir].distance == -next_n.distance); } else if(next.neighbours[cur.dir].neighbour == cur_i) { next.dir = !cur.dir; assert(next.neighbours[cur.dir].distance == next_n.distance); } else { abort(); } int next_distance = cur.distance + (cur.dir ? -next_n.distance : next_n.distance); if(next_i == 1) { D(cerr << "got back to first node, finishing enumeration" << endl); assert(next.dir == 0); assert(next_distance == n_students); } else { assert(next.visited == 0); next.visited = 1; next.distance = next_distance; } cur_i = next_i; } while(cur_i != 1); map<int,int> distance_to_node; for(int i=1; i<active_worker_nodes+1; i++) { assert(nodes[i].visited == 2); D(cerr << "node " << i << " distance: " << nodes[i].distance << endl); distance_to_node[nodes[i].distance] = i; } #ifdef DEBUG for(auto &t: target_descs) { auto &S = t.second; int pd; for(int j=0; j<S.size(); j++) { int d = nodes[S[0].node].distance + (nodes[S[0].node].dir ? -S[0].distance : S[0].distance); if(j > 1) { assert(pd == d); } pd = d; } } #endif for(int i=1; i<=n_queries; i++) { auto &S = target_descs[QueryFrom(i)]; auto &D = target_descs[QueryTo(i)]; assert(!S.empty()); assert(!D.empty()); int sd = nodes[S[0].node].distance + (nodes[S[0].node].dir ? -S[0].distance : S[0].distance); int dd = nodes[D[0].node].distance + (nodes[D[0].node].dir ? -D[0].distance : D[0].distance); int diff = (sd - dd + n_students) % n_students; diff = min(diff, n_students - diff); cout << diff << endl; } } else { Receive(0); int n_checkpoints; n_checkpoints = GetInt(0); int my_start = 0; for(int i=0; i<n_checkpoints; i++) { int v, n; v = GetInt(0); n = GetInt(0); checkpoints[v] = n; if(n == me) { my_start = v; } } if(my_start == 0) { D(cerr << "nothing to do for me" << endl); return 0; } for(int i=NumberOfQueries(); i>0; i--) { targets.insert(QueryFrom(i)); targets.insert(QueryTo(i)); } D(cerr << "starting from " << my_start << endl); SidewaysGoer a(my_start, 1), b(my_start, -1); while(!a.arrived() && !b.arrived()) { a.go(); b.go(); } while(!a.arrived()) { a.go(); } while(!b.arrived()) { b.go(); } assert(a.distance > 0); assert(b.distance < 0); PutInt(0, a.neighbour); PutInt(0, a.distance); PutInt(0, b.neighbour); PutInt(0, b.distance); PutInt(0, found_targets.size()); for(auto &s: found_targets) { PutInt(0, s.first); // position PutInt(0, s.second); // distance } Send(0); } return 0; } |