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
#include "kollib.h"
#include "message.h"
#include <iostream>
#include <set>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <map>
using namespace std;

vector<int> global_startpoints;
set<int> global_startpoints_set;

void generateRandomStartpoints()
{	
	srand(time(0));
	int instances = NumberOfNodes();
	
	while(global_startpoints.size() < instances/2 && global_startpoints.size() < NumberOfStudents())
	{
		global_startpoints.push_back((random() % NumberOfStudents()) +1);
		global_startpoints_set.insert(global_startpoints.back());
	}
}

void giveRandomStartpoints(int toInstance)
{
	PutInt(toInstance, global_startpoints.size());	
	for(int start : global_startpoints)
	{
		PutInt(toInstance, start);
	}
	Send(toInstance);
}

void retrieveRandomStartpoints(int fromInstance)
{	
	Receive(fromInstance);
	int n = GetInt(fromInstance);
	while(n-->0)
	{
		int startpoint = GetInt(fromInstance);
		global_startpoints.push_back(startpoint);
		global_startpoints_set.insert(startpoint);
	}
}

void runSegment()
{
	if(MyNodeId()/2 >= global_startpoints.size())
	{
		return;
	}
		
	map<int, int> local_positions;
	for(int i=1;i<=NumberOfQueries();i++)
	{
		local_positions[QueryFrom(i)] = -1;
		local_positions[QueryTo(i)] = -1;
	}
		
	
	int p = global_startpoints[MyNodeId()/2];
	int prev = MyNodeId()%2 ? FirstNeighbor(p) : SecondNeighbor(p);
	
	int afterstart = MyNodeId()%2 ? SecondNeighbor(p) : FirstNeighbor(p);
		
	int i = 0;
	do
	{		
		auto it = local_positions.find(p);
		if(it!=local_positions.end())
		{
			it->second = i;
		}
		
		int p2 = FirstNeighbor(p);
		int p3 = SecondNeighbor(p);
		
		if(p2==prev){ prev = p; p = p3;}
		else{ prev = p; p = p2;}
		i++;
	}
	while( global_startpoints_set.count(p) == false);
	
	int beforeend = prev;
	
	
	PutInt(0, p); //endpoint
	PutInt(0, i); //steps
	PutInt(0, afterstart);
	PutInt(0, beforeend);
	
	for(auto& pa : local_positions)
	{
		PutInt(0, pa.second);		
	}
	Send(0);
}

struct InstanceResult
{
	int startpoint;
	int endpoint;
	int afterstart, beforeend;
	int steps;
	map<int, int> indexes;
};

void proceedResults()
{	
	map<int, int> global_positions;
	vector<InstanceResult> all_results;
	
	//Get interesting points
	for(int i=1;i<=NumberOfQueries();i++)
	{
		global_positions[QueryFrom(i)]=-1;
		global_positions[QueryTo(i)]=-1;
	}
	
	//Retrieve results from instances
	for(int i=0;i/2<global_startpoints.size();i++)
	{
		InstanceResult ir;
		ir.startpoint = global_startpoints[i/2];
		
		Receive(i);
		ir.endpoint = GetInt(i);
		ir.steps = GetInt(i);
		ir.afterstart = GetInt(i);
		ir.beforeend = GetInt(i);
		
		for(auto& pa : global_positions)
		{			
			int p = pa.first;
			int at = GetInt(i);
			
			ir.indexes[p]=at;
		}
		
		all_results.push_back(ir);
	}
	
	int p = all_results[0].startpoint;
	int start = p;
	int prev = all_results[0].afterstart;
	int i=0;
	
	//Connect results and calculate global indexes
	do
	{
		for(InstanceResult& ir : all_results)//linear searching, but who cares..
		{
			if(ir.startpoint == p && ir.afterstart!=prev)
			{
				for(auto& pa : ir.indexes)
				{
					if(pa.second != -1)
					{
						global_positions[pa.first] = pa.second + i;
					}
				}
				
				p = ir.endpoint;
				prev = ir.beforeend;
				i+= ir.steps;
				
				break;
			}
		}
	}
	while(p != start);
	
	for(int i=1;i<=NumberOfQueries();i++)
	{
		int from = QueryFrom(i);
		int to = QueryTo(i);
		
		int distance = (global_positions[from]-global_positions[to] + NumberOfStudents())
			% NumberOfStudents();
		if( distance > NumberOfStudents()/2)
			distance = NumberOfStudents() - distance;
			
		printf("%d\n", distance);
	}
}

int main() {
	int instanceID = MyNodeId();
	
	if(instanceID == 0)
	{
		generateRandomStartpoints();
		for(int i=1;i<NumberOfNodes();i++)
		{
			giveRandomStartpoints(i);
		}
	}
		
	if(instanceID > 0)
		retrieveRandomStartpoints(0);
	
	runSegment();
	
	if(instanceID == 0)
	{
		proceedResults();
	}
	return 0;
}