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
#include "kollib.h"
#include "message.h"
#include <iostream>
using namespace std;
int tab[101];
int main() {
  int from, to, first, sec, j, odp;
  for( j = 1; j <101; ++j) tab[j] = 0;
  if(MyNodeId() <= NumberOfQueries() ) {
	 from =  QueryFrom(MyNodeId());
	 to = QueryTo(MyNodeId());
	 first = FirstNeighbor(from);
	 sec = SecondNeighbor(from);
    if (MyNodeId() == 1 && from == to)
      cout << 0 << endl;
    else if (MyNodeId() == 1 && (to == first || to == sec) )
      cout << 1 << endl;
    else
      {
		  int wyn_prawy = 0, wyn_lewy = 0;
		  int ip = from, il= from;
			  do {
				  ++wyn_lewy;
				  ++wyn_prawy; 
				  il = SecondNeighbor(il);
				  ip = SecondNeighbor(ip);		  
				  }
			  while(il != to && ip != to);
		if(	MyNodeId() > 1){  		
		PutInt(1,min(wyn_lewy,wyn_prawy));
		Send(1);	  			  
		}
			  
		else {
		tab[1] = min(wyn_lewy,wyn_prawy) ;	
		for(j = 2; j <= NumberOfQueries(); ++j) {
			int instancja = Receive(-1);
			odp = GetInt(instancja);
			tab[instancja] = odp;
		}
		}
		for(j = 1; j <= NumberOfQueries(); ++j) cout << tab[j] << endl;
	}
	

  }
  return 0;
}