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
#include <cstdio>
#include <vector>
#include <algorithm>
#include <set>
#include <ctime>
#include <cstdlib>
#include <map>

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define MP make_pair
#define FI first
#define SE second
#define PII pair<int, int>
#define PB push_back
#define LL long long

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

using namespace std;

vector<int> rFrom;
vector<int> rTo;
vector<int> rDist;
set<int> students;
set<int> seeds;
map<int, pair<PII, PII > > res;

void traverse(int from, int next)
{
	int dist = 0;
	int prev = from;
	while (true)
	{
		++dist;
		bool isSeed = seeds.find(next) != seeds.end();
		bool isStudent = students.find(next) != students.end();
		if (isSeed || isStudent)
		{
			rFrom.PB(from);
			rTo.PB(next);
			rDist.PB(dist);
			from = next;
			dist = 0;
		}
		if (isSeed)
			break;
		int fn = FirstNeighbor(next);
		int sn = SecondNeighbor(next);
		int oldNext = next;
		next = (fn == prev) ? sn : fn;
		prev = oldNext;
	}
}

void doWorker()
{
	int q = NumberOfQueries();
	if (q == 0)
		return;
	Receive(0);
	int nodes = NumberOfNodes();
	int n = NumberOfStudents();
	int workerNodes = min(nodes, n);
	int id = MyNodeId();
	if (id >= workerNodes)
		return;
	int x;
	seeds.clear();
	REP(i, workerNodes)
	{
		int seed = GetInt(0);
		if (i == id)
			x = seed;
		seeds.insert(seed);
	}
	FOR(i, 1, q)
	{
		students.insert(QueryFrom(i));
		students.insert(QueryTo(i));
	}
	// Traverse
	traverse(x, FirstNeighbor(x));
	traverse(x, SecondNeighbor(x));
	// Send results
	int nres = rDist.size();
	PutInt(0, nres);
	REP(i, nres)
	{
		PutInt(0, rFrom[i]);
		PutInt(0, rTo[i]);
		PutInt(0, rDist[i]);
	}
	Send(0);
}

void doMaster()
{
	int q = NumberOfQueries();
	if (q == 0)
		return;
	int nodes = NumberOfNodes();
	srand(time(NULL));
	int n = NumberOfStudents();
	int workerNodes = min(nodes, n);
	if (n <= 500)
	{
		REP(i, workerNodes)
		{
			seeds.insert(i + 1);
		}
	}
	else
	{
		REP(i, workerNodes)
		{
			int r;
			do
			{
				r = (32768*(rand()%32768) + (rand()%32768))%n + 1;
			} while(seeds.find(r) != seeds.end());
			seeds.insert(r);
		}
	}
	vector<int> vseeds;
	for (set<int>::iterator it = seeds.begin(); it != seeds.end(); ++it)
		vseeds.PB(*it);
	REP(node, workerNodes)
	{
		REP(i, workerNodes)
			PutInt(node, vseeds[i]);
		Send(node);
	}
	doWorker();
	REP(node, workerNodes)
	{
		Receive(node);
		int nres = GetInt(node);
		REP(i, nres)
		{
			int from = GetInt(node);
			int to = GetInt(node);
			int dist = GetInt(node);
			if (res.find(from) == res.end())
				res[from] = MP(MP(to, dist), MP(to, dist));
			else
			{
				res[from].SE.FI = to;
				res[from].SE.SE = dist;
			}
		}
	}
	FOR(i, 1, q)
	{
		int qfrom = QueryFrom(i);
		int qto = QueryTo(i);
		if (qfrom == qto)
		{
			printf("0\n");
			continue;
		}
		int prev = qfrom;
		int next = res[qfrom].FI.FI;
		int dist = res[qfrom].FI.SE;
		while (true)
		{
			if (next == qto)
			{
				printf("%d\n", min(dist, n - dist));
				break;
			}
			int oldNext = next;
			next = res[next].FI.FI == prev ? res[next].SE.FI : res[next].FI.FI;
			dist += res[oldNext].FI.FI == prev ? res[oldNext].SE.SE : res[oldNext].FI.SE;
			prev = oldNext;
		}
	}
}

int main()
{
	int id = MyNodeId();
	if (id == 0)
		doMaster();
	else
		doWorker();
	return 0;
}