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

int main(){
	
	int queries = NumberOfQueries();

	set<int> member;

	for (int i = 1; i <= queries; i++){
		member.insert(QueryFrom(i));
		member.insert(QueryTo(i));
	}

	set<int>::iterator im;
	//for (im = member.begin(); im != member.end(); ++im){
		//cout << *im << " ";
	//}
	//cout << endl;

	int NODES = NumberOfNodes();
	int node = MyNodeId();
	
	map<int, vector<int> > table;
	int numb = 0;
	for (im = member.begin(); im != member.end(); ++im){
		if (numb++ % NODES != node){ continue; }
		int first = FirstNeighbor(*im);
		int second = SecondNeighbor(*im);
		int last = *im;
		int firstCounter = 1;
		int secondCounter = 1;
		while (member.end() == member.find(first)){
			int next = FirstNeighbor(first);
			if (next == last){
				next = SecondNeighbor(first);
			}
			last = first;
			first = next;
			firstCounter++;
		}
		last = *im;
		while (member.end() == member.find(second)){
			int next = FirstNeighbor(second);
			if (next == last){
				next = SecondNeighbor(second);
			}
			last = second;
			second = next;
			secondCounter++;
		}
		
		if (node == 0){
			vector<int> connect;
			connect.push_back(first);
			connect.push_back(firstCounter);
			connect.push_back(second);
			connect.push_back(secondCounter);
			table[*im] = connect;
		}
		else{
			PutInt(0, first);
			PutInt(0, firstCounter);
			PutInt(0, second);
			PutInt(0, secondCounter);
			Send(0);
		}
	}

	// collecting results
	if (node == 0){
		numb = 0;
		for (im = member.begin(); im != member.end(); ++im){
			if (numb % NODES != 0){
				Receive(numb % NODES);
				vector<int> connect;
				connect.push_back(GetInt(numb % NODES));
				connect.push_back(GetInt(numb % NODES));
				connect.push_back(GetInt(numb % NODES));
				connect.push_back(GetInt(numb % NODES));
				table[*im] = connect;
			}
			numb++;
		}
	}
	
	

	//map<int, vector<int> >::iterator it;
	//for (it = table.begin(); it != table.end(); ++it){
	//	cout << it->first << " " << (it->second)[0] << " " << (it->second)[1] << " " << (it->second)[2] << " " << (it->second)[3] << endl;
	//}

	if (node == 0){

		map<int, int> position;
		int zero = table.begin()->first;
		int last = zero;
		position[zero] = 0;
		int first = table[zero][0];
		int firstPosition = table[zero][1];
		position[first] = firstPosition;
		while (1){
			int next = table[first][0];
			int nextPosition = table[first][1];
			if (last == next){
				next = table[first][2];
				nextPosition = table[first][3];
			}
			if (next == zero)break;
			firstPosition += nextPosition;
			position[next] = firstPosition;
			last = first;
			first = next;
		}

		//map<int, int>::iterator mii;
		//for (mii = position.begin(); mii != position.end(); ++mii){
		//	cout << mii->first << " " << mii->second << endl;
		//}

		int students = NumberOfStudents();
		for (int i = 1; i <= queries; i++){
			int from = position[QueryFrom(i)];
			int to = position[QueryTo(i)];
			if (to == from){
				cout << 0 << endl;
			}
			else{
				int a = (students + from - to) % students;
				int b = (students - from + to) % students;
				cout << (a < b ? a : b) << endl;
			}

		}

	}
	
	return 0;
}