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

#include<cstdio>
#include<iostream>
#include<vector>
#define MP make_pair
#define PB push_back
#define s second
#define f first
#define PII pair<int,int>
#define VPII vector <pair<int,int> >
#define abs(a) max((a),-(a))
using namespace std;
int a,b,c,n,m;
int P,ID;
int t[31250001];
void zaznacz(int a)
{
t[a>>5]=(t[a>>5]|(1<<(a&31)));
}
#define zaznaczone(a) (t[a>>5]&(1<<(a&31)))


void solve()
{
n=NumberOfStudents();
m=NumberOfQueries();
ID=MyNodeId();
P=NumberOfNodes();

if(ID!=0)return;

    for(int i=1;i<=m;i++)
        {
        a=QueryFrom(i);
        b=QueryTo(i);
        zaznacz(a);
        zaznacz(b);       
        }
    
    if(ID!=0)return;
    VPII V;
    a=1;
    int popa=-1;
    int odl=0;
    
    if(zaznaczone(a))
            {
            V.PB(MP(a,odl));
            }
        c=FirstNeighbor(a);
        if(c==popa)c=SecondNeighbor(a);
        popa=a;
        a=c;
        odl++;
    
    while(a!=1)
        {
        if(zaznaczone(a))
            {
            V.PB(MP(a,odl));
            }
        c=FirstNeighbor(a);
        if(c==popa)c=SecondNeighbor(a);
        popa=a;
        a=c;
        odl++;
        }
    for(int i=1;i<=m;i++)
        {
        a=QueryFrom(i);
        b=QueryTo(i);
        int x,y;
        for(int j=0;j<V.size();j++)
            {
            if(a==V[j].f)x=V[j].s;
            if(b==V[j].f)y=V[j].s;
            }
        
        printf("%d\n",min(abs(x-y),n-abs(x-y)));       
        }
    return;
    
  

}
main()
{
solve();
}