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
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wunused-result"
#else
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#endif
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <list>
#include <algorithm>
#include <string>
#include <iostream>
#include <random>
#include "message.h"
#include "kollib.h"
#define FOR(x,y,z) for (int x=(y); x<=(z); ++x)
#define FORD(x,y,z) for(int x=(y); x>=(z); --x)
#define REP(x,y) for (int x=0; x<(y); ++x)
#if defined(__GNUC__) && __cplusplus < 201103L
#define FOREACH(y,x) for (typeof((x).begin()) y = (x).begin(); y != (x).end(); ++y)
#else
#define FOREACH(y,x) for (auto y = (x).begin(); y != (x).end(); ++y)
#endif
#define ALL(x) (x).begin(),(x).end()
#define SIZE(x) ((int)(x).size())
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
const int INF = 1000000001;

void Go(int v, int u, const VI& v0s, const VI& v0, const VI& qe)
{
	int p = 0;
	VPII res;
	while (1) {
		if (binary_search(ALL(v0s), u)) break;
		++p;
		if (binary_search(ALL(qe), u)) res.push_back(PII(u, p));
		int w = FirstNeighbor(u);
		if (w == v) w = SecondNeighbor(u);
		v = u;
		u = w;
	}
	int uix = find(ALL(v0), u) - v0.begin();
	PutInt(0, uix);
	PutInt(0, p);
	PutInt(0, SIZE(res));
	FOREACH(it,res) {
		PutInt(0, it->first);
		PutInt(0, it->second);
	}
	Send(0);
}

struct X {
	int u,v,d;
	X() : u(-1), v(-1), d(-1) { }
	X(int u, int v, int d) : u(u), v(v), d(d) { }
};

int main(int argc, char** argv)
{
	int nodes = NumberOfNodes();
	int id = MyNodeId();
	int n = NumberOfStudents();
	int m = NumberOfQueries();
	if (id >= n) return 0;
	nodes = min(nodes, n);
	VI v0(nodes);
	if (id == 0) {
		random_device rdev;
		mt19937 gen(rdev());
		uniform_int_distribution<int> distr(1, n);	
		set<int> s;
		REP(i,nodes) {
			int v;
			while (1) {
				v = distr(gen);
				if (s.find(v) == s.end()) break;
			}
			v0[i] = v;
			s.insert(v);
		}
		FOR(i,1,nodes-1) {
			REP(j,nodes) PutInt(i, v0[j]);
			Send(i);
		}
	}
	else {
		Receive(0);
		REP(i,nodes) v0[i] = GetInt(0);
	}
	VI v0s(v0);
	sort(ALL(v0s));
	int v = v0[id];
	VI qe(2*m);
	REP(i,m) {
		qe[2*i] = QueryFrom(i+1);
		qe[2*i+1] = QueryTo(i+1);
	}
	sort(ALL(qe));
	Go(v, FirstNeighbor(v), v0s, v0, qe);
	Go(v, SecondNeighbor(v), v0s, v0, qe);
	if (id != 0) return 0;
	VI cc[2], sz[2];
	REP(i,2) {
		cc[i].resize(nodes);
		sz[i].resize(nodes);
	}
	map<int,X> mp;
	REP(i,nodes) {
		REP(z,2) {
			Receive(i);
			cc[z][i] = GetInt(i);
			sz[z][i] = GetInt(i);
			int k = GetInt(i);
			REP(j,k) {
				int v = GetInt(i);
				int d = GetInt(i);
				X x(i, cc[z][i], d);
				if (x.u > x.v) {
					swap(x.u, x.v);
					x.d = sz[z][i] - x.d + 1;
				}
				mp[v] = x;
			}
		}
	}
	REP(i,nodes) {
		X x(i, cc[0][i], 0);
		if (x.u > x.v) {
			swap(x.u, x.v);
			x.d = sz[0][i] + 1;
		}		
		mp[v0[i]] = x;
	}
	FOR(i,1,m) {
		int v1 = QueryFrom(i);
		int v2 = QueryTo(i);
		if (v1 == v2) {
			printf("%d\n", 0);
			continue;
		}
		X x1 = mp[v1];
		X x2 = mp[v2];
		int res;
		if (x1.u == x2.u && x1.v == x2.v) res = abs(x1.d - x2.d);
		else {
			res = x1.d;
			int u = x1.u, v = x1.v;
			while (u != x2.u && u != x2.v) {
				int w;
				if (v == cc[0][u]) {
					res += sz[1][u] + 1;
					w = cc[1][u];
				}
				else {
					res += sz[0][u] + 1;
					w = cc[0][u];				
				}
				v = u;
				u = w;
			}
			if (u == x2.u) res += x2.d;
			else {
				if (x2.u == cc[0][u]) res += sz[0][u] - x2.d + 1;
				else res += sz[1][u] - x2.d + 1;
			}
		}
		printf("%d\n", min(res, n-res));
	}
	
	return 0;
}