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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <iostream>
#include "message.h"
#include "kollib.h"

using namespace std;
struct start_stop
{
    int first_student;
    int prev_student;
    int last_student;
    int students;
};

start_stop tab[100];

struct node_info
{
    int from_distance;
    int to_distance;
};

struct query_info
{
    int result;
    node_info nodes[100];
};

query_info queries[200];

void simple_main_thread()
{
    long number_of_students = NumberOfStudents();
    int number_of_queries = NumberOfQueries();
    int number_of_nodes = NumberOfNodes();
    int node = 1;
    int queries_to_node[200];

    //cout<<"NumberOfStudents:"<<number_of_students<<endl;
    //cout<<"NumberOfQueries"<<number_of_queries<<endl;
    //cout<<"NumberOfNodes"<<number_of_nodes<<endl;

    for(int i=0; i<number_of_queries; i++)
        queries_to_node[i] = -1;

    for(int i=0; i<number_of_queries; i++)
    {

        long from = QueryFrom(i+1);
        long to = QueryTo(i+1);

        if(from != to)
        {
            PutInt(node, i+1);
            Send(node);
            queries_to_node[i] = node;
            node = (node+1)%number_of_nodes;
            if(0 == node)
                node++;
        }
    }


    for(int i=0; i<number_of_queries; i++)
    {
        if(-1 != queries_to_node[i])
        {
            Receive(queries_to_node[i]);
            cout<<GetInt(queries_to_node[i])<<endl;
        }
        else
        {
            cout<<"0"<<endl;
        }
    }

    for(int i=1; i<number_of_nodes; i++)
    {
        PutInt(i, -1);
        Send(i);
    }
}

void advanced_main_thread()
{
    long number_of_students = NumberOfStudents();
    int number_of_queries = NumberOfQueries();
    int number_of_nodes = NumberOfNodes();

    int number_of_requests = 0;

    int query;
    int neighbour, last_n, next_n;

    tab[0].first_student = 1;
    tab[0].prev_student = SecondNeighbor(1);

    neighbour = 1;
    int count = 0;
    int node = 0;
    do
    {
        count++;
        next_n = FirstNeighbor(neighbour);
        if(last_n == next_n)
        {
            next_n = SecondNeighbor(neighbour);
        }

        if(count == number_of_students/(number_of_nodes-1) && node < number_of_nodes-2)
        {
            tab[node].last_student = next_n;
            tab[node].students = count;
            //cout<<"NodeID:"<<node+1<<"last_student"<<tab[node].last_student<<" students:"<<count<<endl;
            node++;
            tab[node].first_student = next_n;
            tab[node].prev_student = neighbour;
            count = 0;
        }

        last_n = neighbour;
        neighbour = next_n;

    }while(neighbour !=1);

    tab[node].last_student = 1;
    tab[node].students = count;
    //cout<<"NodeID:"<<node+1<<"last_student"<<tab[node].last_student<<" students:"<<tab[node].students<<endl;

    for(int i=1; i<number_of_nodes; i++)
    {
        PutInt(i, tab[i-1].first_student);
        PutInt(i, tab[i-1].prev_student);
        PutInt(i, tab[i-1].last_student);
        Send(i);
    }

    //cout<<"NumberOfStudents:"<<number_of_students<<endl;
    //cout<<"NumberOfQueries"<<number_of_queries<<endl;
    //cout<<"NumberOfNodes"<<number_of_nodes<<endl;
    number_of_requests = number_of_queries;
    for(query=1; query<=number_of_queries; query++)
    {

        queries[query-1].result = -1;

        long from = QueryFrom(query);
        long to = QueryTo(query);

        if(from == to)
        {
            queries[query-1].result = 0;
            number_of_requests--;
        }
    }

    number_of_requests*=(number_of_nodes-1);

    for(int i=0; i<number_of_requests; i++)
    {
            node = Receive(-1);
            //cout<<"Response from node "<<node<<endl;
            query = GetInt(node);
            queries[query-1].nodes[node-1].from_distance = GetInt(node);
            queries[query-1].nodes[node-1].to_distance = GetInt(node);

            if(queries[query-1].nodes[node-1].from_distance != -1 &&
               queries[query-1].nodes[node-1].to_distance != -1)
            {
                queries[query-1].result=queries[query-1].nodes[node-1].from_distance-queries[query-1].nodes[node-1].to_distance;
                if(queries[query-1].result<0)
                    queries[query-1].result*=-1;
            }
    }

    int distance = -1;

    for(query=0; query<number_of_queries; query++, distance = -1)
    {
        //cout<<"Query:"<<query+1<<endl;
        if(queries[query].result >=0)
            cout<<queries[query].result<<endl;
        else
        {
            for(int i=0; i<number_of_nodes-1; i++)
            {
                if(distance>=0)
                {
                    if(queries[query].nodes[i].from_distance == -1 &&
                       queries[query].nodes[i].to_distance == -1)
                    {
                        distance+=tab[i].students;
                        //cout<<"add from node"<<i+1<<" value:"<<tab[i].students<<endl;

                    }else if(queries[query].nodes[i].to_distance >=0)
                    {
                        distance+=queries[query].nodes[i].to_distance;
                        //cout<<"add from node"<<i+1<<" value:"<<queries[query].nodes[i].to_distance<<endl;
                        break;
                    }
                    else
                    {
                        distance+=queries[query].nodes[i].from_distance;
                        //cout<<"add from node"<<i+1<<" value:"<<queries[query].nodes[i].from_distance<<endl;
                        break;
                    }

                }else
                {
                    if(queries[query].nodes[i].from_distance != -1 &&
                       queries[query].nodes[i].to_distance != -1)
                    {
                        distance=queries[query].nodes[i].from_distance-queries[query].nodes[i].to_distance;
                        if(distance<0)
                            distance*=-1;
                        break;

                    }else if(queries[query].nodes[i].to_distance >=0)
                    {
                        distance=tab[i].students-queries[query].nodes[i].to_distance;
                        //cout<<"set from node"<<i+1<<" value:"<<queries[query].nodes[i].to_distance<<endl;
                    }
                    else if(queries[query].nodes[i].from_distance >=0)
                    {
                        distance=tab[i].students-queries[query].nodes[i].from_distance;
                        //cout<<"set from node"<<i+1<<" value:"<<queries[query].nodes[i].from_distance<<endl;
                    }
                }
            }

            if(distance>number_of_students-distance)
                distance = number_of_students - distance;

            cout<<distance<<endl;
        }

    }


    for(int i=1; i<number_of_nodes; i++)
    {
        PutInt(i, -1);
        Send(i);
    }
}

void simple_worker_thread()
{
    long number_of_students = NumberOfStudents();

    int query = -1;
    long from = -1;
    long to = -1;
    long neighbour = -1;
    long next_n = -1;
    long last_n = -1;
    long distance = 0;
    long result = 0;

    //cout<<"NodeID:"<<MyNodeId()<<endl;
    while(true)
    {
        Receive(0);
        query = GetInt(0);
        if(query == -1)
            return;
        //cout<<"NodeID:"<<MyNodeId()<<" query:"<<query<<endl;
        from = QueryFrom(query);
        to = QueryTo(query);
        neighbour = from;
        next_n = -1;
        last_n = -1;
        distance = 0;

        while(neighbour != to)
        {
            next_n = FirstNeighbor(neighbour);
            if(last_n == next_n)
            {
                next_n = SecondNeighbor(neighbour);
            }

            last_n = neighbour;
            neighbour = next_n;
            distance++;
        }

        if(distance <= number_of_students-distance)
            result = distance;
        else
            result = number_of_students-distance;

        PutInt(0, result);
        Send(0);

    }
}

void advanced_worker_thread()
{
    int first_student;
    int prev_student;
    int last_student;

    long from_distance;
    long to_distance;
    int number_of_queries = NumberOfQueries();

    int query;
    int from = -1;
    int to = -1;
    long neighbour = -1;
    long next_n = -1;
    long last_n = -1;
    long distance = 0;

    Receive(0);
    first_student = GetInt(0);
    prev_student = GetInt(0);
    last_student = GetInt(0);

    //cout<<"NodeID:"<<MyNodeId()<<" search from:"<<first_student<<" to:"<<last_student<<endl;

    for(query=1; query<=number_of_queries; query++)
    {
        from = QueryFrom(query);
        to = QueryTo(query);
        if(from == to)
            continue;

        //cout<<"NodeID:"<<MyNodeId()<<" query:"<<query<<" from:"<<from<<" to:"<<to<<endl;

        neighbour = first_student;
        next_n = -1;
        last_n = prev_student;
        from_distance = -1;
        to_distance = -1;
        distance = 0;

        while(neighbour != last_student && (from_distance == -1 || to_distance == -1))
        {
            if(neighbour == from)
                from_distance = distance;

            if(neighbour == to)
                to_distance = distance;

            next_n = FirstNeighbor(neighbour);
            if(last_n == next_n)
            {
                next_n = SecondNeighbor(neighbour);
            }

            last_n = neighbour;
            neighbour = next_n;
            distance++;
        }
        //cout<<"NodeID:"<<MyNodeId()<<" from_distance:"<<from_distance<<" to_distance:"<<to_distance<<endl;

        PutInt(0, query);
        PutInt(0, from_distance);
        PutInt(0, to_distance);
        Send(0);

    }
}

int main()
{
    int my_id = MyNodeId();
    int number_of_students = NumberOfStudents();
    if(my_id==0)
    {
        if(number_of_students<20000)
            simple_main_thread();
        else
            advanced_main_thread();
    }
    else
    {
        if(number_of_students<20000)
            simple_worker_thread();
        else
            advanced_worker_thread();
    }

    return 0;
}