#include "kollib.h" #include "message.h" #include <iostream> #include <algorithm> using namespace std; int main() { //if(MyNodeId() 0){ for (int i = MyNodeId() + 1; i <= NumberOfQueries(); i += NumberOfNodes()) { //printf("%d %d\n", i, MyNodeId()); PutInt(0,i); int distance = 1, temp; int begin = QueryFrom(i); int onePath = FirstNeighbor(begin), secondPath = SecondNeighbor(begin); int previousOne = begin, previousSecond = begin; int end = QueryTo(i); if(begin == end){ PutInt(0,0); Send(0); continue; } while(end != onePath && end != secondPath){ //printf("%d %d\n", onePath, secondPath); temp = onePath; onePath = (FirstNeighbor(onePath) == previousOne ? SecondNeighbor(onePath) : FirstNeighbor(onePath)); previousOne = temp; temp = secondPath; secondPath = (FirstNeighbor(secondPath) == previousSecond ? SecondNeighbor(secondPath) : FirstNeighbor(secondPath)); previousSecond = temp; distance++; } PutInt(0,distance); Send(0); } if(MyNodeId() == 0){ //printf("Getting"); int where, what, numberOfComp, instance, geted = 0; int * answers = new int[NumberOfQueries() + 1]; for(int i = 1; i <= NumberOfQueries(); ++i){ instance = Receive(-1); where = GetInt(instance); what = GetInt(instance); answers[where] = what; } for(int i = 1; i <= NumberOfQueries(); ++i) printf("%d\n", answers[i]); } return 0; }
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 | #include "kollib.h" #include "message.h" #include <iostream> #include <algorithm> using namespace std; int main() { //if(MyNodeId() 0){ for (int i = MyNodeId() + 1; i <= NumberOfQueries(); i += NumberOfNodes()) { //printf("%d %d\n", i, MyNodeId()); PutInt(0,i); int distance = 1, temp; int begin = QueryFrom(i); int onePath = FirstNeighbor(begin), secondPath = SecondNeighbor(begin); int previousOne = begin, previousSecond = begin; int end = QueryTo(i); if(begin == end){ PutInt(0,0); Send(0); continue; } while(end != onePath && end != secondPath){ //printf("%d %d\n", onePath, secondPath); temp = onePath; onePath = (FirstNeighbor(onePath) == previousOne ? SecondNeighbor(onePath) : FirstNeighbor(onePath)); previousOne = temp; temp = secondPath; secondPath = (FirstNeighbor(secondPath) == previousSecond ? SecondNeighbor(secondPath) : FirstNeighbor(secondPath)); previousSecond = temp; distance++; } PutInt(0,distance); Send(0); } if(MyNodeId() == 0){ //printf("Getting"); int where, what, numberOfComp, instance, geted = 0; int * answers = new int[NumberOfQueries() + 1]; for(int i = 1; i <= NumberOfQueries(); ++i){ instance = Receive(-1); where = GetInt(instance); what = GetInt(instance); answers[where] = what; } for(int i = 1; i <= NumberOfQueries(); ++i) printf("%d\n", answers[i]); } return 0; } |