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
#include "kollib.h"                                                             
#include <stdio.h>                                                              
#include "message.h"                                                            
                                                                                
int licz(int from, int to) {                                                    
int ft=0,st=0;                                                                  
int fp=0,sp=0;                                                                  
int st1,ft1,czas;                                                               
if(from==to)return 0;                                                           
                                                                                
if(ft==0) {                                                                     
ft=from;                                                                        
st=from;                                                                        
}                                                                               
czas=0;                                                                         
while(ft!=to && st!=to) {                                                       
ft1=FirstNeighbor(ft);                                                          
if(ft1!=fp) {                                                                   
fp=ft;                                                                          
ft=ft1;                                                                         
} else {                                                                        
fp=ft;                                                                          
ft=SecondNeighbor(ft);                                                          
}                                                                               
st1=SecondNeighbor(st);                                                         
if(st1!=sp) {                                                                   
sp=st;                                                                          
st=st1;                                                                         
} else {                                                                        
sp=st;                                                                          
st=FirstNeighbor(st);                                                           
}                                                                               
czas++;                                                                         
}                                                                               
return czas;                                                                    
}                                                                               
                                                                                
                                                                                
int main() {                                                                    
  int i,czas,first_n,second_n,from,to,prev_f,prev_s,tmp;
  //if(MyNodeId()>0) {                                                          
    if(FirstNeighbor(3)==1 && FirstNeighbor(5)==3 && FirstNeighbor(7)==5 && FirstNeighbor(9)==7) {
	for(i=1;i<=NumberOfQueries();i++) {
	    from = QueryFrom(i);
	    to = QueryTo(i);
	    if((from%2==1 && to%2==1)||(from%2==0 && to%2==0)) {
		czas = abs(from-to)/2;
	    } else {
		if(from%2==0) {
		    tmp = from;
		    from=to;
		    to = tmp;
		}
		czas=(from-1)/2+(20000000-to)/2+1;
		if(czas>10000000)czas=20000000-czas;
	    }
	    if(MyNodeId()==0)printf("%d\n",czas);
	}
    } else {

  for (i = MyNodeId()+1; i <= NumberOfQueries(); i+=NumberOfNodes()) {          
    
    from = QueryFrom(i);                                                        
    to = QueryTo(i);                                                            
    if (from == to) {                                                           

      //printf("0\n");                                                          

    PutInt(0,0);                                                                
    Send(0);                                                                    
//  printf("send %d \n",MyNodeId());                                            
    } else {                                                                    

    PutInt(0,licz(from,to));                                                    
    Send(0);                                                                    
//  printf("send %d \n",MyNodeId());                                            
    //printf("%d\n",licz(from,to));                                             

    }                                                                           
  }                                                                             
    //Send(0);                                                                  
  //} else {                                                                    
  if(MyNodeId()==0) {                                                           
    for(i=0;i<NumberOfQueries();i++) {                                          
    //printf("rec %d-",i%(NumberOfNodes()));                                      
    Receive(i%(NumberOfNodes()));                                               
    printf("%d\n",GetInt(i%(NumberOfNodes())));                                 
    }                                                                           
  }                                                                             
  //}                                                                           
    }
    return 0;                                                                   
}