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
#include "kollib.h"
#include "message.h"

#include <map>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <set>
#define MAX_NODE 107
#define INF
#define PB push_back
#define MP make_pair
#define ST first
#define ND second

#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(a,b,c) for(int a=b;a<=(c);a++)
#define FORD(a,b,c) for (int a=b;a>=(c);a--)
#define VAR(v,n) __typeof(n) v=(n)
#define ALL(c) c.begin(),c.end()
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();i++)

using namespace std;

typedef long long LL; 
typedef pair<int,int> PII;

LL a = 12345701LL, b = 888888887LL, mod = 1000000033LL;
LL seed = 1;

int ile_inst;
int mojeID;

int n,m;
int stan[MAX_NODE],akt[MAX_NODE];
map<int,PII> next1, next2;
vector<int> punkty;

int odl(int pop, int a, int b, int dl) {
	//printf("mamy %d %d %d %d\n",pop,a,b,dl);
	if (a == b) return dl;
	PII pom1 = next1[a];
	PII pom2 = next2[a];
	//printf("%d %d %d %d\n",pom1.ST,pom1.ND,pom2.ST,pom2.ND);
	
	if (pom1.ST == pop)
		return odl(a,pom2.ST,b,dl+pom2.ND);
	
	return odl(a,pom1.ST,b,dl+pom1.ND);
}

void AddEdge(int a, int b, int dl) {
	//printf("odleglosc miedy %d oraz %d to %d\n",a,b,dl); 
	
	if (next1.find(a) != next1.end())
		next2[a] = MP(b,dl);
	else
		next1[a] = MP(b,dl);
}

PII oblicz(int x, int pop) {
	int dl = 1;
	vector<int>::iterator it = lower_bound(ALL(punkty),x);
	while (it == punkty.end() || *it != x) {
		++dl;
		int pom = FirstNeighbor(x);
		if (pom == pop) {
			pop = x;
			x = SecondNeighbor(x);
		}
		else {
			pop = x;
			x = pom;
		}
		it = lower_bound(ALL(punkty),x);
	}
	return MP(x,dl);
}

int main() {
	ile_inst = NumberOfNodes();
	mojeID = MyNodeId();
	n = NumberOfStudents();
	m = NumberOfQueries();
	
	if (m == 0) return 0;
 	
	FOR(i,1,m) {
		punkty.PB(QueryFrom(i));
		punkty.PB(QueryTo(i));
	}
	FOR(i,1,2*(ile_inst-1)) {
		seed = (a*seed + b)%mod;
		punkty.PB((seed%n)+1);
	}
	
	sort(ALL(punkty));
	punkty.resize(unique(ALL(punkty))-punkty.begin());
	
	//FOREACH(it,punkty) printf("%d ",*it); puts("");
	
	if (mojeID) {
		bool trzeba = true;
		while (trzeba) {
			Receive(0);
			int start = GetInt(0);
			if (start == -1) 
				trzeba = false;
			else {
				PII wyn = oblicz(FirstNeighbor(start),start);
				PutInt(0,wyn.ST);
				PutInt(0,wyn.ND);
				Send(0);
				
				wyn = oblicz(SecondNeighbor(start),start);
				PutInt(0,wyn.ST);
				PutInt(0,wyn.ND);
				Send(0);
			}
		}
	}
	else {
		int wiel = int(punkty.size());
		FOR(i,1,ile_inst-1) {
			if (i <= wiel) {
				PutInt(i,punkty[i-1]);
				Send(i);
				stan[i] = 2;
				akt[i] = punkty[i-1];
			}
			else {
				PutInt(i,-1);
				Send(i);
			}
		}
		
		int wsk = ile_inst-1;
		REP(i,2*wiel) {
			int kt = Receive(-1);
			int dokad = GetInt(kt);
			int dl = GetInt(kt);
			AddEdge(akt[kt],dokad,dl);
			--stan[kt];
			if (!stan[kt]) {
				if (wsk < wiel) {
					PutInt(kt,punkty[wsk]);
					akt[kt] = punkty[wsk];
					stan[kt] = 2;
					++wsk;
				}
				else PutInt(kt,-1);
				Send(kt);
			}
		}
			
		FOR(i,1,m) {
			int a = QueryFrom(i);
			int b = QueryTo(i);
			int pom = (a == b) ? 0 : odl(a,next1[a].ST,b,next1[a].ND); 
			printf("%d\n",min(n-pom,pom));
		}
	
	}
	return 0;
}