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
#include <cstdio>
#include <cstdlib>
#include <set>
#include <vector>
#include <utility>
#include <map>
#include <algorithm>
using namespace std;

#include "kollib.h"
#include "message.h"

void getDistinctStudentsInQueries(set<int>& students, int numberOfQueries) {
	for (int i = 0; i < numberOfQueries; ++i) {
		int a = QueryFrom(i);
		int b = QueryTo(i);
		if (a != b) {
			students.insert(a);
			students.insert(b);
		}
	}
}

void preparePoints(vector<int>& points, set<int>& pointsChecker, const set<int>& queried, int numberOfStudents, int numberOfNodes) {
	points.assign(queried.begin(), queried.end());
	pointsChecker.insert(queried.begin(), queried.end());

	if (numberOfStudents >= 50000) {
		int additionalPoints = 1000 - queried.size();
		srand(1000000007);
		while (additionalPoints > points.size()) {
			int newOne = 1 + (((rand() % 34999) * (rand() % 34999)) % 1000000007) % numberOfStudents;
			if (pointsChecker.find(newOne) == pointsChecker.end()) {
				pointsChecker.insert(newOne);
				points.push_back(newOne);
			}
		}
	}
}

void moveByOne(int& previous, int& current) {
	int next = FirstNeighbor(current);
	if (previous == next) {
		next = SecondNeighbor(current);
	}
	previous = current;
	current = next;
}

void runOneWay(int previous, int current, const set<int>& pointsChecker) {
	int distance = 1;
	while (pointsChecker.find(current) == pointsChecker.end()) {
		moveByOne(previous, current);
		distance++;
	}
	PutInt(0, current);
	PutInt(0, distance);
}

void runNodeMethod(int searchStart, const set<int>& pointsChecker) {
	do {
		PutInt(0, searchStart);
		runOneWay(searchStart, FirstNeighbor(searchStart), pointsChecker);
		runOneWay(searchStart, SecondNeighbor(searchStart), pointsChecker);

		Send(0);
		Receive(0);
		searchStart = GetInt(0);
		if (searchStart < 0) {
			break;
		}
	} while (true);
}

struct ListItem {
	int n1;
	int d1;
	int n2;
	int d2;

	ListItem() {
		n1 = n2 = -1;
		d1 = d2 = 1000000007;
	}
};

void getNext(const map<int, ListItem>& knownDistances, int previous, int current, int& next, int&distance) {
	if (knownDistances.at(current).n1 == previous) {
		next = knownDistances.at(current).n2;
		distance = knownDistances.at(current).d2;
	} else {
		next = knownDistances.at(current).n1;
		distance = knownDistances.at(current).d1;
	}
}

int getDistance(const map<int, ListItem>& knownDistances, int a, int b) {
	int total = 0;
	int distance;
	int next;
	int current = a;
	int previous = knownDistances.at(current).n1;

	do {
		getNext(knownDistances, previous, current, next, distance);
		total += distance;
		previous = current;
		current = next;
	} while (next != b);

	return min(total, NumberOfStudents() - total);
}

void printResults(const map<int, ListItem>& knownDistances) {
	int queriesCount = NumberOfQueries();
	for (int i = 1; i <= queriesCount; ++i) {
		int a = QueryFrom(i);
		int b = QueryTo(i);
		if (a != b) {
			printf("%d\n", getDistance(knownDistances, a, b));
		} else {
			printf("0\n");
		}
	}
}

void runController(const set<int>& studentsInQueries, const vector<int>& points, 
	int nodesUsed) {
	int nextToCheck = nodesUsed;
	
	map<int, ListItem> knownDistances;

	while (nextToCheck < points.size()) {
		int node = Receive(-1);
		int search = GetInt(node);
		knownDistances[search].n1 = GetInt(node);
		knownDistances[search].d1 = GetInt(node);
		knownDistances[search].n2 = GetInt(node);
		knownDistances[search].d2 = GetInt(node);

		PutInt(node, points[nextToCheck++]);
		Send(node);
	}

	for (int i = 0; i < nodesUsed; ++i) {
		int node = Receive(-1);
		int search = GetInt(node);
		knownDistances[search].n1 = GetInt(node);
		knownDistances[search].d1 = GetInt(node);
		knownDistances[search].n2 = GetInt(node);
		knownDistances[search].d2 = GetInt(node);

		PutInt(node, -1);
		Send(node);
	}

	printResults(knownDistances);
}

int main() {
	int numberOfQueries = NumberOfQueries();
	if (numberOfQueries == 0) {
		return 0;
	}

	int numberOfStudents = NumberOfStudents();
	int numberOfNodes = NumberOfNodes();
	int myNodeId = MyNodeId();

	set<int> studentsInQueries;
	getDistinctStudentsInQueries(studentsInQueries, numberOfQueries);

	//all queries are about informing itself
	if (studentsInQueries.size() == 0) {
		if (myNodeId == 0) {
			for (int i = 0; i < numberOfQueries; ++i) {
				printf("0\n");
			}
		}
		return 0;
	}

	set<int> pointsChecker;
	vector<int> points;
	preparePoints(points, pointsChecker, studentsInQueries, numberOfStudents, numberOfNodes);

	if (myNodeId == 0) {
		int usedNodes = numberOfNodes-1;
		if (usedNodes > points.size()) {
			usedNodes = points.size();
		}
		runController(studentsInQueries, points, usedNodes);
	} else {
		if (myNodeId <= points.size()) {
			runNodeMethod(points[myNodeId-1], pointsChecker);
		}
	}
	return 0;
}