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

using namespace std;

typedef set<int> SI;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef set<PII> SPII;

struct node {
	int n1, l1;
	int n2, l2;
	int ds;
};

typedef vector<node> VN;
typedef map<int,int> MII;

VPII vq;
SI sq;
SPII so;
MII nd;
VN vn;

void s0(int ni) {
	PutInt(ni, 0);
	Send(ni);
}

void s1(int ni) {
	SPII::iterator it = so.begin();

	if (it != so.end()) {
		PutInt(ni, 1);
		PutInt(ni, (*it).second);
		PutInt(ni, (*it).first);
		Send(ni);

		so.erase(it);
	}
}

void sr(int n0) {
	int nc, nf, nt, ds;
	int ns = NumberOfStudents();

	nc = n0;
	nf = 0;
	ds = 0;

	do {
		vn[nc].ds = ds;
		ds += vn[nc].n1 == nf ? vn[nc].l2 : vn[nc].l1;
		nt = nc;
		nc = vn[nc].n1 == nf ? vn[nc].n2 : vn[nc].n1;
		nf = nt;
	} while (nc);

	for (VPII::iterator it = vq.begin(); it != vq.end(); ++it) {
		PII q = *it;
		if (q.first == q.second) {
			printf("0\n");
		} else {
			ds = vn[nd[q.first]].ds - vn[nd[q.second]].ds;
			if (ds < 0)
				ds = -ds;
			if (ds > ns - ds)
				ds = ns - ds;
			printf("%d\n", ds);
		}
	}
}

void w0() {
	int nn = NumberOfNodes();
	int nq = NumberOfQueries();
	int of, ofd, ot, otd, ol, no, ni;

	vq.resize(nq);
	for (int i = 1; i <= nq; ++i) {
		of = QueryFrom(i);
		ot = QueryTo(i);
		vq[i - 1] = make_pair(of, ot);
		if (of != ot) {
			sq.insert(of);
			sq.insert(ot);
		}
	}

	vn.resize(sq.size() + 1);
	no = 1;
	for (SI::iterator it = sq.begin(); it != sq.end(); ++it) {
		of = *it;
		nd[of] = no++;

		so.insert(make_pair(1, of));
		so.insert(make_pair(2, of));
	}

	for (ni = 1; ni < nn; ++ni)
		s1(ni);

	no = sq.size() - 1;
	while (no) {
		ni = Receive(-1);
		of = GetInt(ni);
		ofd = GetInt(ni);
		ot = GetInt(ni);
		otd = GetInt(ni);
		ol = GetInt(ni);

		so.erase(make_pair(otd, ot));

		if (ofd == 1) {
			vn[nd[of]].n1 = nd[ot];
			vn[nd[of]].l1 = ol;
		} else {
			vn[nd[of]].n2 = nd[ot];
			vn[nd[of]].l2 = ol;
		}

		if (otd == 1) {
			if (!vn[nd[ot]].n1)
				--no;
			vn[nd[ot]].n1 = nd[of];
			vn[nd[ot]].l1 = ol;
			if (!vn[nd[ot]].n2)
				so.insert(make_pair(3 - otd, SecondNeighbor(ot)));

		} else {
			if (!vn[nd[ot]].n2)
				--no;
			vn[nd[ot]].n2 = nd[of];
			vn[nd[ot]].l2 = ol;
			if (!vn[nd[ot]].n1)
				so.insert(make_pair(3 - otd, FirstNeighbor(ot)));
		}

		if (no)
			s1(ni); 
	}

	for (ni = 1; ni < nn; ++ni)
		s0(ni);

	for (int i = 1; i < vn.size(); ++i)
		if (!vn[i].n1 || !vn[i].n2) {
			sr(i);
			return;
		}
}

void w1() {
	int nq = NumberOfQueries();
	int of, ot, oo, ofd, on, od, ol;

	for (int i = 1; i <= nq; ++i) {
		of = QueryFrom(i);
		ot = QueryTo(i);
		if (of != ot) {
			sq.insert(of);
			sq.insert(ot);
		}
	}

	do {
		Receive(0);
		oo = GetInt(0);
		if (oo == 1) {
			of = on = GetInt(0);
			ofd = od = GetInt(0);
			ol = 0;

			do {
				ot = od == 1 ? FirstNeighbor(on) : SecondNeighbor(on);
				od = FirstNeighbor(ot) == on ? 2 : 1;
				on = ot;
				++ol;
			} while (sq.find(on) == sq.end());

			PutInt(0, of);
			PutInt(0, ofd);
			PutInt(0, on);
			PutInt(0, 3 - od);
			PutInt(0, ol);
			Send(0);
		}
	} while (oo != 0);
}

int main() {
	int mi = MyNodeId();

	if (mi) 
		w1();
	else
		w0();

	return 0;
}