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




int main()
{
    map<long, long> res;

    long n = NumberOfStudents();
    int m = NumberOfQueries();

    int c = MyNodeId();
    while(c < m) {
        int counter = 0;
        int target = QueryTo(c+1);
        int tmp;
        int pos = QueryFrom(c+1);
        while(pos != target) {
            counter++;
            tmp = FirstNeighbor(pos);
            if(tmp == pos)
                pos = SecondNeighbor(pos);
            else
                pos = tmp;
        }
        if(counter > n/2)
            counter = n - counter;
        c += NumberOfNodes();

        if(MyNodeId() == 0)
            res[c] = counter;
        else {
            PutInt(0, c);
            PutInt(0, counter);
            Send(0);
        }
    }


    if(MyNodeId() == 0) {
        while(res.size() < m) {
            int r = Receive(-1);
            int key = GetInt(r);
            int value = GetInt(r);
            res[key] = value;
        }

        for(map<long,long>::iterator it = res.begin(); it != res.end(); it++)
            cout << (*it).second << endl;

    }

    return 0;
}