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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// /home/user/Desktop/PA/ROZPROSZONE/local/compile kolsol.cpp kollib.cpp
// /home/user/Desktop/PA/ROZPROSZONE/local/run 10 kolsol.e
#include "message.h"
#include "kollib.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
int N, M, Instances;
vector<int> T;

bool isKnown(int);
int number(int);

int losuj()
{
	//srand(time(0));
	int wyn = 0;
	int bits = 31;
	while(--bits)
	{
		if(rand()%2)
			wyn += 1;
		wyn = wyn << 1;
	}
	if(rand()%2)
		wyn += 1;
	return wyn;
}
/*
int distanceTo(int start, int finish)
{
	int tmp, 
	l = FirstNeighbor(start),
	r = SecondNeighbor(start),
	prevl = start,
	prevr = start,
	dist = 1;
	while( l != finish && r != finish )
	{
		tmp = FirstNeighbor(l);
		if( tmp == prevl )
			tmp = SecondNeighbor(l);
		prevl = l;
		l = tmp;
		
		tmp = FirstNeighbor(r);
		if( tmp == prevr )
			tmp = SecondNeighbor(r);
		prevr = r;
		r = tmp;
		dist++;
	}
	return dist;
}*/
int distanceToKnown( int start_index )
{
	int start = T[start_index];
	//cout<< "start: " << start<<endl;
	int tmp, 
	l = FirstNeighbor(start),
	r = SecondNeighbor(start),
	prevl = start,
	prevr = start,
	dist = 1;
	while( ! isKnown(l)  )
	{
		tmp = FirstNeighbor(l);
		if( tmp == prevl )
			tmp = SecondNeighbor(l);
		prevl = l;
		l = tmp;
		dist++;
	}
	//cout<<"INSTANCJA: "<< MyNodeId() <<" znalezione od: "<< start <<" do: "<<l<<" dystans: " <<dist<<endl;
	PutInt(0, number(l) );
	PutInt(0, dist);
	
	dist = 1;
	while(  ! isKnown(r) )
	{
		
		tmp = FirstNeighbor(r);
		if( tmp == prevr )
			tmp = SecondNeighbor(r);
		prevr = r;
		r = tmp;
		dist++;
	}
	
	PutInt(0, number(r) );
	PutInt(0, dist);
	//cout<<"INSTANCJA: "<< MyNodeId() <<" znalezione od: "<<start<<" do: "<<r<<" dystans: " <<dist<<endl;
	return dist;
}

int distance(int start, int finish, int distances[][4])
{
	
	if(start == finish)
		return 0;
	start = number(start);
	finish = number(finish);
//cout<<"START_DISTANCES "<<start<<' '<<finish<<endl;
	int startPrevl = start;
	int startPrevr = start;
	int l = distances[start][0];
	int wynl = distances[start][1];
	int r = distances[start][2];
	int wynr = distances[start][3];
	int tmp;
	//int x = 0;
	while(l != finish && r != finish )
	{
		tmp = distances[l][0];
		if(tmp != startPrevl )
		{
			wynl += distances[l][1];
		}
		else
		{
			tmp = distances[l][2];
			wynl += distances[l][3];
		}
		startPrevl = l;
		l = tmp;
		
		tmp = distances[r][0];
		if(tmp != startPrevr )
		{
			wynr += distances[r][1];
		}
		else
		{
			tmp = distances[r][2];
			wynr += distances[r][3];
		}
		startPrevr = r;
		r = tmp;
		//cout<<l <<' '<< r << endl;
		//x++;
		//while(x == 5);

	}
	if(l == finish)
		return min(N-wynl,wynl);
	return min(N-wynr,wynr);
}

bool isKnown(int student)
{
	std::vector<int>::iterator low;
	low = lower_bound( T.begin(), T.end(), student );
	if(low == T.end() || student != T[ low - T.begin()] )
		return false;
	return true;
}

int number(int student)
{
	std::vector<int>::iterator low;
	low = lower_bound( T.begin(), T.end(), student );
	if(low == T.end() || student != T[ low - T.begin()] )
		return -1;
	return low - T.begin();
}

int main() 
{
	srand(time(0));
	N = NumberOfStudents();
	M = NumberOfQueries();
	Instances = NumberOfNodes();
	for(int i = 1; i <= M; ++i)
	{
		T.push_back( QueryFrom(i) );
		T.push_back( QueryTo(i) );
	}

	// using default comparison:
	sort(T.begin(), T.end());
	std::vector<int>::iterator it;
	it = unique (T.begin(), T.end());
	T.resize( std::distance(T.begin(),it) );
	int diff = M + (Instances - 1);
	diff += (Instances - 1) - (diff % (Instances - 1));
	//if (MyNodeId() == 0) cout<< diff <<' ';
	diff -= M;
	//if (MyNodeId() == 0) cout<< diff<<endl;
	
	
	if (MyNodeId() == 0) 
	{
		for( int i = 0; i < diff; ++i )
		{ 
			int newNode = losuj()%N + 1;
			T.push_back( newNode );
			for( int j = 1; j < Instances; ++j )
			{
				PutInt(j, newNode);
			}
		}
		for( int j = 1; j < Instances; ++j )
		{
			Send(j);
		}
		
	}
	else
	{
		Receive(0);
		for( int i = 0; i < diff; ++i )
		{ 
			T.push_back( GetInt(0) );
		}
	}
	sort(T.begin(), T.end());
	M = T.size();
	it = unique (T.begin(), T.end());
	T.resize( std::distance(T.begin(),it) );
	int queriesToPerform = M / (Instances-1);
	if(queriesToPerform*(Instances-1) != M );
		cout<"BLAD BLAD ";
		
	
	
	if (MyNodeId() == 0) 
	{
		
		//cout<<"N = "<<N<<' '<<"M = "<< T.size() <<endl;
		//for(int i = 0; i < T.size(); i++)
		//	cout<<T[i]<<' ';
		//cout<<endl;
		
		int Distances[600][4];
		
		for( int j = 1; j < Instances; ++j )
		{
			Receive(j);
			int a,b;
			for(int i = 0; i < queriesToPerform; ++i)
			{
				if( j + i*(Instances-1) -1 < T.size() )
				{
					a = GetInt( j );
					b = GetInt(j);
					//cout<<"instancja0 otrzymala od " << j << "-tej otrzymala:" << a<<' '<< b<<endl;
					Distances[ j + i*(Instances-1) -1 ][ 0 ] = a;
					Distances[ j + i*(Instances-1) -1 ][ 1 ] = b;
					a = GetInt( j );
					b = GetInt(j);
					//cout<<"instancja0 otrzymala od " << j << "-tej otrzymala:" << a<<' '<< b<<endl;
					Distances[ j + i*(Instances-1) -1 ][ 2 ] = a;
					Distances[ j + i*(Instances-1) -1 ][ 3 ] = b;
				}
			}
		}
		
		
		/*for(int i = 0; i < T.size(); ++i)
		{
			//distance(1,2,Distances);
			cout<<i<<": ";
			cout<< Distances[i][0] <<' ';
			cout<< Distances[i][1] <<' ';
			cout<<Distances[i][2] <<' ';
			cout<< Distances[i][3] <<' ';

			cout<<endl;
		}*/
		
		 for (int i = 1; i <= NumberOfQueries(); ++i) 
		 {

			printf("%d\n", distance(QueryFrom(i), QueryTo(i), Distances));

		 }
		
	}
	else
	{
		
		for(int i = 0; i < queriesToPerform; ++i)
		{
			if( MyNodeId() + i*(Instances-1)-1 < T.size() )
			{
				//cout<<"MyNodeId= "<<MyNodeId()<<' '<< MyNodeId() + i*(Instances-1)-1 << " " << T[ MyNodeId() + i*(Instances-1)-1 ] <<endl;
				distanceToKnown(  MyNodeId() + i*(Instances-1)-1  );
			}
		}
		Send(0);
	}
	
	
		if (MyNodeId() == 0) 
	{
		
		
		
	 /*for (int i = 1; i <= NumberOfQueries(); ++i) 
	  {
		if (QueryFrom(i) == QueryTo(i))
			printf("0\n");
		else 
		{
			printf("%d\n", distanceTo(QueryFrom(i), QueryTo(i)));
		}
	  }*/
	}
	
  return 0;
}