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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include<cstdio>
#include<vector>
#include<algorithm>
#include<set>
#include<iostream>
#include "message.h"
#include "kollib.h"

using namespace std;

#define rep(i,n) for(int i=0; i<(int)n; i++)
#define st first
#define nd second
#define mp make_pair
#define pb push_back

typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<pi> vpii;
typedef set<int> SI;

#ifdef DEBUG
const bool debug = true;
#else
const bool debug = false;
#endif
long long n,m,k,l,myNum;
const int inf = 1000 * 1000 * 1000 ;
const int MAKSN = 1000 * 1000 + 13; // UZUPElnic

long long nodesNum, stepsNum, currSteps, currPrzer, lastFrom;

long long ans[202];
long long odebrane;
//Przekazuje -> zapytanie,  ile kroków, z którego przyszliśmy, kogo szukamy
//zapytania -> numer [1, 200]
//każdemu 2*n / NodeNUm ludzi

void readIn()
{
	n = NumberOfStudents();
	m = NumberOfQueries();
	myNum = MyNodeId();
	nodesNum = NumberOfNodes();
	stepsNum = (100*n) / nodesNum;
	odebrane = 0;
	currSteps = 0;
	currPrzer = 0;

	rep(i,200)
		ans[i] = -1;
}


void solve()
{

	int temp;
	int from, to;

	if(myNum == 0)
	{
		for(int i = 1; i <= m; i++)
		{
			from = QueryFrom(i);
			to = QueryTo(i);
			currPrzer = from;
			currSteps = 0;

//Przekazuje ->  Nr zapytania,  ile kroków, z którego przyszliśmy, kogo szukamy
			PutLL(myNum + 1, i);
			PutLL(myNum + 1, currSteps);
			PutLL(myNum + 1, currPrzer);
			PutLL(myNum + 1, to);
			Send(myNum + 1);
		}

		int fromWhom;
		long long nr, aktans;

		while(odebrane < m)
		{
// 			cout<<"TATATAK\n";
			fromWhom = Receive(-1);
			nr = GetLL(fromWhom);
			aktans = GetLL(fromWhom);
			ans[nr] = aktans;
			odebrane++;
		}

		for(int i=1; i <= m; i++)
		{
// 			cout<<ans[i] <<" "<<n-ans[i]<<"\n";
			cout<<min(ans[i],n-ans[i])<<"\n";
		}

	}
	else
	{
// 		return;
		long long nrZap;/* ileKr, odKogo, doKogo;*/

		for(int i = 1; i <= m; i++)
		{

			currSteps = 0;

			Receive(myNum-1);
			nrZap = GetLL(myNum -1);
			currSteps = GetLL(myNum -1);
			from = GetLL(myNum -1);
			to = GetLL(myNum -1);
			currPrzer = from;


// 			cout<<myNum<<"  "<<nrZap <<" "<<currSteps<<" "<<from <<" "<<to<<"\n";

			if(currPrzer != to)
			{
			for(int j=0; j<stepsNum; j++)
			{
				if(currPrzer == to)
				{
					ans[i] = currSteps;
					odebrane++;
					break;
				}
// 				cout<<currPrzer<<" curr1 "<<from<<" "<<currSteps<<"\n";

				if(FirstNeighbor(currPrzer) == from)
				{
					temp = SecondNeighbor(currPrzer);
				}
				else
				{
					temp = FirstNeighbor(currPrzer);
				}

 				from = currPrzer;
				currPrzer = temp;
				currSteps ++;

				if(currPrzer == to)
				{
					ans[i] = currSteps;
					odebrane++;
// 					cout<<myNum<<" zANA "<<nrZap<<" \n";
					break;
				}
			}
			}
// 			else
// 				cout<<myNum<<" zANA "<<nrZap<<" \n";;

			if(myNum < nodesNum - 1)
			{
				PutLL(myNum + 1, nrZap);
				PutLL(myNum + 1, currSteps);
				PutLL(myNum + 1, currPrzer);
				PutLL(myNum + 1, to);
				Send(myNum + 1);
			}
			else
			{
				PutLL(0, nrZap);
				PutLL(0, currSteps);
				Send(0);
			}
		}

	}

}

int main()
{
	readIn();
	solve();
	return 0;
}