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
#include<cstdio>
#include<algorithm>
#include "message.h"
#include "kollib.h"
using namespace std;
int n, a, b, i, q, x, l, p, wynik;

int main()
{
  n = NumberOfStudents();
  
  int t[n + 1];
  
  if (MyNodeId() == 0)
  {
    p = 0;
    x = 1;
    
    for (i = 0; i < n; i++)
    {
      a = FirstNeighbor(x);
      b = SecondNeighbor(x);
      if (a != p){t[a] = i; p = x; x = a;}
      else
      if (b != p){t[b] = i; p = x; x = b;}
    }
    
    q = NumberOfQueries();
    
    for(i = 1; i <= q; i++)
    {
      a = QueryFrom(i);
      b = QueryTo(i);
      a = t[a];
      b = t[b];
      wynik = abs(b - a);
      wynik = min(wynik, n - abs(b - a));
      printf("%d\n", wynik);
    }
  }
    
return 0;
}