artemgf

Число Исенбаева

Aug 3rd, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include <iostream>
  3. #include <conio.h>
  4. #include <string>
  5. #include <set>
  6. #include <map>
  7. #include <algorithm>
  8. #include <string>
  9. #include <math.h>
  10. #include <vector>
  11. #include <cmath>
  12. #include <sstream>
  13. #include <queue>
  14.  
  15. using namespace std;
  16.  
  17. struct player
  18. {
  19.     string sname;
  20.     bool was;
  21.     map <string, player*> teammades;
  22. };
  23.  
  24. player* NEW(string name)
  25. {
  26.     player* New = new player;
  27.     New->sname = name;
  28.     New->was = false;
  29.  
  30.     return New;
  31. }
  32.  
  33. map <string, player*> friends;
  34.  
  35. void graf(vector <string> data)
  36. {
  37.     for (int i = 0; i <= 2; i++)
  38.         for (int j = 0; j <= 2; j++)
  39.             if (j != i)
  40.                 if (friends[data[i]]->teammades.find(data[j]) == friends[data[i]]->teammades.end())
  41.                     friends[data[i]]->teammades.insert(pair <string, player*>(data[j], friends[data[j]]));
  42. }
  43.  
  44. map <string, int> answer;
  45.  
  46. queue <player*> Next;
  47.  
  48. void find(player* friends, int n)
  49. {
  50.     Next.push(friends);
  51.     answer.insert(pair <string, int>(friends->sname, n));
  52.     while (!Next.empty())
  53.     {
  54.         player* to = new player;
  55.         to = Next.front();
  56.         Next.pop();
  57.         n++;
  58.         for (auto i = to->teammades.begin(); i != to->teammades.end(); i++)
  59.         {
  60.             player* to1 = new player;
  61.             to1 = i->second;
  62.             if (!to1->was)
  63.             {
  64.                 to1->was = true;
  65.                 Next.push(to1);
  66.                 answer.insert(pair <string, int>(to1->sname, answer[to->sname] + 1));
  67.             }
  68.         }
  69.     }
  70. }
  71.  
  72. int main()
  73. {
  74.     player* team = new player;
  75.     vector <string> data;
  76.     string prom;
  77.     int n;
  78.  
  79.     cin >> n;
  80.  
  81.     for (int i = 1; i <= n; i++)
  82.     {
  83.         cin >> prom;
  84.         data.push_back(prom);
  85.         cin >> prom;
  86.         data.push_back(prom);
  87.         cin >> prom;
  88.         data.push_back(prom);
  89.         if (friends.find(data[0]) == friends.end())
  90.             friends.insert(pair <string, player*>(data[0], NEW(data[0])));
  91.         if (friends.find(data[1]) == friends.end())
  92.             friends.insert(pair <string, player*>(data[1], NEW(data[1])));
  93.         if (friends.find(data[2]) == friends.end())
  94.             friends.insert(pair <string, player*>(data[2], NEW(data[2])));
  95.         graf(data);
  96.         data.clear();
  97.     }
  98.  
  99.     auto j = friends.begin();
  100.  
  101.     if (friends.find("Isenbaev") != friends.end())
  102.     {
  103.         answer.insert(pair <string, int>("Isenbaev", 0));
  104.         find(friends["Isenbaev"], 0);
  105.     }
  106.  
  107.     bool promq = false;
  108.  
  109.     if(answer.size()!=0)
  110.         for (auto i = answer.begin(); i != answer.end(); i++)
  111.         {
  112.             if (promq == true)
  113.                 i--;
  114.             if (i->first == j->first)
  115.             {
  116.                 promq = false;
  117.                 cout << i->first << " ";
  118.                 cout << i->second << endl;
  119.             }
  120.             else
  121.             {
  122.                 promq = true;
  123.                 cout << j->first << " ";
  124.                 cout << "undefined" << endl;
  125.             }
  126.             j++;
  127.         }
  128.  
  129.     while (j != friends.end())
  130.     {
  131.         cout << j->first << " ";
  132.         cout << "undefined" << endl;
  133.         j++;
  134.     }
  135.  
  136.     _getch();
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment