#include "kollib.h"
#include "message.h"
#include <iostream>
using namespace std;
class Chodzenie{
public:
int start = 0;
int poprzedni = 0;
int obecny = 0;
int koniec = 0;
int dlugoscDrogi = 0;
Chodzenie(){
}
void setZapytanie(int start, int koniec){
this->start = start;
this->koniec = koniec;
this->obecny = start;
this->dlugoscDrogi = 0;
}
bool idzDalej(){ // true jesli doszlo
if (obecny == koniec)
return true;
++dlugoscDrogi;
int pierwszySasiad = FirstNeighbor(obecny);
int drugiSasiad = SecondNeighbor(obecny);
if (czyToPoczatek()){
if (pierwszySasiad == koniec || drugiSasiad == koniec)
return true;
poprzedni = start;
obecny = pierwszySasiad;
}
else{
if (pierwszySasiad == poprzedni){
poprzedni = obecny;
obecny = drugiSasiad;
}
else{
poprzedni = obecny;
obecny = pierwszySasiad;
}
}
return false;
}
bool czyToPoczatek(){
return dlugoscDrogi == 0;
}
};
int main() {
int N = NumberOfQueries();
int poczatek = ((MyNodeId() * N) / NumberOfNodes())+1;
int koniec = (((MyNodeId() + 1) * N) / NumberOfNodes())+1;
int liczbaUczniow = NumberOfStudents();
Chodzenie h = Chodzenie();
PutInt(0, koniec-poczatek);
for (int i = poczatek; i < koniec; ++i) {
h.setZapytanie(QueryFrom(i), QueryTo(i));
while(!h.idzDalej()){
}
int droga = min(liczbaUczniow-h.dlugoscDrogi, h.dlugoscDrogi);
if (MyNodeId() > 0) {
PutInt(0, droga);
}
else {
cout << droga << endl;
}
}
if (MyNodeId() > 0){
Send(0);
}
else{
for (int instancja = 1; instancja < NumberOfNodes(); ++instancja) {
Receive(instancja);
int iloscWyslanychDanych = GetInt(instancja);
for (int i = 0; i<iloscWyslanychDanych; ++i){
cout << GetInt(instancja) << endl;
}
}
}
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 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 | #include "kollib.h" #include "message.h" #include <iostream> using namespace std; class Chodzenie{ public: int start = 0; int poprzedni = 0; int obecny = 0; int koniec = 0; int dlugoscDrogi = 0; Chodzenie(){ } void setZapytanie(int start, int koniec){ this->start = start; this->koniec = koniec; this->obecny = start; this->dlugoscDrogi = 0; } bool idzDalej(){ // true jesli doszlo if (obecny == koniec) return true; ++dlugoscDrogi; int pierwszySasiad = FirstNeighbor(obecny); int drugiSasiad = SecondNeighbor(obecny); if (czyToPoczatek()){ if (pierwszySasiad == koniec || drugiSasiad == koniec) return true; poprzedni = start; obecny = pierwszySasiad; } else{ if (pierwszySasiad == poprzedni){ poprzedni = obecny; obecny = drugiSasiad; } else{ poprzedni = obecny; obecny = pierwszySasiad; } } return false; } bool czyToPoczatek(){ return dlugoscDrogi == 0; } }; int main() { int N = NumberOfQueries(); int poczatek = ((MyNodeId() * N) / NumberOfNodes())+1; int koniec = (((MyNodeId() + 1) * N) / NumberOfNodes())+1; int liczbaUczniow = NumberOfStudents(); Chodzenie h = Chodzenie(); PutInt(0, koniec-poczatek); for (int i = poczatek; i < koniec; ++i) { h.setZapytanie(QueryFrom(i), QueryTo(i)); while(!h.idzDalej()){ } int droga = min(liczbaUczniow-h.dlugoscDrogi, h.dlugoscDrogi); if (MyNodeId() > 0) { PutInt(0, droga); } else { cout << droga << endl; } } if (MyNodeId() > 0){ Send(0); } else{ for (int instancja = 1; instancja < NumberOfNodes(); ++instancja) { Receive(instancja); int iloscWyslanychDanych = GetInt(instancja); for (int i = 0; i<iloscWyslanychDanych; ++i){ cout << GetInt(instancja) << endl; } } } return 0; } |
English