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
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
///*** Naglowki Piotra Stanczyka

#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <fstream>

#include "message.h"
#include <assert.h>
#include "kollib.h"

using namespace std;

typedef vector<int> VI;
typedef long long LL;

#define FOR(x, b, e) for(int x=b; x<=(e); ++x)
#define FORD(x, b, e) for(int x=b; x>=(e); x--)
#define REP(x, n) for(int x=0; x<(n); ++x)
#define VAR(v,n) typeof(n) v=(n)
#define ALL(c) c.begin(),c.end()
#define SIZE(x) (int)x.size()
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
#define PB push_back
#define ST first
#define ND second

const int size = 2000;
vector <vector <int> > neighbours(size);
vector <int> nodes(size), d(size);
vector <bool> considered(size);

int cw, ccw;


void CW(int u, int start, int end, int last)
{

    //cout<<"uczen cw: "<<u<<endl;

    if(u != end)
    {

        //cout<<"Jestem w ifie\n";

        cw++;
        if(u == start)CW(FirstNeighbor(u), start, end, u);
        else
        {
            if(FirstNeighbor(u) != last)CW(FirstNeighbor(u), start, end, u);
            else CW(SecondNeighbor(u), start, end, u);
        }
    }
}

void CCW(int u, int start, int end, int last)
{

    //cout<<"uczen ccw: "<<u<<endl;

    if(u != end)
    {
        ccw++;
        if(u == start)CCW(SecondNeighbor(u), start, end, u);
        else
        {
            if(FirstNeighbor(u) != last)CCW(FirstNeighbor(u), start, end, u);
            else CCW(SecondNeighbor(u), start, end, u);
        }
    }
}

vector <VI> qidies;

int main()
{

    ios_base::sync_with_stdio(0);

    int n, left, right, start, end;
    int liczbaInstancji = NumberOfNodes();
    int myNode = MyNodeId();

    //cin>>n;
    n = NumberOfStudents();

    int t, idInstancji = 0;
    VI temp;
    temp.clear();

    //cin>> t;
    t = NumberOfQueries();
    REP(i, t)
    {
        if(i%liczbaInstancji == 0 && i !=0)
        {
            /*FOREACH(i, temp)
            {
                PutInt(idInstancji, *i);
                Send(idInstancji);
            }*/

            qidies.PB(temp);
            temp.clear();
            idInstancji++;
        }
        else temp.PB(i+1);
    }
    qidies.PB(temp);
    idInstancji++;

    int uzyteczneInstancje = idInstancji;

    if(myNode < uzyteczneInstancje){
    FOREACH(i, qidies[myNode])
    {
        //cin>>start>>end;
        start = QueryFrom(*i);
        end = QueryTo(*i);

        //cout<<"QueryFrom: "<<start<<endl;
        //cout<<"QueryTo: "<<end<<endl;

        cw = 0;
        CW(start, start, end, start);

        ccw = 0;
        CCW(start, start, end, start);

        //cout<<"d:\n";
        //FOR(i, 1, n)cout<<i<<": "<<d[i]<<endl;

        PutInt(0, min(cw, ccw));
        Send(0);

        //cout<<min(cw, ccw)<<endl;
    }
    }

    //cout<<"byleco\n";

    if(myNode == 0)
    {
        FOR(i, 0, uzyteczneInstancje-1)
        {
            FOR(j, 1, qidies[i].size())
            {
                Receive(i);
                cout<<GetInt(i)<<endl;
            }
        }
    }

    return 0;
}