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

void Host_SendStudents(){
    int CurrentStudentNumber = 1, NumberOfStudentsPerNode = NumberOfStudents() / (NumberOfNodes() - 1);
    for (int i=1; i<NumberOfNodes()-1; i++) {
        for (int j=0; j<NumberOfStudentsPerNode; j++) {
            PutChar (i, 'S');
            PutInt (i, j);
            PutInt (i, CurrentStudentNumber);
            Send (i);

            if (CurrentStudentNumber == FirstNeighbor (CurrentStudentNumber))
                CurrentStudentNumber = SecondNeighbor (CurrentStudentNumber);
            else CurrentStudentNumber = FirstNeighbor (CurrentStudentNumber);
        }
    }
}

void Host_SendRequest(){
    int NumberOfStudentsPerNode = NumberOfStudents() / (NumberOfNodes() - 1);
    int NumberOfStudentsLastNode = NumberOfStudents() - (NumberOfNodes() - 2) * NumberOfStudentsPerNode;
    for (int i=0; i<NumberOfQueries(); i++) {
        int WhereFrom = QueryFrom(i) / NumberOfStudentsPerNode; // Maszyna ktora trzyma info o szukanej liczbie;
        int WhereTo = QueryTo(i) / NumberOfStudentsPerNode;

        WhereFrom = (WhereFrom >= (NumberOfNodes() - 1) ? (NumberOfNodes() - 1) : WhereFrom);
        WhereTo = (WhereTo >= (NumberOfNodes() - 1) ? (NumberOfNodes() - 1) : WhereTo);

        int PlaceFrom, PlaceTo;
        int WhatMachineIsFrom, WhatMachineIsTo;

        PlaceFrom = (WhereFrom == (NumberOfNodes() - 1) ? (QueryFrom(i) - (NumberOfNodes()-2)*NumberOfStudentsPerNode) : (QueryFrom(i) % NumberOfStudentsPerNode));
        PlaceTo = (WhereTo == (NumberOfNodes() - 1) ? (QueryTo(i) - (NumberOfNodes() - 2)*NumberOfStudentsPerNode) : (QueryTo(i) % NumberOfStudentsPerNode));

        PutChar (WhereFrom, 'W');
        PutInt (WhereFrom, PlaceFrom);
        Send (WhereFrom);

        Receive (WhereFrom);
        WhatMachineIsFrom = GetInt(WhereFrom);

        PutChar (WhereTo, 'W');
        PutInt (WhereTo, PlaceTo);
        Send (WhereTo);

        Receive (WhereTo);
        WhatMachineIsTo = GetInt(WhereTo);

        int PlaceOfFrom, PlaceOfTo;

        PutChar (WhatMachineIsFrom, 'P');
        PutInt (WhatMachineIsFrom, QueryFrom (i));
        Send (WhatMachineIsFrom);

        Receive (WhatMachineIsFrom);
        PlaceOfFrom = GetInt (WhatMachineIsFrom);

        PutChar (WhatMachineIsTo, 'P');
        PutInt (WhatMachineIsTo, QueryTo (i));
        Send (WhatMachineIsTo);

        Receive (WhatMachineIsTo);
        PlaceOfTo = GetInt (WhatMachineIsTo);

        if (WhatMachineIsFrom == WhatMachineIsTo) {
            cout << abs (PlaceOfTo - PlaceOfFrom) << '\n';
            continue;
        }
        if (WhatMachineIsFrom > WhatMachineIsTo) {
            swap (WhatMachineIsFrom, WhatMachineIsTo);
            swap (PlaceOfTo, PlaceOfFrom);
        }

        int FirstWay, SecondWay;
        FirstWay = (NumberOfStudentsPerNode-1-PlaceFrom) + PlaceTo + (WhatMachineIsTo - WhatMachineIsFrom - 1)*NumberOfStudentsPerNode + (WhatMachineIsTo - WhatMachineIsFrom);
        SecondWay = PlaceFrom + MyNodeId()*NumberOfStudentsPerNode + MyNodeId() + (WhatMachineIsTo == (NumberOfNodes() - 1) ? (NumberOfStudentsLastNode-PlaceTo) : (NumberOfStudentsPerNode-PlaceTo + (NumberOfNodes() - 2 - MyNodeId()*NumberOfStudentsPerNode + (NumberOfNodes() - 2 - MyNodeId()) + NumberOfStudentsLastNode)));

        cout << min (FirstWay, SecondWay) << '\n';
    }
}

void Host_ShutDownMachines(){
    for (int i=1; i<NumberOfNodes(); i++) {
        PutChar (i, 'E');
        Send(i);
    }
}

void ImHost(){
    Host_SendStudents();
    Host_SendRequest();
    Host_ShutDownMachines();
}

vector <int> NH_NewStudent (vector <int> MyStudents, int NodeId){
    int NumberOfStudentsPerNode = NumberOfStudents() / (NumberOfNodes() - 1);
    int Place, StudentNumber;
    Place = GetInt (NodeId);
    StudentNumber = GetInt (NodeId);

    MyStudents[Place] = StudentNumber;

    int NodeIdOfInfo = ((StudentNumber % NumberOfStudentsPerNode == 0) ? (StudentNumber / NumberOfStudentsPerNode) : ((StudentNumber / NumberOfStudents()) + 1));
    if (NodeIdOfInfo > (NumberOfNodes()-1)) NodeIdOfInfo = (NumberOfNodes()-1);

    int PlaceOfInfo = (NodeIdOfInfo == (NumberOfNodes() - 1) ? (StudentNumber - ((NumberOfNodes()-2)*NumberOfStudentsPerNode)) : (StudentNumber % NumberOfStudentsPerNode));

    PutChar (NodeIdOfInfo, 'L');
    PutInt (NodeIdOfInfo, PlaceOfInfo);
    Send (NodeIdOfInfo);

    return MyStudents;
}

void NH_WhereAreStudents (vector <int> WhereAreStudents, int NodeId) {
    int Place = GetInt (NodeId);
    PutInt (NodeId, WhereAreStudents[Place]);
    Send (NodeId);
}

vector <int> NH_WhereAreStudentsUpdate (vector <int> WhereAreStudents, int NodeId) {
    int Place = GetInt (NodeId);
    WhereAreStudents[Place] = NodeId;
    return WhereAreStudents;
}

void NH_PlaceOfStudent (vector <int> MyStudents, int NodeId) {
    int StudentNumber = GetInt (NodeId);
    int StudentPlace;
    vector <int>::iterator it = upper_bound (MyStudents.begin(), MyStudents.end(), StudentNumber);
    StudentPlace = it - MyStudents.begin();

    PutInt (NodeId, StudentPlace);
    Send (NodeId);
}

void ImNotHost(){
    vector <int> MyStudents, WhereAreStudents;
    int NumberOfStudentsPerNode = NumberOfStudents() / (NumberOfNodes() - 1);
    int NumberOfStudentsLastNode = NumberOfStudents() - (NumberOfNodes() - 2) * NumberOfStudentsPerNode;

    if (MyNodeId() == NumberOfNodes()-1) {
        MyStudents.resize(NumberOfStudentsLastNode, 0);
        WhereAreStudents.resize(NumberOfStudentsLastNode, 0);
    }

    else {
        MyStudents.resize(NumberOfStudentsPerNode, 0);
        WhereAreStudents.resize(NumberOfStudentsPerNode, 0);
    }

    char Command;
    int NodeId;
    while (true) {
        NodeId = Receive (-1);
        if (Command == 'E') return;
        if (Command == 'S') MyStudents = NH_NewStudent (MyStudents, NodeId);
        if (Command == 'W') NH_WhereAreStudents (WhereAreStudents, NodeId);
        if (Command == 'L') WhereAreStudents = NH_WhereAreStudentsUpdate (WhereAreStudents, NodeId);
        if (Command == 'P') NH_PlaceOfStudent(MyStudents, NodeId);
    }
}

int main(){
    if (MyNodeId() == 0) ImHost();
    else ImNotHost();
    return 0;
}