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
#include "kollib.h"
#include "message.h"
#include <iostream>
using namespace std;

int main()
{
    int odp;
    if(MyNodeId()==NumberOfNodes()-1)
    {
        for(int i=1; i<=NumberOfQueries(); i++)
        {
            Receive(i-1);
            cout << GetInt(i-1) << endl;
        }
    }
    else
    for(int i=1+MyNodeId(); i<=NumberOfQueries(); i+=NumberOfNodes()-1)
    {
        if(QueryFrom(i)==QueryTo(i))
            odp=0;
        else
        {
            if(SecondNeighbor(QueryFrom(i))==QueryTo(i) || FirstNeighbor(QueryFrom(i))==QueryTo(i))
                odp=1;
            else
            {
                int b=SecondNeighbor(QueryFrom(i)), ilosc=2, poprz=QueryFrom(i);
                while(SecondNeighbor(b)!=QueryTo(i) && FirstNeighbor(b)!=QueryTo(i))
                {
                    ilosc++;
                    if(FirstNeighbor(b)==poprz)
                    {
                        poprz=b;
                        b=SecondNeighbor(b);
                    }
                    else
                    {
                        poprz=b;
                        b=FirstNeighbor(b);
                    }
                }
                if(NumberOfStudents()-ilosc<ilosc)
                    ilosc=NumberOfStudents()-ilosc;
                odp=ilosc;
            }
        }
        PutInt(NumberOfNodes()-1, odp);
        Send(NumberOfNodes()-1);
    }
    return 0;
}