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
#include "kollib.h"
#include "message.h"
#include <cstdio>
#include <cstdlib>
#include <set>
#include <ctime>
#include <vector>
#include <map>
#include <algorithm>
#define PB push_back
#define ST first
#define ND second
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;

const int n = NumberOfNodes();
const int m = MyNodeId();
int tab[110];
vector<PII> v;
map<int, int> rtab;

void genRand()
{
	srand(time(NULL));
	set<int> wylosowane;
	for (int i = 0; i < n; ++i)
	{
		while (wylosowane.find(tab[i] = (rand()%NumberOfStudents()) + 1) != wylosowane.end()); // Co jak jest za malo uczniow???
		
		wylosowane.insert(tab[i]);
		rtab[tab[i]] = i;
		PutInt(1, tab[i]);
	}
	Send(1);
}

void brut()
{
	if (MyNodeId() != 0)
		return;
		
	map<int, int> mapa;
	int old = 1;
	mapa[1] = 0;
	int x = FirstNeighbor(1);
	int size = 0;
	while (x != 1)
	{
		size++;
		mapa[x] = size;
		if (FirstNeighbor(x) != old)
		{ 
			old = x;
			x = FirstNeighbor(x);
		}
		else
		{
			old = x;
			x = SecondNeighbor(x);
		}
	}
	
	int queriesNumb = NumberOfQueries();
	for (int i = 1; i <= queriesNumb; ++i)
	{
		int ma = mapa[QueryFrom(i)];
		int mb = mapa[QueryTo(i)];
		int a = min(ma, mb);
		int b = max(ma, mb);
			
		int res = min(b - a, a + NumberOfStudents() - b);
		printf ("%d\n", res);
	}
}

int main ()
{
	if (NumberOfStudents() < 1000000)
	{
		brut();
		return 0;
	}
	//printf ("Zaczynam: %d\n", m); 
	if (m == 0)
	{
		//printf("Wchodze do rand\n");
		genRand();
		//printf ("Wychodze z rand\n");
	}
		
	else
	{
		Receive(m - 1);
		for (int i = 0; i < n; ++i)
			tab[i] = GetInt(m - 1);
		
		if (m != n - 1)
		{
			for (int i = 0; i < n; ++i)
				PutInt(m + 1, tab[i]);
		
			Send(m + 1);
		}
	}
	//printf ("Odebralem: %d\n", m);
	set<int> begins (tab, tab + n);
	begins.erase(tab[m]);
	set<int> queries;
	for (int i = 1; i <= NumberOfQueries(); ++i)
	{
		queries.insert(QueryFrom(i));
		queries.insert(QueryTo(i));
	}
	// Zwracam: węzeł 1, odlełość do tego węzła, węzeł 2, odległość do tego węzła, 
	// ile znalazlem, na ktorych miejscach u siebie znalazlem, co na tych miejscach znalazlem
	
	if (queries.find(tab[m]) != queries.end())
		v.PB(PII(0, tab[m]));
		
	int old = tab[m];
	int x = FirstNeighbor(tab[m]);
	int size = 0;
	int wezel1 = x;
	//printf ("Przetworzam: %d\n", m);
	if(begins.find(x) == begins.end())
	{
		size = 1;
		if (queries.find(x) != queries.end())
			v.PB(PII(-size, x));
		while (begins.find(FirstNeighbor(x)) == begins.end() && begins.find(SecondNeighbor(x)) == begins.end())
		{
			size++;
			if (FirstNeighbor(x) != old)
			{ 
				old = x;
				x = FirstNeighbor(x);
			}
			else
			{
				old = x;
				x = SecondNeighbor(x);
			} 
			
			if (queries.find(x) != queries.end())
				v.PB(PII(-size, x));
		}
		
		if (begins.find(FirstNeighbor(x)) != begins.end())
			wezel1 = FirstNeighbor(x);
		else
			wezel1 = SecondNeighbor(x);
	}
	
	old = tab[m];
	x = SecondNeighbor(tab[m]);
	int size2 = 0;
	int wezel2 = x;
	if(begins.find(x) == begins.end())
	{
		size2 = 1;
		if (queries.find(x) != queries.end())
			v.PB(PII(size2, x));
		while (begins.find(FirstNeighbor(x)) == begins.end() && begins.find(SecondNeighbor(x)) == begins.end())
		{
			size2++;
			if (FirstNeighbor(x) != old)
			{ 
				old = x;
				x = FirstNeighbor(x);
			}
			else
			{
				old = x;
				x = SecondNeighbor(x);
			}
			if (queries.find(x) != queries.end())
			v.PB(PII(size2, x));
		}
		
		if (begins.find(FirstNeighbor(x)) != begins.end())
			wezel2 = FirstNeighbor(x);
		else
			wezel2 = SecondNeighbor(x);
	}
	
	//printf ("Przetworzylem: %d\n", m);
	if (m != 0)
	{
		PutInt(0, wezel1);
		PutInt(0, size);
		PutInt(0, wezel2);
		PutInt(0, size2);
		PutInt(0, v.size());
		for (int i = 0; i < v.size(); ++i)
		{
			PutInt(0, v[i].ST);
			PutInt(0, v[i].ND);
		}
		Send(0);
	}
	else
	{
		//TODO: Przetworzenie zgloszeń
		//begins.insert(tab[0]);
		map<int, int> mapa;
		for (int i = 0; i < v.size(); ++i)
		{
			if (v[i].ST <= 0)
				mapa[v[i].ND] = -v[i].ST;
		}
		size++;
		
		old = tab[0];
		x = wezel1;
		while (x != tab[0])
		{
			int y = rtab[x];
			Receive(y); // Odbieram od x
			int w1 = GetInt(y);
			int siz1 = GetInt(y);
			int w2 = GetInt(y);
			int siz2 = GetInt(y);
			
			int ileMasz = GetInt(y);
			
			if (w1 != old)
			{
				for (int i = 0; i < ileMasz; ++i)
				{
					int miejsce = GetInt(y);
					int uczen = GetInt(y);
					if(miejsce <= 0)
						mapa[uczen] = -miejsce + size;
				}
				
				size += siz1 + 1;
				old = x;
				x = w1;
			}
			else // w2 != old
			{
				for (int i = 0; i < ileMasz; ++i)
				{
					int miejsce  = GetInt(y);
					int uczen  = GetInt(y);
					if (miejsce >= 0)
						mapa[uczen] = miejsce + size;
				}
				
				size += siz2 + 1;
				old = x;
				x = w2;
			}
		}
		
		// Mapa uczniów gotowa
		int queriesNumb = NumberOfQueries();
		for (int i = 1; i <= queriesNumb; ++i)
		{
			int ma = mapa[QueryFrom(i)];
			int mb = mapa[QueryTo(i)];
			int a = min(ma, mb);
			int b = max(ma, mb);
			
			int res = min(b - a, a + NumberOfStudents() - b);
			printf ("%d\n", res);
		}
	}
	
	return 0;
}