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
#include <cstdio>
#include <algorithm>
#include "message.h"
#include "kollib.h"

using namespace std;

int sas[10000000][2], nr[10000000];

int main ()
{
    int n,a,b,c,d,e,f;

    if (MyNodeId() == 0)
    {
        n=NumberOfStudents();

        for (a=1; a<=n; a++)
        {
            sas[a][0]=FirstNeighbor(a);
            sas[a][1]=SecondNeighbor(a);
        }

        b=1;
        nr[1]=b++;
        a=sas[1][0];

        for (; !nr[a]; b++)
        {
            nr[a]=b;

            if (nr[sas[a][0]])
                a=sas[a][1];
            else
                a=sas[a][0];
        }

        f=NumberOfQueries();


        for (c=1; c<=f; c++)
        {
            a=QueryFrom(c);
            b=QueryTo(c);
            a=nr[a];
            b=nr[b];

            if (a<b)
                printf ("%d\n", min(b-a,n-b+a));
            else
                printf ("%d\n", min(a-b,n-a+b));
        }
    }

    return 0;
}