Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <utility>
- #include <set>
- #include <functional>
- #include <random>
- #include <stack>
- #include <map>
- #include "kollib.h"
- #include "message.h"
- using namespace std;
- class krawedz
- {public:
- int dokad;
- int odleglosc;
- };
- pair<int, int> szukaj( int aktualny ,int poprzedni,const set<int> &spec )
- {
- int licznik = 1;
- while ( spec.count(aktualny)==0 )
- {
- int next = FirstNeighbor(aktualny);
- if (next == poprzedni)
- next = SecondNeighbor(aktualny);
- poprzedni = aktualny;
- aktualny = next;
- licznik++;
- }
- return make_pair(aktualny, licznik);
- }
- int main()
- {
- int n = NumberOfStudents();
- int m = NumberOfQueries();
- vector<pair<int,int>> zapytania;
- set <int> spec;
- vector<int> pytaj_o;
- map<int, pair<krawedz,krawedz> > krawedzie;
- for (int i=1;i<=m;i++)
- {
- int f,t;
- f = QueryFrom(i);
- t = QueryTo(i);
- zapytania.emplace_back(f,t);
- spec.insert(t);
- spec.insert(f);
- }
- mt19937 gen(145788);
- for (int i=0;i<900-spec.size();i++)
- {
- int w;
- std::uniform_int_distribution<int> dis(1,n);
- w = dis(gen);
- spec.insert(w);
- }
- pytaj_o.resize(spec.size());
- copy(spec.begin(),spec.end(),pytaj_o.begin());
- if (MyNodeId()==0) //hub
- {
- vector<int>::iterator pytaj = pytaj_o.begin();
- int wolne = NumberOfNodes()-1;
- for (int i=1;i<NumberOfNodes();i++)
- {
- if (pytaj!=pytaj_o.end())
- {
- PutInt(i,*pytaj);
- ++pytaj;
- --wolne;
- }
- else
- {
- PutInt(i,-1);
- }
- Send(i);
- }
- while ( wolne < NumberOfNodes()-1 )
- {
- int kto = Receive(-1);
- int sasiad1, sasiad2, odl1, odl2, student ;
- student = GetInt(kto);
- sasiad1 = GetInt(kto);
- odl1 = GetInt(kto);
- sasiad2 = GetInt(kto);
- odl2 = GetInt(kto);
- krawedz k1, k2;
- k1.dokad=sasiad1;
- k1.odleglosc=odl1;
- k2.dokad=sasiad2;
- k2.odleglosc=odl2;
- krawedzie[student] = make_pair(k1,k2);
- if (pytaj!=pytaj_o.end())
- {
- PutInt(kto,*pytaj);
- Send(kto);
- ++pytaj;
- }
- else
- {
- ++wolne;
- PutInt(kto,-1);
- Send(kto);
- }
- }
- int student = pytaj_o[0];
- int pierwszy = student;
- int poprz =-1;
- map<int,int> pozycje;
- int pozycja=0;
- pozycje[student]=0;
- do
- {
- int nast = krawedzie[student].first.dokad;
- int odl = krawedzie[student].first.odleglosc;
- if (nast==poprz)
- {
- nast = krawedzie[student].second.dokad;
- odl = krawedzie[student].second.odleglosc;
- }
- pozycja += odl;
- pozycje[nast]= pozycja;
- poprz=student;
- student = nast;
- }
- while ( student!=pierwszy );
- for (int i=0;i<zapytania.size();i++)
- {
- int odl = abs(pozycje[zapytania[i].first]-pozycje[zapytania[i].second]);
- if (2*odl>n) odl= n-odl;
- cout<<odl<<endl;
- }
- }
- else // MyNodeId() > 0
- { // robotnik
- for(;;)
- {
- Receive(0);
- int student = GetInt(0);
- if (student ==-1) break;
- int sasiad1, sasiad2, odl1, odl2 ;
- tie(sasiad1,odl1) = szukaj( FirstNeighbor(student),student, spec );
- tie(sasiad2,odl2) = szukaj( SecondNeighbor(student),student, spec );
- PutInt(0,student);
- PutInt(0,sasiad1);
- PutInt(0,odl1);
- PutInt(0,sasiad2);
- PutInt(0,odl2);
- Send(0);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement