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


struct Item
{
	int ida, idb;
	int a, b;
};


std::vector<Item> items;



bool update(int val, int id)
{
int i;
bool res = true;

	//std::cout << "update: " << val << " " << id << std::endl;

	for(i=0 ; i<items.size() ; ++i)
	{
		if( items[i].a == val )
			items[i].ida = id;

		if( items[i].b == val )
			items[i].idb = id;

		if( items[i].ida == -1 || items[i].idb == -1 )
			res = false;
	}

return res;
}



void first()
{
int len;
int a, b, old, preold;
int i, n;
int how_many = 0;

	n = NumberOfStudents();
	len = n;

	preold = 1;
	old    = FirstNeighbor(preold);
	update(1, 0);
	//std::cout << preold << std::endl;

	for(i=0 ; i<len ; ++i)
	{
		bool br = update(old, i+1);
		//std::cout << old << std::endl;

		if( br )
			break;

		a = FirstNeighbor(old);
		b = SecondNeighbor(old);

		if( a == preold )
		{
			preold = old;
			old = b;
		}
		else
		{
			preold = old;
			old = a;
		}
	}
}





void calc()
{
	int len = NumberOfNodes();
	int id = MyNodeId(); // 0..

	int n = NumberOfQueries();

	if( id >= n )
		return;


	int start = (n / len) * id;

//	std::cout << "komputer o id " << id << " rozpoczyna analize od: " << start << std::endl;

	int how_many = (n/len);

	if( id == len - 1 )
		how_many = n - start;

	for(int i=0 ; i<how_many ; ++i)
	{
		Item item;

		item.ida = -1;
		item.idb = -1;
		item.a = QueryFrom(start+i+1);
		item.b = QueryTo(start+i+1);

		items.push_back(item);

//		std::cout << "id: " << id << ", bede sie pytal o " << item.a << " " << item.b << std::endl;
	}

	first();
	int slen = NumberOfStudents();

	for(int i=0 ; i<items.size() ; ++i)
	{
		int ida = items[i].ida;
		int idb = items[i].idb;

		int r = abs(idb - ida);

		if( r > slen/2 )
			r = slen - r;

//		std::cout << "odpowiedz id: " << id << " dla " << items[i].a << " " << items[i].b << " " << r << std::endl;

		if( id > 0 )
		{
			PutInt(0, r);
		}
		else
		{
			std::cout << r << "\n";
		}
	}


	if( id == 0 )
	{
		for(int i=1 ; i<len ; ++i)
		{
			Receive(i);

			int how_many = (n/len);

			if( i == len - 1 )
				how_many = n - (n / len) * i;

			for(int a=0 ; a<how_many ; ++a)
				std::cout << GetInt(i) << "\n";
		}
	}
	else
	{
		Send(0);
	}

}



int main()
{
	calc();
}