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
#include "kollib.h"
#include "message.h"
#include <iostream>
#include<map>
using namespace std;

int getDist(int nr, const map<int,int> & dist)
{
    map<int,int>::const_iterator it = dist.find(nr);
    if(it == dist.end()) return -1;
    return it->second;
}

int main() {
  int computers = NumberOfNodes();
  int students = NumberOfStudents();
  if(MyNodeId() == 0)
  {
      int numbersPerComp = students/(computers-1);
      int last = students - (computers -2) * numbersPerComp;
      int offset = 0;
      for (int i =1;i<computers;i++)
      {
	  PutInt(i, offset);
	  Send(i);
	  offset += numbersPerComp;
      }
      for (int i=1;i<computers-1;i++)
      {
	  PutInt(i, numbersPerComp);
	  Send(i);
      }
      PutInt(computers-1, last);
      Send(computers-1);

      int nr = 1;
      int next = FirstNeighbor(nr);
      PutInt(1, 1);
      Send(1);
      int ktory=1;
      for (int i =1;i<students; i++)
      {
	  ktory = 1+ i/numbersPerComp;
	  if(ktory >= computers) ktory--;
	  PutInt(ktory, next);
	  Send(ktory);

	  int a = FirstNeighbor(next);
	  int b = SecondNeighbor(next);
	  if(a == nr)
	  {
	      nr = next;
	      next = b;
	  }
	  else
	  {
	      nr =next;
	      next = a;
	  }	  
      }
      int zes = NumberOfQueries();
      for (int j=0;j<zes;j++)
      {
	  int source = QueryFrom(j+1);
	  int target = QueryTo(j+1);
	  
	  for (int i=1;i<computers;i++)
	  {
	      PutInt(i, source);
	      Send(i);
	  }
	  int computer1 = Receive(-1);
	  int dist1 = GetInt(computer1);
	  for (int i=1;i<computers;i++)
	  {
	      PutInt(i, target);
	      Send(i);
	  }
	  int computer2 = Receive(-1);
	  int dist2 = GetInt(computer2);
	  if(dist1>dist2) {swap(dist1,dist2);}
	  printf("%d\n",min(dist2-dist1, dist1+ students - dist2));
      }



  }
  else
  {
      map<int,int> dist;
      Receive(0);

      int offset = GetInt(0);
      Receive(0);
      int count = GetInt(0);
      for (int i=0;i<count;i++)
      {
      	Receive(0);
	int nr = GetInt(0);
	dist[nr] = offset + i;
      }
      int zes = NumberOfQueries();
      for (int i=0;i<2*zes;i++)
      {
	  Receive(0);
	  int a  = GetInt(0);
	  int d = getDist(a, dist);
	  if(d!=-1) 
	  {
	      PutInt(0,d);
	      Send(0);
	  }
      }


  }
  return 0;
}