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
//Author: Artur Hibner
#include "kollib.h"
#include "message.h"
#include <cstdio>

using namespace std;

int steps, to, left, right, previous_left, previous_right;

int main() {
  for (int i = MyNodeId() ; i < NumberOfQueries() ; i += NumberOfNodes()) {
    left = right = previous_left = previous_right = QueryFrom(i+1);
    to = QueryTo(i+1);
    steps = 0;
    while (left != to && right != to) {
      if (FirstNeighbor(left) == previous_left) {
        previous_left = left;
        left = SecondNeighbor(left);
      } else {
        previous_left = left;
        left = FirstNeighbor(left);
      }
      if (SecondNeighbor(right) == previous_right) {
        previous_right = right;
        right = FirstNeighbor(right);
      } else {
        previous_right = right;
        right = SecondNeighbor(right);
      }
      steps++;
    }
    if (MyNodeId() > 0) {
      PutInt(0, steps);
      Send(0);
    } else {
      printf("%d\n", steps);
      for (int j = 1 ; j < NumberOfNodes() && i+j < NumberOfQueries() ; j++) {
        Receive(j);
        printf("%d\n", GetInt(j));
      }
    }
  }
  return 0;
}