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
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>

#include "kollib.h"
#include "message.h"

using namespace std;

const int M = 200;
const int N = NumberOfStudents();
const int T = 2E4;

const int NODE = MyNodeId();
const int NODES = NumberOfNodes();
const int QUERIES = NumberOfQueries();
const int MAIN = 4;

struct Path
{
   int from, to;
};

enum State {PRINT = 1, CLOSE = 2, WORK = 3, REQUEST = 4, DONE = 5};
enum Direction {LEFT = 0, RIGHT = 1};

typedef map<int, map<int, int> > Matrix;

set<int> checkpoints;
Path q[M];
Matrix d;

void readQueries()
{
   for(int i = 0; i < QUERIES; i++)
   {
      const Path &p = { QueryFrom(i + 1), QueryTo(i + 1) };
      q[i] = p;
      checkpoints.insert(p.from);
      checkpoints.insert(p.to);
   }

   for(set<int>::const_iterator i = checkpoints.begin(); i != checkpoints.end(); i++)
   {
      d[*i][*i] = 0;
   }
}

State single(const int k, const Direction direction)
{
   if (k)
   {
      const int a = direction == LEFT ? FirstNeighbor(k) : SecondNeighbor(k);
      const int b1 = direction == LEFT ? SecondNeighbor(a) : FirstNeighbor(a);
      const int b2 = direction == LEFT ? FirstNeighbor(a) : SecondNeighbor(a);
      const int b = b1 == k ? b2 : b1;

      d[k][a] = 1;

      if (N == 3)
      {
         d[k][b] = 1;
      }
      else
      {
         for(int i1 = a, i2 = b, p = 1; i1 != k; p++)
         {
            if (p % T == 0)
            {
               PutChar(MAIN, REQUEST);
               Send(MAIN);

               Receive(MAIN);
               if (GetChar(MAIN) == CLOSE)
               {
                  return CLOSE;
               }
            }

            if (checkpoints.find(i1) != checkpoints.end())
            {
               d[k][i1] = p;
            }

            const int j1 = direction == LEFT ? FirstNeighbor(i2) : SecondNeighbor(i2);
            const int j2 = j1 == i1 ? direction == LEFT ? SecondNeighbor(i2) : FirstNeighbor(i2) : j1;

            i1 = i2;
            i2 = j2;
         }

         for(int i = 0; i < QUERIES; i++)
         {
            const int p = abs(d[k][q[i].from] - d[k][q[i].to]);
            d[q[i].from][q[i].to] = p;
         }
      }
   }

   return DONE;
}

void query()
{
   for(int i = 0; i < QUERIES; i++)
   {
      const int p = q[i].from == q[i].to ? 0 : d[q[i].from][q[i].to];
      printf("%d\n", min(p, N - p));
   }
}

void wait()
{
   for(int closed = 0; closed < 4; )
   {
      const int node = Receive(-1);
      const State state = (State)GetChar(node);

      switch(state)
      {
         case REQUEST:
            PutChar(node, closed ? CLOSE : WORK);
            if (closed) closed++;
            break;
         case DONE:
            PutChar(node, closed ? CLOSE : PRINT);
            closed++;
            break;
      }

      Send(node);
   }
}

int main()
{
   readQueries();

   if (N > 1)
   {
      State state;

      switch(MyNodeId())
      {
         case 0:
            state = single(*checkpoints.begin(), LEFT);
            break;
         case 1:
            state = single(*checkpoints.begin(), RIGHT);
            break;
         case 2:
            state = single(*checkpoints.rbegin(), LEFT);
            break;
         case 3:
            state = single(*checkpoints.rbegin(), RIGHT);
            break;
      }

      if (MyNodeId() == MAIN)
      {
         wait();
      }
      else if (state == DONE)
      {
         PutChar(MAIN, DONE);
         Send(MAIN);

         Receive(MAIN);
         if (GetChar(MAIN) == PRINT)
         {
            query();
         }
      }
   }
}