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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//
//  main.cpp
//  PA_2014_KOL
//
//  Created by Michal Kowalski on 15/05/14.
//  Copyright (c) 2014 Michal Kowalski. All rights reserved.
//

#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
#include "message.h"
#include "kollib.h"
using namespace std;

//int NumberOfStudents()
//{
//    return 10;
//}
//
//int FirstNeighbor(int i)
//{
//    int first[] = {0,2,1,5,5,3,1,8,3,2,7};
//    return first[i];
//}
//
//int SecondNeighbor(int i)
//{
//    int first[] = {0,6,9,8,6,4,4,10,7,10,9};
//    return first[i];
//}
//
//int NumberOfQueries()
//{
//    return 4;
//}
//
//int QueryFrom(int i)
//{
//    int first[] = {0,7,4,4,9};
//    return first[i];
//}
//
//int QueryTo(int i)
//{
//    int first[] = {0,6,9,4,4};
//    return first[i];
//}

int STUDENTS;
int NODE_ID;
int N_NODES;
int QUERIES;
int Q_TO;
int Q_FROM;

class ST
{
public:
    ST * first;
    ST * second;
};

ST *TAB;

int find_cost(int from,int to);

int AQ[500];
int main()
{
    NODE_ID = MyNodeId();

    N_NODES = NumberOfNodes();

    STUDENTS = NumberOfStudents();

    if ( NODE_ID == 0)///ostatni node
    {
        TAB = new ST[STUDENTS+1];
        int fn,sn;
        for (int x=1 ; x<=STUDENTS; ++x) {
            fn = FirstNeighbor(x);
            sn = SecondNeighbor(x);

            if (fn>sn)
                swap(fn, sn);
            (TAB+x)->first = (TAB+fn);
            (TAB+x)->second = (TAB+sn);
        }
        QUERIES = NumberOfQueries();

        for (int x=1; x<=QUERIES; ++x) {
            if ((x%N_NODES) == NODE_ID)
            {
                Q_FROM = QueryFrom(x);
                Q_TO = QueryTo(x);

                int cost = find_cost(Q_FROM, Q_TO);
                AQ[x]=cost;
            }
        }

        ///pobranie wynikow
        for (int inst=1; inst < N_NODES; ++inst) {
            Receive(inst);
            int n = GetInt(inst);
            for (int nn=0;nn<n;++nn)
            {
                int q = GetInt(inst);
                int cost = GetInt(inst);
                AQ[q]=cost;
            }
        }
        ///wypisanie wynikow
        for (int q =1; q<=QUERIES; ++q) {
            printf("%d\n",AQ[q]);
        }
    }
    else
    {
            TAB = new ST[STUDENTS+1];
            int fn,sn;
            for (int x=1 ; x<=STUDENTS; ++x) {
                fn = FirstNeighbor(x);
                sn = SecondNeighbor(x);
                if (fn>sn)
                    swap(fn, sn);
                (TAB+x)->first = (TAB+fn);
                (TAB+x)->second = (TAB+sn);
            }
            QUERIES = NumberOfQueries();
            vector<pair<int,int> > ans;
            for (int x=1; x<=QUERIES; ++x) {
                if ((x%N_NODES) == NODE_ID)
                {
                    Q_FROM = QueryFrom(x);
                    Q_TO = QueryTo(x);
                    
                    int cost = find_cost(Q_FROM, Q_TO);
                    ans.push_back(pair<int,int>(x,cost));
                }
            }
        PutInt(0, (int)ans.size());
        for (int i=0; i<ans.size(); ++i) {
            PutInt(0, ans[i].first);
            PutInt(0, ans[i].second);
        }
        Send(0);
    }

    return 0;
}


int find_cost(int from,int to)
{
    int cost = 0;
    ST * pST = TAB+from;
    ST * lastST = NULL;
    while(pST != (TAB+to))
    {
        ST *pFirst = pST->first;
        if (pFirst == lastST)
        {
            pFirst = pST->second;
        }
        lastST = pST;
        pST = pFirst;
        cost++;
    }
    return cost;
}