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
#include "kollib.h"
#include "stdio.h"
// #define DEBUG 1
int results[300];

int node, nodeCount, queries, lastNode, srcNode, q;
int i, src, dst, ps1, s1, ns1, ps2, s2, ns2;
int result = 0;
int main() {
	node = MyNodeId();
	nodeCount = NumberOfNodes();
	lastNode = nodeCount - 1;
	#ifdef DEBUG
		fprintf(stderr, "Configuration node = %d, nodeCount = %d, lastNode = %d\n", node, nodeCount, lastNode);
	#endif
		
	q = queries = NumberOfQueries();
	for (i = 1 + node; i <= queries; i += nodeCount) 
	{
		src = QueryFrom(i);
		dst = QueryTo(i);
		#ifdef DEBUG
			fprintf(stderr, "Solve %d -> %d\n", src, dst);
  		#endif
  		
		if (src == dst)
			result = 0;
		else
		{
	  		for(result = 1, ps1 = ps2 = src, s1 = FirstNeighbor(src), s2 = SecondNeighbor(src);
				s1 != dst && s2 != dst;
				++result, ps1 = s1, ps2 = s2, s1 = ns1, s2 = ns2)
	  		{
	  			#ifdef DEBUG
					fprintf(stderr, "\ts1 : { p = %d, c = %d, Result q = %d\n", q);
	  			#endif
	  		
				ns1 = SecondNeighbor(s1);
				if(ns1 == ps1) ns1 = FirstNeighbor(s1);
				ns2 = SecondNeighbor(s2);
				if(ns2 == ps2) ns2 = FirstNeighbor(s2);
	  		}
		}
		results[i] = result;
		--q;
  	}
  	if(node != lastNode)
  	{
		for(i = 1 + node; i <= queries; i += nodeCount)
		{
		  	#ifdef DEBUG
				fprintf(stderr, "PutInt(%d, %d)\n", i, results[i]);
		  	#endif
	  
			PutInt(lastNode, i);
			PutInt(lastNode, results[i]);
		}
		PutInt(lastNode, 0);
		PutInt(lastNode, 0);
		#ifdef DEBUG
			fprintf(stderr, "Send Result to %d\n", lastNode);
		#endif  
		Send(lastNode);
  	}
  	else
  	{
		while(q)
		{
			#ifdef DEBUG
				fprintf(stderr, "Result q = %d\n", q);
	  		#endif
	  		
	  		srcNode = Receive(-1);

	  		#ifdef DEBUG
				fprintf(stderr, "Received from %d\n", srcNode);
	  		#endif
	  		
	  		src = dst = 1;
	  		while(src)
	  		{
				src = GetInt(srcNode);
				dst = GetInt(srcNode);
				if(src)
				{
		  			results[src] = dst;
		  			--q;
				}
	  		}
		}
		for(i = 1; i <= queries; ++i)
		{
	  		printf("%d\n", results[i]);
		}
  	}
  	return 0;
}