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

int main()
{
    if(MyNodeId()==0)
    {
        int nodes=min(NumberOfNodes(),NumberOfQueries()+1);
        int *wyn=new int[NumberOfQueries()];
        for(int a=nodes;a<=NumberOfQueries();++a)
        {
            int i=Receive(-1);
            int query=GetInt(i);
            wyn[query-1]=GetInt(i);
            PutInt(i,a);
            Send(i);
        }
        for(int a=1;a<nodes;++a)
        {
            int i=Receive(-1);
            int query=GetInt(i);
            wyn[query-1]=GetInt(i);
            PutInt(i,-1);
            Send(i);
        }
        for(int a=0;a<NumberOfQueries();++a)printf("%d\n",wyn[a]);
    }else
    {
        int query=MyNodeId();
        if(query>NumberOfQueries())return 0;
        do
        {
            int value=0;

            int from=QueryFrom(query);
            int to=QueryTo(query);
            if(from!=to)
            {
                int ostR=from,ostL=from;
                int r=FirstNeighbor(from),l=SecondNeighbor(from);
                ++value;
                while(r!=to && l!=to)
                {
                    int f,s;

                    f=FirstNeighbor(r);
                    s=SecondNeighbor(r);
                    if(f==ostR)
                    {
                        ostR=r;
                        r=s;
                    }else
                    {
                        ostR=r;
                        r=f;
                    }

                    f=FirstNeighbor(l);
                    s=SecondNeighbor(l);
                    if(f==ostL)
                    {
                        ostL=l;
                        l=s;
                    }else
                    {
                        ostL=l;
                        l=f;
                    }

                    ++value;
                }
            }

            PutInt(0,query);
            PutInt(0,value);
            Send(0);
            Receive(0);
            query=GetInt(0);
        }while(query!=-1);
    }
    return 0;
}