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
242
243
244
245
246
247
248
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <time.h>
#include <stdlib.h>
#include <unordered_set>
#include <unordered_map>
#include "kollib.h"
#include "message.h"

using namespace std;

const unsigned MaybeALuckySeed = 0xBADCAFE;


typedef pair<int,int> PII;

/** STRUKTURA Z PRZESYLANA WIADOMOSCIA Z INTAMI **/
const char MSG_DEFAULT = 0,
           MSG_CENTER_DATA = 16,
           MSG_EDGE_VECTOR = 32;


struct Msg {
	char type;
	vector<int> data;
	
	Msg(){}
	Msg(char t) : type(t) {}
};

Msg get_message(int node = -1){
	Msg result;
	node = Receive(node);
	
	result.type = GetChar(node);
	int len = GetInt(node);
	result.data.resize(len);
	
	for(int i = 0; i < len; i++){
		result.data[i] = GetInt(node);
	}
	return result;
}
void send_message(const Msg M, int node){
	PutChar(node, M.type);
	PutInt(node, (int)M.data.size());
	
	for(int num: M.data){
		PutInt(node, num);
	}
	Send(node);
}
/** STRUKTURA Z PRZESYLANA WIADOMOSCIA Z INTAMI **/


/** INFORMACJE O KOMPUTERZE **/
int nodeId, numNodes;
void init(){
	numNodes = NumberOfNodes();
	nodeId = MyNodeId();
}
bool is_master(){
	return (nodeId == 0);
}


/** INFORMACJE O KOMPUTERZE **/



vector<PII> queries;
unordered_set<int> lookup; // te wartosci, ktorych poszukujemy

void read_queries(){
	int Q = NumberOfQueries();
	for(int i = 1; i <= Q; i++){
		int a, b;
		a = QueryFrom(i);
		b = QueryTo(i);
		queries.push_back({a,b});
		lookup.insert(a); lookup.insert(b);
		
		/*if(is_master()){
			printf("QQQ %d %d\n", a, b);
		}*/
	}
}


vector<int> centers;
unordered_set<int> centerData;
int N, myCenter;

void init_intervals(){ // odbiera srodek, z ktorego mamy zaczac czytanie
	N = NumberOfStudents();
	unsigned seed = MaybeALuckySeed;
	
	/*if(is_master()){
		seed = time(NULL);
		Msg M(MSG_CENTER_DATA);
		M.data.push_back(seed);
		
		for(int i = 0; i < numNodes; i++)
			send_message(M, i);
	}
	
	Msg M = get_message(0);*/
	srand(seed);
	
	for(int i = 0; i < numNodes; i++){
		centers.push_back(rand() % N + 1);
	}
	myCenter = centers[nodeId];
	
	for(int v: centers){
		centerData.insert(v);
	}
}


struct Edge {
	int u, v, c;
	Edge(int _u=0, int _v=0, int _c=0) : u(_u), v(_v), c(_c) {}
};
typedef vector<Edge> EdgeVec;
Msg make_msg(EdgeVec &E){
	Msg res;
	res.type = MSG_EDGE_VECTOR;
	
	for(Edge e: E){
		if(e.u < e.v){
			res.data.push_back(e.u);
			res.data.push_back(e.v);
			res.data.push_back(e.c);
		}
	}
	return res;
}
EdgeVec read_msg(Msg M){
	EdgeVec res;
	
	for(int i = 0; i < M.data.size(); i += 3){
		res.push_back(Edge(M.data[i], M.data[i+1], M.data[i+2]));
	}
	return res;
}
EdgeVec EV;




int lastVert = -1, lastDist = 0;
void make_walk(int v, int prev){
	//printf("make_walk(%d, %d)\n", v, prev);
	// dopoki nie napotkamy kolejnego konca...
	while(centerData.find(v) == centerData.end()){
		// probujemy znalezc jakas wartosc szukana
		auto it = lookup.find(v);
		if(it != lookup.end()){
			// dodajemy 'krawedz' z ostatnio znalezionego wierzcholka
			// do nastepnego o odpowiedniej wadze
			EV.push_back(Edge(lastVert, v, lastDist));
			//printf("adding %d=>%d, cost=%d\n", lastVert,v,lastDist);
			lastDist = 0;
			lastVert = v;
		}
			
		// przechodzimy dalej
		lastDist++;
		int L = FirstNeighbor(v),
		    R = SecondNeighbor(v);
		if(L == prev){
			prev = v; v = R;
		} else {
			prev = v; v = L;
		}
	}
	// jeszcze krawedz do konca
	EV.push_back(Edge(lastVert, v, lastDist));
	//printf("adding %d=>%d, cost=%d !!!\n", lastVert,v,lastDist);
}

// przetwarzamy dane
unordered_multimap<int, PII> Graph;


int goal;
int dfs_cycle(int v, int prev){
	if(v == goal) return 0;
	
	auto eq = Graph.equal_range(v);
	
	for(auto it = eq.first; it != eq.second; it++){
		PII info = it->second;
		
		if(prev == info.first) continue;
		return dfs_cycle(info.first, v) + info.second;
	}
}


void master_process_data(){
	if(!is_master()) return;
	
	EdgeVec E;
	
	for(int node = 0; node < numNodes; node++){
		E = read_msg(get_message(node));
		
		for(Edge e: E){
			Graph.insert({e.u, {e.v,e.c}});
			Graph.insert({e.v, {e.u,e.c}});
		}
	}
	
	// teraz przetwarzamy wszystkie zapytania po kolei, zwyklym DFS-em
	// po naszym grafie
	for(PII P: queries){
		goal = P.second;
		int dist = dfs_cycle(P.first, P.first);
		
		printf("%d\n", min(dist, N-dist));
	}
}



int main(){
	init();
	
	read_queries();
	
	init_intervals();
	//printf("Y %d\n", myCenter);
	
	// teraz robimy przechadzki 'w lewo' i 'w prawo' (choc dokladnie
	// nie wiemy, co to lewo, a co to prawo)
	lastVert = myCenter; lastDist = 1;
	make_walk(FirstNeighbor(myCenter), myCenter);
	lastVert = myCenter; lastDist = 1;
	make_walk(SecondNeighbor(myCenter), myCenter);
	
	// wysylamy do mastera wynik
	send_message(make_msg(EV), 0);
	
	master_process_data();
}