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

using namespace std;

int main()
{
    int t = NumberOfStudents();
    int tab[t + 1];
    tab[1] = 1;
    int q = FirstNeighbor( 1 );
    for( int i = 2; i <= t; i ++ )
    {
        if( FirstNeighbor( q ) != tab[i-1] ) { tab[ FirstNeighbor(q) ] = i; q = FirstNeighbor(q); }
        else { tab[ SecondNeighbor( q ) ] = i; q = SecondNeighbor(q); }
    }
    int n = NumberOfQueries();
    for( int i = 0; i < n; i ++ )
    {
        int wynik;
        int g = abs(tab[ QueryFrom(i) ] - tab[ QueryTo(i) ]);
        int h = t - q;
        wynik = min( h, g );
        cout << wynik << endl;
    }
    return 0;
}