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
#include <stdio.h>
#include <stdlib.h>
#include "message.h"
#include "kollib.h"

int main()
{
    int F[20] = { 0 };
    int T[20] = { 0 };
    int Fp[20] = { 0 };
    int Tp[20] = { 0 };
    int dist[200] = { 0 };
    int pyt, FTsize = 0, i, ilWstaw = 0, wstawiono = 0, j;
    int n, np, lInd, rInd;
    int prevLeftNode, prevRightNode, leftNode, rightNode, temp;
    int distSize = NumberOfQueries();
    int nodes = NumberOfNodes();
    int toReceive, received, diff;

    n = NumberOfStudents();
    np = n/2;
    if (MyNodeId() + 1 > distSize) {
        return 0;
    }
    for (pyt = MyNodeId() + 1; pyt <= distSize; pyt += nodes) {
        F[FTsize] = QueryFrom(pyt);
        T[FTsize] = QueryTo(pyt);
        if (F[FTsize] == F[0]) {
            Fp[FTsize] = 1;
            ++wstawiono;
        }
        if (T[FTsize] == F[0]) {
            Tp[FTsize] = 1;
            ++wstawiono;
        }
        ++FTsize;
    }
    ilWstaw = 2 * FTsize;
    prevLeftNode = F[0];
    prevRightNode = F[0];
    leftNode = FirstNeighbor(prevLeftNode);
    rightNode = SecondNeighbor(prevRightNode);
    lInd = 2;
    rInd = n;
    for(i = 0; i < np && wstawiono < ilWstaw; ++i) {
        for (j = 0; j < FTsize; ++j) {
            if (F[j] == leftNode) {
                Fp[j] = lInd;
                ++wstawiono;
            }
            if (T[j] == leftNode) {
                Tp[j] = lInd;
                ++wstawiono;
            }
            if (F[j] == rightNode) {
                Fp[j] = rInd;
                ++wstawiono;
            }
            if (T[j] == rightNode) {
                Tp[j] = rInd;
                ++wstawiono;
            }
        }
        temp = FirstNeighbor(leftNode);
        if (temp != prevLeftNode) {
            prevLeftNode = leftNode;
            leftNode = temp;
        } else {
            prevLeftNode = leftNode;
            leftNode = SecondNeighbor(leftNode);
        }
        temp = FirstNeighbor(rightNode);
        if (temp != prevRightNode) {
            prevRightNode = rightNode;
            rightNode = temp;
        } else {
            prevRightNode = rightNode;
            rightNode = SecondNeighbor(rightNode);
        }
        ++lInd;
        --rInd;
    }
    if (MyNodeId() != 0) {
        for (i = 0; i < FTsize; ++i) {
            diff = Fp[i] > Tp[i] ? Fp[i] - Tp[i] : Tp[i] - Fp[i];
            if (diff > np) {
                PutInt(0, (n - diff));
            } else {
                PutInt(0, diff);
            }
        }
        Send(0);
    } else {
        for (i = 0; i < FTsize; ++i) {
            diff = Fp[i] > Tp[i] ? Fp[i] - Tp[i] : Tp[i] - Fp[i];
            if (diff > np) {
                dist[i * nodes] = n - diff;
            } else {
                dist[i * nodes] = diff;
            }
        }
        toReceive = nodes < distSize ? nodes - 1 : distSize - 1;
        for (i = 0; i < toReceive; ++i) {
            received = Receive(-1);
            for (pyt = received; pyt < distSize; pyt += nodes) {
                dist[pyt] = GetInt(received);
            }
        }
        for (i = 0; i < distSize; i++) {
            printf("%d\n", dist[i]);
        }
    }


    return 0;
}