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
#include "message.h"
#include "kollib.h"
#include <unordered_set>
#include <random>
#include <chrono>
#include <unordered_map>
#include <algorithm>

#define MASTER_NODE 0
#define MAX_NB_CHECKPOINTS 1000
#define KILL_SIGNAL -9
#define OK_SIGNAL -5
#define DEMAND_FOR_RESPONSE -20
#define DEMAND_FREQUENCY 20
#define MESSAGE_LIMIT 1000

using namespace std;

int myNodeId;
int numberOfNodes;
int numberOfStudents;

struct CheckPoint{
    int studentId;

    int firstDistance;
    int firstPivotId;

    int secondDistance;
    int secondPivotId;
    CheckPoint(){}

    CheckPoint(int studentId, int firstDistance, int firstPivotId, int secondDistance, int secondPivotId) :
        studentId(studentId),
        firstDistance(firstDistance), firstPivotId(firstPivotId),
        secondDistance(secondDistance), secondPivotId(secondPivotId){}

    CheckPoint(int studentId)
        : studentId(studentId), firstDistance(-1), firstPivotId(-1),secondDistance(-1), secondPivotId(-1){}

    bool operator<(const CheckPoint& rhs) const{
        return studentId < rhs.studentId;
    }
    bool operator==(const CheckPoint& rhs) const{
        return studentId == rhs.studentId;
    }
};

struct Hash {
    size_t operator()(const CheckPoint &checkPoint) const {
        return checkPoint.studentId;
    }
};

unordered_set<CheckPoint, Hash> checkPoints;
unordered_set<CheckPoint, Hash> filledCheckPoints;

//masterNode--------------------------------------------------------------------------------
void generateRandomAndSend(){
    unsigned seed = chrono::system_clock::now().time_since_epoch().count();
    default_random_engine generator(seed);
    uniform_int_distribution<int> distribution(1, numberOfStudents);

    int numberOfQueries = NumberOfQueries();
    for(int i=1; i<=numberOfQueries; i++){
        checkPoints.insert(CheckPoint(QueryFrom(i)));
        checkPoints.insert(CheckPoint(QueryTo(i)));
    }

    int checkPointsCapacity = min(MAX_NB_CHECKPOINTS, numberOfStudents);
    while(checkPoints.size() < checkPointsCapacity){
        checkPoints.insert(CheckPoint(distribution(generator)));
    }

        int nodeId = 1;
        for (const auto& checkPoint: checkPoints) {
            PutInt(nodeId, checkPoint.studentId);

            PutInt(nodeId, checkPoints.size());
           for(const auto& checkPoint: checkPoints){
               PutInt(nodeId, checkPoint.studentId);
            }
            Send(nodeId++);
            if(nodeId == numberOfNodes) break;
        }
}//------------------------------------------------------------------------------------------


void sendKillAll(){
    for(int i=1; i<numberOfNodes; i++){
        PutInt(i, KILL_SIGNAL);
        Send(i);
    }
}

bool receiveCheckPoints(){
    while(checkPoints.size() > filledCheckPoints.size()){
        int sender = Receive(-1);
        bool demandForResponse = GetInt(sender) == DEMAND_FOR_RESPONSE;
        CheckPoint checkPoint;
        checkPoint.studentId = GetInt(sender);
        checkPoint.firstDistance = GetInt(sender);
        checkPoint.firstPivotId = GetInt(sender);
        checkPoint.secondDistance =GetInt(sender);
        checkPoint.secondPivotId = GetInt(sender);
        filledCheckPoints.insert(checkPoint);

        //printf("STUDENT_ID: %d", checkPoint.studentId);
        //printf("NbOfChecksFilled: %d", (int)filledCheckPoints.size());

        if(demandForResponse){
            if(checkPoints.size() > filledCheckPoints.size()){
                PutInt(sender, OK_SIGNAL);
                Send(sender);
            }
            else break;
        }
    }

    sendKillAll();
}

int findDistanceFirst(int start, int end){
    int distance = 0;
    int source = -1;
    while(start != end){
        CheckPoint checkPoint = *filledCheckPoints.find(CheckPoint(start));
        if(checkPoint.firstPivotId == source){
            source = start;
            start = checkPoint.secondPivotId;
            distance+=checkPoint.secondDistance;
        }
        else{
            source = start;
            start = checkPoint.firstPivotId;
            distance+=checkPoint.firstDistance;
        }

    }
    return distance;
}

int findDistanceSecond(int start, int end){
    int distance = 0;
    int source = -1;
    while(start != end){
        CheckPoint checkPoint = *filledCheckPoints.find(CheckPoint(start));
        if(checkPoint.secondPivotId == source){
            source = start;
            start = checkPoint.firstPivotId;
            distance+=checkPoint.firstDistance;
        }
        else{
            source = start;
            start = checkPoint.secondPivotId;
            distance+=checkPoint.secondDistance;
        }
    }
    return distance;
}


void masterNodeFacade(){
    generateRandomAndSend();
    receiveCheckPoints();
    int nbOfQueries = NumberOfQueries();
    for(int i=1; i<=nbOfQueries; i++){


        int from = QueryFrom(i);
        int to = QueryTo(i);
        printf("%d\n", min(findDistanceFirst(from, to), findDistanceSecond(from, to)));
        //printf("%d\n", findDistanceFirst(from, to));
    }

    //puts("MASTER KONIEC");

    //printf("MASTER! %d of %d with %d students, nbCh: %d", myNodeId, numberOfNodes, numberOfStudents, (int)checkPoints.size());
}

//-----------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------


//slaveNode--------------------------------------------------------------------------------
int receiveStartStudent(){
    Receive(MASTER_NODE);
    int startStudent = GetInt(MASTER_NODE);
    return startStudent;
}

void receiveAndFillCheckPoints(){
    int numberOfCheckPoints = GetInt(MASTER_NODE);
    for(int i=0; i<numberOfCheckPoints; i++){
        int checkPoint = GetInt(MASTER_NODE);
        checkPoints.insert(CheckPoint(checkPoint));
    }
}

int requestCounter;
bool sendCheckPoint(const CheckPoint &checkPoint){
    if(requestCounter >= MESSAGE_LIMIT) return true;

    if(requestCounter%DEMAND_FREQUENCY == 0) PutInt(MASTER_NODE, DEMAND_FOR_RESPONSE);
    else PutInt(MASTER_NODE, OK_SIGNAL);

    PutInt(MASTER_NODE, checkPoint.studentId);
    PutInt(MASTER_NODE, checkPoint.firstDistance);
    PutInt(MASTER_NODE, checkPoint.firstPivotId);
    PutInt(MASTER_NODE, checkPoint.secondDistance);
    PutInt(MASTER_NODE, checkPoint.secondPivotId);
    Send(MASTER_NODE);

    if(requestCounter%DEMAND_FREQUENCY == 0){
        Receive(MASTER_NODE);
        return GetInt(MASTER_NODE) == KILL_SIGNAL;
    }
    return false;
}


void process(int start){
    unsigned seed = chrono::system_clock::now().time_since_epoch().count();
    default_random_engine generator(seed);
    uniform_int_distribution<int> distribution(0, 1);

    int direction = distribution(generator);

    int firstCheckPoint = start;
    CheckPoint middleCheckPoint = CheckPoint(-1);
    int lastCheckPoint = -1;

    int lastStudent = start;
    int activeStudent;

    if(direction) activeStudent = FirstNeighbor(lastStudent);
    else activeStudent = SecondNeighbor(lastStudent);

    if(checkPoints.find(CheckPoint(activeStudent)) != checkPoints.end()){
        middleCheckPoint.studentId = activeStudent;
        middleCheckPoint.firstPivotId = firstCheckPoint;
        middleCheckPoint.firstDistance = 1;
    }

    int distance = 1;
    while(true){
        int newStudent = FirstNeighbor(activeStudent);
        if(newStudent == lastStudent) newStudent = SecondNeighbor(activeStudent);
        lastStudent = activeStudent;
        activeStudent = newStudent;

        if(checkPoints.find(CheckPoint(newStudent)) != checkPoints.end()){
            if(middleCheckPoint.studentId == -1){
                middleCheckPoint.studentId = newStudent;
                middleCheckPoint.firstPivotId = firstCheckPoint;
                middleCheckPoint.firstDistance = distance;
            }
            else{
                middleCheckPoint.secondDistance = distance;
                middleCheckPoint.secondPivotId = newStudent;

                if(sendCheckPoint(middleCheckPoint))
                    return;
                requestCounter++;

                lastCheckPoint = middleCheckPoint.studentId;
                middleCheckPoint.studentId = newStudent;
                middleCheckPoint.firstDistance = middleCheckPoint.secondDistance;
                middleCheckPoint.firstPivotId = lastCheckPoint;
                middleCheckPoint.secondDistance = 0;
                middleCheckPoint.secondPivotId = 0;
            }
            distance = 0;
        }
        distance++;
    }

}



void slaveNodeFacade(){
    requestCounter = 0;
    int startStudent = receiveStartStudent();
    if(startStudent == -1) return;
    receiveAndFillCheckPoints();

    process(startStudent);

    //puts("SLAVE KONIEC");
    //printf("SLAVE! %d of %d with %d students and start is %d, nbCh: %d",
   //        myNodeId, numberOfNodes, numberOfStudents, startStudent, (int)checkPoints.size());
}




//-----------------------------------------------------------------------------------------
int main(){
    myNodeId = MyNodeId();
    numberOfNodes = min(NumberOfNodes(), NumberOfStudents());
    numberOfStudents = NumberOfStudents();

    if(myNodeId == MASTER_NODE){
        masterNodeFacade();
    }else if(myNodeId < numberOfNodes){
        slaveNodeFacade();
    }
    //puts("SLAVE_KONIEC");
    return 0;
}