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
// Grzegorz Bukowiec
// PA 2014 - Kolko informatyczne

#include "kollib.h"
#include "message.h"
#include <iostream>
using namespace std;


int main()
{
	ios_base::sync_with_stdio(0);

	if (MyNodeId() == 0) {
		long long int actual1, actual2, previous1, previous2, aim, distance;
		for (long long int i = 1; i <= NumberOfQueries(); ++i) {
			if (QueryFrom(i) == QueryTo(i)) {
				cout << 0 << endl;
				continue;
			}
			previous1 = QueryFrom(i);
			previous2 = QueryFrom(i);
			actual1 = FirstNeighbor(previous1);
			actual2 = SecondNeighbor(previous1);
			distance = 0;

			while (true) {
				++distance;
				if (actual1 == QueryTo(i)) {
					cout << distance << endl;
					break;
				}
				if (actual2 == QueryTo(i)) {
					cout << distance << endl;
					break;
				}
				
				if (FirstNeighbor(actual1) != previous1) {
					previous1 = actual1;
					actual1 = FirstNeighbor(actual1);
				} else {
					previous1 = actual1;
					actual1 = SecondNeighbor(actual1);
				}

				if (FirstNeighbor(actual2) != previous2) {
					previous2 = actual2;
					actual2 = FirstNeighbor(actual2);
				} else {
					previous2 = actual2;
					actual2 = SecondNeighbor(actual2);
				}
			}
		}
	}

	return 0;
}