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
// Krzyszof Piesiewicz
// Kółko informatyczne PA 2014
#include "message.h"
#include "kollib.h"
#include <algorithm>
#include <cstdio>
#include <map>
#define LL (long long)
using namespace std;

const int DB = 0, MAX_M = 207;

int id, n, m, size, p, k, step, pre, nextt, curr, ile, x, y, q[ MAX_M ][ 2 ];
map< int, int > mp;

int main() {
	id = MyNodeId();
	if( id < 2 ) {
		size = NumberOfStudents();
		if( id == 0 ) {
			curr = 1;
			pre = SecondNeighbor( 1 );
			step = 1;
			p = 1;
			k = 1 + size / 2;
		}
		else {
			curr = SecondNeighbor( 1 );
			pre = 1;
			step = -1;
			p = size;
			k = size / 2;
		}
		
		m = NumberOfQueries();
		for( int i = 1; i <= m; i++ ) {
			q[ i ][ 0 ] = QueryFrom( i );
			q[ i ][ 1 ] = QueryTo( i );
			for( int j = 0; j < 2; j++ )
				if( mp.find( q[ i ][ j ] ) == mp.end() )
					mp[ q[ i ][ j ] ] = -1;
		}
		
		for( int i = p; i != k; i += step ) {
			if( mp.find( curr ) != mp.end() )
				mp[ curr ] = i;
			nextt = FirstNeighbor( curr );
			if( nextt == pre )
				nextt = SecondNeighbor( curr );
			pre = curr;
			curr = nextt;
		}
		
		if( id == 1 ) {
			for( map< int, int >::iterator it = mp.begin(); it != mp.end(); it++ )
				if( it->second != -1 )
					ile++;
			PutInt( 0, ile );
			for( map< int, int >::iterator it = mp.begin(); it != mp.end(); it++ )
				if( it->second != -1 ) {
					PutInt( 0, it->first );
					PutInt( 0, it->second );
				}
			Send( 0 );
			if( DB )for( map< int, int >::iterator it = mp.begin(); it != mp.end(); it++ )
				printf( "%d %d\n", it->first, it->second );
		}
		else {
			Receive( 1 );
			ile = GetInt( 1 );
			for( int i = 0; i < ile; i++ )
				mp[ GetInt( 1 ) ] = GetInt( 1 );
			for( int i = 1; i <= m; i++ ) {
				x = mp[ q[ i ][ 0 ] ];
				y = mp[ q[ i ][ 1 ] ];
			
				if( x > y )
					swap( x, y );
				printf( "%d\n", min( y - x, size - y + x ) );
			}
			if( DB )for( map< int, int >::iterator it = mp.begin(); it != mp.end(); it++ )
				printf( "%d %d\n", it->first, it->second );
		}
		
		if( DB ) printf( "id %d, p %d, k %d\n", id, p, k );
	}
	return 0;
}