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

#include <algorithm>
#include <assert.h>
#include <cstdio>
#include <deque>
#include <map>
#include <set>
using namespace std;

#define FOR(E, I) for(auto E = I.begin(); E != I.end(); E++)

#undef DEBUG

struct Order
{
	char o;
	int studentIndex;

	Order()
	{
		this->o = 'Q';
		this->studentIndex = -1;
	}
	Order(char o, int studentIndex)
	{
		this->o = o;
		this->studentIndex = studentIndex;
	}
};

struct Response
{
	int nodeId;

	int studentIndex;
	int neighbor;
	int distance;
};


set<int> getInterestingStudents()
{
	set<int> interestingStudents;

	int numberOfQueries = NumberOfQueries();
	for (int q = 1; q <= numberOfQueries; q++)
	{
		interestingStudents.insert(QueryFrom(q));
		interestingStudents.insert(QueryTo(q));
	}

	return interestingStudents;
}

Order slaveGetOrder()
{
	Order order;
	int source = Receive(0);
	order.o = GetChar(source);

	if (order.o != 'Q')
	{
		order.studentIndex = GetInt(source);
	}

	return order;
}

void slaveSendResponse(const Response &response)
{
	PutInt(0, response.studentIndex);
	PutInt(0, response.neighbor);
	PutInt(0, response.distance);

	Send(0);
}

Response masterGetResponse()
{
	Response response;

	int source = Receive(-1);
	response.nodeId = source;
	response.studentIndex = GetInt(source);
	response.neighbor = GetInt(source);
	response.distance = GetInt(source);

#ifdef DEBUG
	printf("node %d: %d -> %d = %d\n", response.nodeId, response.studentIndex, response.neighbor, response.distance);
#endif

	return response;
}

void masterSendQuit(int nodeId)
{
	PutChar(nodeId, 'Q');
	Send(nodeId);
}

void masterSendOrder(int nodeId, deque<Order> &orders)
{
	PutChar(nodeId, orders.front().o);
	PutInt(nodeId, orders.front().studentIndex);
	Send(nodeId);

	orders.pop_front();
}

void slaveFindClosestInterestingStudent(Response &response, const set<int> &interestingStudents, int s0, int s1)
{
	int distance = 1;
	while (interestingStudents.find(s1) == interestingStudents.end())
	{
		if (s0 == FirstNeighbor(s1))
		{
			s0 = s1;
			s1 = SecondNeighbor(s1);
		}
		else
		{
			s0 = s1;
			s1 = FirstNeighbor(s1);
		}
		distance++;
	}

	response.neighbor = s1;
	response.distance = distance;
}

void slave()
{
	set<int> interestingStudents = getInterestingStudents();

	while (true)
	{
		Order order = slaveGetOrder();
		if (order.o == 'Q') break;

		Response response;
		response.studentIndex = order.studentIndex;
		slaveFindClosestInterestingStudent(
			response, interestingStudents, order.studentIndex, 
			order.o == 'L' ? FirstNeighbor(order.studentIndex) : SecondNeighbor(order.studentIndex)
		);
		slaveSendResponse(response);
	}
}

int findDistance(multimap<int, Response> &responses, int numberOfStudents, int f, int t)
{
	int f0 = f;
	int distance = 0;
	while (f != t)
	{
		auto r = responses.find(f);
		assert(r != responses.end());

		if (r->second.neighbor != f0)
		{
			f0 = f;
			f = r->second.neighbor;
			distance += r->second.distance;
		}
		else
		{
			r++;
			assert(r != responses.end());

			f0 = f;
			f = r->second.neighbor;
			distance += r->second.distance;			
		}
	}

	return min(distance, numberOfStudents - distance);
}


void master()
{
	size_t numberOfSlaves = NumberOfNodes() - 1;
	set<int> interestingStudents = getInterestingStudents();
	deque<Order> orders;

	// prepare orders
	FOR(s, interestingStudents)	
	{
		orders.push_back(Order('L', *s));
		orders.push_back(Order('R', *s));
	}

	int totalOrderCount = orders.size();

	// submit orders to all slaves
	for (int i = 1; i <= min(numberOfSlaves, orders.size()); i++)
	{
		masterSendOrder(i, orders);
	}

	// receive responses and submit more orders
	multimap<int, Response> responses;
	while (responses.size() < totalOrderCount)
	{
		Response response = masterGetResponse();
		if (!orders.empty())
		{
			masterSendOrder(response.nodeId, orders);
		}

		responses.insert(make_pair(response.studentIndex, response));
	}

	// order slaves to quit
	for (int i = 1; i <= numberOfSlaves; i++)
	{
		masterSendQuit(i);
	}

	// calculate results
	int numberOfStudents = NumberOfStudents();
	int numberOfQueries = NumberOfQueries();	
	for (int q = 1; q <= numberOfQueries; q++)
	{
		int f = QueryFrom(q);
		int t = QueryTo(q);

		printf("%d\n", findDistance(responses, numberOfStudents, f, t));
	}
}


int main() 
{
	MyNodeId() ? slave() : master();

	return 0;
}