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
249
250
251
#include "kollib.h"
#include "message.h"
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <ctime>
#include <set>
#include <map>
#include <vector>
#include <random>
using namespace std;

#define SETIMPL set
#define MAPIMPL map

#define VDEBUG(...) 
#define DEBUG(...) 
//#define DEBUG(...) printf(__VA_ARGS__); fflush(stdout)
//#define VDEBUG(...) printf(__VA_ARGS__); fflush(stdout)

#define MASTER 0
#define SMALLTHR 10000
#define NUMINTERESTING 1000
#define MAXM 207

int nstudents, nqueries, nnodes, myid;
int from[MAXM], to[MAXM];

int next_neighbour(int i, int prev) {
	int ret = FirstNeighbor(i);
	if (FirstNeighbor(i) == prev)
		ret = SecondNeighbor(i);
	return ret;
}

void solve_small() {
	int i;
	
	for (i = myid; i < nqueries; i += nnodes) {
		int prev1, prev2, cur1, cur2;
		int ret;
		
		if (from[i] == to[i]) {
			PutInt(MASTER, 0);
			continue;
		}
		prev1 = prev2 = from[i];
		cur1 = FirstNeighbor(from[i]); cur2 = SecondNeighbor(from[i]);
		ret = 1;
		while (cur1 != to[i] && cur2 != to[i]) {
			int oldprev1 = prev1, oldprev2 = prev2;
			prev1 = cur1; prev2 = cur2;
			cur1 = next_neighbour(cur1, oldprev1);
			cur2 = next_neighbour(cur2, oldprev2);
			ret++;
		}
		PutInt(MASTER, ret);
	}
	Send(MASTER);
	if (myid == MASTER) {
		for (i = 0; i < nnodes; i++)
			Receive(i);
		for (i = 0; i < nqueries; i++) {
			int ret = GetInt(i % nnodes);
			printf("%d\n", ret);
		}
	}
}

////////////////////////////////////////////////////////////////

struct edge {
	int a, b, dst;
	edge(int &a, int &b, int &dst) { this->a = a; this->b = b; this->dst = dst; }
};

SETIMPL<int> V;
vector<edge> E;

int seek(int from, int next) {
	int dst = 1;
	int prev = from;
	while (V.find(next) == V.end()) {
		int oldprev = next;
		next = next_neighbour(next, prev);
		prev = oldprev;
		dst++;
	}
	E.push_back(edge(from, next, dst));
}

void synchronize() {
	int i;
	if (myid != MASTER) {
		int ne = E.size();
		PutInt(MASTER, ne);
		for (i = 0; i < ne; i++) {
			PutInt(MASTER, E[i].a);
			PutInt(MASTER, E[i].b);
			PutInt(MASTER, E[i].dst);
		}
		Send(MASTER);
	} else {
		for (i = 1; i < nnodes; i++) {
			int j, ne;
			Receive(i);
			ne = GetInt(i);
			for (j = 0; j < ne; j++) {
				int a = GetInt(i);
				int b = GetInt(i);
				int dst = GetInt(i);
				E.push_back(edge(a, b, dst));
			}
		}
	}
}

struct edge_master {
	int n1, dst1;
	int n2, dst2;
	edge_master() { }
	edge_master(int n1, int dst1) { this->n1 = n1; this->dst1 = dst1; this->n2 = 0; this->dst2 = 0; }
};
MAPIMPL<int, edge_master> Vmaster;

/* a -> b. */
void master_add_edge(int a, int b, int dst) {
	int i;
	MAPIMPL<int, edge_master>::iterator f = Vmaster.find(a);
	if (f == Vmaster.end()) {
		Vmaster[a] = edge_master(b, dst);
	} else {
		edge_master &e = (*f).second;
		if (e.n2 == 0 && b != e.n1) {
			e.n2 = b;
			e.dst2 = dst;
		}
		assert(b == e.n1 || b == e.n2);
	}
}

void master_neighbours(int i, int *n1, int *dst1, int *n2, int *dst2) {
	MAPIMPL<int, edge_master>::iterator f = Vmaster.find(i);
	assert(f != Vmaster.end());
	edge_master &e = (*f).second;
	*n1 = e.n1; *dst1 = e.dst1; *n2 = e.n2; *dst2 = e.dst2;
}

int master_next_neighbour(int i, int prev, int *dst) {
	MAPIMPL<int, edge_master>::iterator f = Vmaster.find(i);
	assert(f != Vmaster.end());
	edge_master &e = (*f).second;
	if (prev == e.n1) {
		*dst = e.dst2;
		return e.n2;
	} else {
		assert(prev == e.n2);
		*dst = e.dst1;
		return e.n1;
	}
}

int master_seek(int from, int next, int dst1, int to) {
	int dst = dst1;
	int prev = from;
	while (next != to) {
		int oldprev = next;
		int newdst;
		next = master_next_neighbour(next, prev, &newdst);
		prev = oldprev;
		dst += newdst;
	}
	return dst;
}

void master_solve() {
	int i;
	
	int ne = E.size();
	for (i = 0; i < ne; i++) {
		DEBUG("%d %d %d\n", E[i].a, E[i].b, E[i].dst);
		master_add_edge(E[i].a, E[i].b, E[i].dst);
		master_add_edge(E[i].b, E[i].a, E[i].dst);
	}
	
	for (i = 0; i < nqueries; i++) {
		if (from[i] == to[i]) {
			printf("0\n");
			continue;
		}
		int n1, dst1, n2, dst2;
		master_neighbours(from[i], &n1, &dst1, &n2, &dst2);
		dst1 = master_seek(from[i], n1, dst1, to[i]);
		dst2 = master_seek(from[i], n2, dst2, to[i]);
		printf("%d\n", min(dst1, dst2));
	}
}

void solve_big() {
	int nv = 2 * nqueries + NUMINTERESTING;
	int i;
	
	/* Interesting nodes. */
	for (i = 0; i < nqueries; i++) {
		V.insert(from[i]);
		V.insert(to[i]);
	}
	
	default_random_engine generator (42 /* constant seed */);
	uniform_int_distribution<int> distribution(1, nstudents);
	while (V.size() < nv) {
		int r = distribution(generator);
		V.insert(r);
	}
	
	SETIMPL<int>::iterator it;
	for (it = V.begin(), i = 0; it != V.end(); it++, i++) {
		if (i % nnodes != myid)
			continue;
		int start = *it;
		seek(start, FirstNeighbor(start));
		seek(start, SecondNeighbor(start));
	}
	
	synchronize();
	
	if (myid != MASTER)
		return;
	
	/* Now Master has everything. */
	master_solve();
}

int main() {
	int i;
	nstudents = NumberOfStudents();
	nqueries = NumberOfQueries();
	nnodes = NumberOfNodes();
	myid = MyNodeId();
	for (i = 0; i < nqueries; i++) {
		from[i] = QueryFrom(i+1);
		to[i] = QueryTo(i+1);
	}
	
	if (nstudents < SMALLTHR)
		solve_small(); /* not to bother about some edge cases of small tests. */
	else
		solve_big();
	
	return 0;
}