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
#include <iostream>
#include <vector>
#include "message.h"
#include "kollib.h"
using namespace std;
int ilosc_inst;
int moja_inst;
int n, m;
int a, b, tmpa, tmpb, tmp;
int licznik;

struct wynik{
	int nr, wyn;
}wyn;
int w[209];

int f(int from, int to){
	licznik=0;
	if(from==to)
		return licznik;
	licznik++;
	a=FirstNeighbor(from);
	b=SecondNeighbor(from);
	tmpa=tmpb=from;
	while(a!=to && b!=to){
		licznik++;
		tmp=FirstNeighbor(a);
		if(tmp==tmpa){
			tmpa=a;
			a=SecondNeighbor(a);
		}
		else{
			tmpa=a;
			a=tmp;
		}
		/***************/
		tmp=FirstNeighbor(b);
		if(tmp==tmpb){
			tmpb=b;
			b=SecondNeighbor(b);
		}
		else{
			tmpb=b;
			b=tmp;
		}
	}
	return licznik;
}

int main(){
	ilosc_inst=NumberOfNodes();
	moja_inst=MyNodeId();
	n=NumberOfStudents();
	m=NumberOfQueries();
	for(int d=moja_inst+1; d<=m; d+=ilosc_inst){
		wyn.nr=d;
		wyn.wyn=f(QueryFrom(d), QueryTo(d));
		if (moja_inst > 0) {
			PutInt(0, wyn.nr);
			PutInt(0, wyn.wyn);
		}
		else
			w[wyn.nr]=wyn.wyn;
	}
	if (moja_inst > 0  &&  m>moja_inst )
		Send(0);

	
	if (moja_inst == 0) {
		for(int i=1; i<ilosc_inst; i++){
			if((m/ilosc_inst + ((m%ilosc_inst)>i?1:0)) >0 )
				Receive(i);
			for(int j=0; j<(m/ilosc_inst + ((m%ilosc_inst)>i?1:0)); j++){
				w[GetInt(i)]=GetInt(i);
			}	
		}
	for(int i=1; i<=m; i++)
		cout << w[i] << "\n";
	}
	
	return 0;
}