Advertisement
bartekltg

kol

May 16th, 2014
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <utility>
  5. #include <set>
  6. #include <functional>
  7. #include <random>
  8. #include <stack>
  9. #include <map>
  10.  
  11.  
  12.  
  13. #include "kollib.h"
  14. #include "message.h"
  15.  
  16.  
  17. using namespace std;
  18.  
  19.  
  20. class krawedz
  21. {public:
  22.     int dokad;
  23.     int odleglosc;
  24. };
  25.  
  26. pair<int, int> szukaj( int aktualny ,int poprzedni,const set<int> &spec )
  27. {
  28.     int licznik = 1;
  29.     while ( spec.count(aktualny)==0 )
  30.     {
  31.         int next = FirstNeighbor(aktualny);
  32.         if (next == poprzedni)
  33.             next = SecondNeighbor(aktualny);
  34.         poprzedni = aktualny;
  35.         aktualny = next;
  36.         licznik++;
  37.     }
  38.     return make_pair(aktualny, licznik);
  39. }
  40.  
  41. int main()
  42. {
  43.  
  44.     int n = NumberOfStudents();
  45.  
  46.     int m = NumberOfQueries();
  47.  
  48.     vector<pair<int,int>> zapytania;
  49.     set <int> spec;
  50.     vector<int> pytaj_o;
  51.  
  52.     map<int, pair<krawedz,krawedz> > krawedzie;
  53.  
  54.     for (int i=1;i<=m;i++)
  55.     {
  56.         int f,t;
  57.         f = QueryFrom(i);
  58.         t = QueryTo(i);
  59.         zapytania.emplace_back(f,t);
  60.         spec.insert(t);
  61.         spec.insert(f);
  62.     }
  63.  
  64.     mt19937 gen(145788);
  65.  
  66.     for (int i=0;i<900-spec.size();i++)
  67.     {
  68.         int w;
  69.         std::uniform_int_distribution<int> dis(1,n);
  70.         w = dis(gen);
  71.  
  72.         spec.insert(w);
  73.  
  74.     }
  75.     pytaj_o.resize(spec.size());
  76.     copy(spec.begin(),spec.end(),pytaj_o.begin());
  77.  
  78.     if (MyNodeId()==0) //hub
  79.     {
  80.         vector<int>::iterator pytaj = pytaj_o.begin();
  81.  
  82.         int wolne = NumberOfNodes()-1;
  83.  
  84.         for (int i=1;i<NumberOfNodes();i++)
  85.         {
  86.             if (pytaj!=pytaj_o.end())
  87.             {
  88.                 PutInt(i,*pytaj);
  89.                 ++pytaj;
  90.                 --wolne;
  91.             }
  92.             else
  93.             {
  94.                 PutInt(i,-1);
  95.             }
  96.             Send(i);
  97.         }
  98.  
  99.         while ( wolne < NumberOfNodes()-1 )
  100.         {
  101.             int kto = Receive(-1);
  102.             int sasiad1, sasiad2, odl1, odl2, student ;
  103.  
  104.             student = GetInt(kto);
  105.             sasiad1 = GetInt(kto);
  106.             odl1    = GetInt(kto);
  107.             sasiad2 = GetInt(kto);
  108.             odl2    = GetInt(kto);
  109.  
  110.            krawedz k1, k2;
  111.             k1.dokad=sasiad1;
  112.             k1.odleglosc=odl1;
  113.             k2.dokad=sasiad2;
  114.             k2.odleglosc=odl2;
  115.  
  116.             krawedzie[student] = make_pair(k1,k2);
  117.  
  118.             if (pytaj!=pytaj_o.end())
  119.             {
  120.                 PutInt(kto,*pytaj);
  121.                 Send(kto);
  122.                 ++pytaj;
  123.             }
  124.             else
  125.             {
  126.                 ++wolne;
  127.                 PutInt(kto,-1);
  128.                 Send(kto);
  129.             }
  130.        }
  131.  
  132.         int student = pytaj_o[0];
  133.         int pierwszy = student;
  134.         int poprz =-1;
  135.         map<int,int> pozycje;
  136.         int pozycja=0;
  137.         pozycje[student]=0;
  138.         do
  139.         {
  140.             int nast = krawedzie[student].first.dokad;
  141.             int odl = krawedzie[student].first.odleglosc;
  142.             if (nast==poprz)
  143.             {
  144.                 nast = krawedzie[student].second.dokad;
  145.                 odl = krawedzie[student].second.odleglosc;
  146.             }
  147.             pozycja += odl;
  148.             pozycje[nast]= pozycja;
  149.             poprz=student;
  150.             student = nast;
  151.         }
  152.         while ( student!=pierwszy );
  153.  
  154.  
  155.         for (int i=0;i<zapytania.size();i++)
  156.         {
  157.             int odl = abs(pozycje[zapytania[i].first]-pozycje[zapytania[i].second]);
  158.             if (2*odl>n) odl= n-odl;
  159.  
  160.              cout<<odl<<endl;
  161.  
  162.         }
  163.     }
  164.     else  // MyNodeId() > 0
  165.     {     // robotnik
  166.         for(;;)
  167.         {
  168.             Receive(0);
  169.             int student = GetInt(0);
  170.  
  171.             if (student ==-1) break;
  172.  
  173.             int sasiad1, sasiad2, odl1, odl2 ;
  174.             tie(sasiad1,odl1) = szukaj( FirstNeighbor(student),student, spec );
  175.             tie(sasiad2,odl2) = szukaj( SecondNeighbor(student),student, spec );
  176.  
  177.             PutInt(0,student);
  178.             PutInt(0,sasiad1);
  179.             PutInt(0,odl1);
  180.             PutInt(0,sasiad2);
  181.             PutInt(0,odl2);
  182.             Send(0);
  183.         }
  184.  
  185.     }
  186.  
  187.     return 0;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement