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

static const int MAX_NODES = 2000;
static const int MAX_M = 500;
static const int ADDITION = 10000;

std::set<int> pytania;

struct Task {
	int element;
	char dir;
	
	Task(int e, int d) : element(e), dir(d) {}
};

int currentFrom[MAX_NODES];
int ring_size;
int distants[MAX_M + ADDITION];
int prefix[MAX_M + ADDITION];

void server() {
	
	int clientsNum = NumberOfNodes()-1;
	int k = 0;
	
	std::map<std::pair<int, int>, int> dists;
	std::map<int, std::pair<int, int>> connections;
	int from;
	int to;
	int dist;
	
	int msgSize;
	for (int i = 1; i <= clientsNum; ++i) {
		Receive(i);
		msgSize = GetInt(i);
		for (int j = 0; j < msgSize; ++j) {
			from = GetInt(i);
			to = GetInt(i);
			dist = GetInt(i);
			
			//printf("Mam polaczenie %d %d\n", from, to);
			dists[std::make_pair(from, to)] = dist;
			std::pair<int, int> c = connections[from];
			if(c.first == 0)
				c.first = to;
			else
				c.second = to;
			connections[from] = c;
		}
	}
	int start = *pytania.begin();
	
	int prev = start;
	int current = start;
	
	
	//Robienie pierscienia
	int sum = 0;
	std::map<int, int> positions;
	do {
		std::pair<int, int> c = connections[current];
		
		if (c.first != prev) {
			prev = current;
			current = c.first;
		} else {
			prev = current;
			current = c.second;
		}

		positions[current] = ring_size;
		distants[ring_size] = dists[std::make_pair(prev, current)];
		
		sum += dists[std::make_pair(prev, current)];
		prefix[ring_size] = sum;
		ring_size++;
	} while(current != start);
	
	
	
	//Odczytanie wynikow
	int beg, end, diff;
	int n = NumberOfStudents();
	for(int i = 1; i <= NumberOfQueries(); ++i) {
		beg = QueryFrom(i);
		end = QueryTo(i);
		//printf("%d %d    ( %d %d )  %d %d\n", beg, end, positions[beg], positions[end],
		//	   prefix[positions[beg]], prefix[positions[end]]);

		diff = abs(prefix[positions[beg]] - prefix[positions[end]]);
		diff = std::min(n - diff, diff);
		printf("%d\n", diff);
	}
}

bool isInteresting(int element)
{
	if (pytania.count(element) > 0)
		return true;
	else
		return false;
}

std::vector<int> startBuffer;
std::vector<int> endBuffer;
std::vector<int> distBuffer;

void spacer(int element, char kierunek) {
	//printf("%d: Zaczynam spacer %d %d\n", MyNodeId(), element, kierunek);
	int start = element;
	int prev = element;
	int dystans = 1;
	int n1,n2;
	
	if (kierunek == 0)
		element = FirstNeighbor(prev);
	else
		element = SecondNeighbor(prev);

	while (!isInteresting(element))
	{
		n1 = FirstNeighbor(element);
		n2 = SecondNeighbor(element);
		
		if (n1 == prev) {
			prev = element;
			element = n2;
		} else {
			prev = element;
			element = n1;
		}
			
		++dystans;
	}
	
	startBuffer.push_back(start);
	endBuffer.push_back(element);
	distBuffer.push_back(dystans);
}

void klient() {
	std::vector<Task> tasks;
	
	for(std::set<int>::iterator it = pytania.begin(); it != pytania.end(); ++it) {
		tasks.push_back(Task(*it, 0));
		tasks.push_back(Task(*it, 1));
	}
	
	int clientsNum = NumberOfNodes()-1;
	int k = 0;
	
	k = MyNodeId() - 1;
	while (k < tasks.size()) {
		spacer(tasks[k].element, tasks[k].dir);
		k += clientsNum;
	}
	
	
	PutInt(0, startBuffer.size());
	for (int i = 0; i < startBuffer.size(); ++ i)
	{
		PutInt(0, startBuffer[i]);
		PutInt(0, endBuffer[i]);
		PutInt(0, distBuffer[i]);
	}
	Send(0);
	//printf("Done\n");
}

void addSpecjal()
{
	int n = std::min( 5 * (NumberOfStudents() / ADDITION) , ADDITION);
	unsigned seed = 9949;
	unsigned num;
	for (int i = 0; i < n; ++i) {
		seed = seed*60649+123311;
		num = seed % NumberOfStudents();
		pytania.insert(1 + num);
	}
}

int main() {
	int m = NumberOfQueries();
	for(int i = 1; i <= m; ++i) {
		pytania.insert(QueryFrom(i));
		pytania.insert(QueryTo(i));
	}
	addSpecjal();

	if (MyNodeId() == 0)
		server();
	else
		klient();
  return 0;
}