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
// i-ta instancja wysyla do (i+1)-szej jedna wiadomosc z jednym int-em
#include "message.h"
#include "kollib.h"
#include <stdio.h>

int main() {
	int numberOfNodes = NumberOfNodes(); 
	int numberOfQueries = NumberOfQueries();
	int myNodeId = MyNodeId();
	int results[201];
	
	for(int queryNumber = myNodeId + 1; queryNumber <= numberOfQueries; queryNumber += numberOfNodes) {
		int distance = 0;		
		int queryFrom = QueryFrom(queryNumber);
		int queryTo = QueryTo(queryNumber);
		
		int currentFirst = queryFrom;
		int previousFirst = queryFrom;
		int currentSecond = queryFrom;
		int previousSecond = queryFrom;
		while (currentFirst != queryTo && currentSecond != queryTo) {
			distance ++;

			int current = FirstNeighbor(currentFirst);
			if (current != previousFirst) {
				previousFirst = currentFirst;
				currentFirst = current;
			} else {
				previousFirst = currentFirst;
				currentFirst = SecondNeighbor(currentFirst);
			}
			
			current = SecondNeighbor(currentSecond);
			if (current != previousSecond) {
				previousSecond = currentSecond;
				currentSecond = current;
			} else {
				previousSecond = currentSecond;
				currentSecond = FirstNeighbor(currentSecond);
			}
		}
		
		PutInt(0, queryNumber - 1);
		PutInt(0, distance);
	}
	
	if (myNodeId < numberOfQueries) {
		Send(0);
	}

	if (myNodeId == 0) {
		for(int queryNumber = 0; queryNumber < numberOfQueries && queryNumber < numberOfNodes; queryNumber++) {
			int source = Receive(-1);
			for(int counter = source + 1; counter <= numberOfQueries; counter += numberOfNodes) {
				int index = GetInt(source);
				int distance = GetInt(source);
				results[index] = distance;
			}
		}
		
		for(int queryNumber = 0; queryNumber < numberOfQueries; queryNumber++) {
			printf("%d\n", results[queryNumber]);
		}		
	};
	
    return 0;
}