Advertisement
HasanRasulov

Baby_Heels.cpp

Mar 10th, 2021
764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.38 KB | None | 0 0
  1. /*
  2.  
  3. There are two types of professional wrestlers: “babyfaces”
  4. (“good guys”) and “heels”(“bad guys”). Between any pair of
  5. professional wrestlers, there may or may not be a
  6. rivalry.Suppose we have n professional wrestlers and we have a
  7. list of r pairs of wrestlers for whichthere are rivalries. Give an
  8. algorithm that determines whether it is possible to
  9. designatesome of the wrestlers as babyfaces and the
  10. remainder as heels such that each rivalry isbetween a babyface
  11. and a heel. If it is possible to perform such a designation,
  12. youralgorithm should produce it.
  13.  
  14. */
  15.  
  16. #include <iostream>
  17. #include <utility>
  18. #include <list>
  19. #include <algorithm>
  20. #include <iterator>
  21.  
  22.  
  23. class Competition
  24. {
  25.     enum class Teams
  26.     {
  27.         babyface,
  28.         heel,
  29.         nope
  30.     };
  31.     class Wrestler
  32.     {
  33.         Teams team;
  34.         size_t _id;
  35.  
  36.       public:
  37.         Wrestler(size_t _id) : _id(_id)
  38.         {
  39.             team = Teams::nope;
  40.         }
  41.         void set_team(Teams team)
  42.         {
  43.             this->team = team;
  44.         }
  45.         Teams get_team() const
  46.         {
  47.             return this->team;
  48.         }
  49.         size_t get_id() const
  50.         {
  51.             return this->_id;
  52.         }
  53.     };
  54.     std::list<Wrestler *> *graph;
  55.     const size_t N;
  56.     bool isPossible(int, Teams);
  57.     bool isCompetitionPossibleUtil(size_t);
  58.     bool add_all() { return true; }
  59.     void print_teams();
  60.  
  61.   public:
  62.     Competition(size_t);
  63.     bool add(std::pair<size_t, size_t>);
  64.     template <typename T, typename... Types>
  65.     bool add_all(T t, Types... pack);
  66.     bool isCompetitionPossible();
  67.     Competition *show_competition_schedule();
  68.     ~Competition();
  69. };
  70. Competition::Competition(size_t N) : N(N)
  71. {
  72.     graph = new std::list<Wrestler *>[N];
  73.  
  74.     for (size_t i = 0; i < N; i++)
  75.     {
  76.         graph[i].push_back(new Wrestler(i));
  77.     }
  78. }
  79.  
  80. Competition::~Competition()
  81. {
  82.     for (size_t i = 0; i < N; i++)
  83.     {
  84.         delete graph[i].front();
  85.     }
  86.     delete[] graph;
  87. }
  88.  
  89. bool Competition::add(std::pair<size_t, size_t> consent)
  90. {
  91.     if (consent.first >= N || consent.second >= N)
  92.         return false;
  93.  
  94.     graph[consent.first].push_back(graph[consent.second].front());
  95.     graph[consent.second].push_back(graph[consent.first].front());
  96.  
  97.     return true;
  98. }
  99.  
  100. template <typename T, typename... Types>
  101. bool Competition::add_all(T t, Types... pack)
  102. {
  103.     if (!add(t))
  104.         return false;
  105.     return add_all(pack...);
  106. }
  107.  
  108. void Competition::print_teams()
  109. {
  110.     std::cout << "\nBaby-Faces:";
  111.     for (size_t i = 0; i < N; i++)
  112.     {
  113.         const auto it = graph[i].front();
  114.         if (it->get_team() == Teams::babyface)
  115.             std::cout << "[" << it->get_id() << "],";
  116.     }
  117.     std::cout << "\nHeels:";
  118.     for (size_t i = 0; i < N; i++)
  119.     {
  120.         const auto it = graph[i].front();
  121.         if (it->get_team() == Teams::heel)
  122.             std::cout << "[" << it->get_id() << "],";
  123.     }
  124.     std::cout << std::endl;
  125. }
  126.  
  127. bool Competition::isPossible(int _id, Teams pre)
  128. {
  129.     auto it = graph[_id].begin();
  130.     std::advance(it, 1);
  131.     return !std::any_of(it, graph[_id].end(), [&pre](Wrestler *w) {
  132.         return w->get_team() == pre;
  133.     });
  134. }
  135.  
  136. bool Competition::isCompetitionPossibleUtil(size_t vertex)
  137. {
  138.     if (vertex == N)
  139.         return true;
  140.     if (isPossible(vertex, Teams::heel))
  141.     {
  142.         graph[vertex].front()->set_team(Teams::heel);
  143.         if (isCompetitionPossibleUtil(vertex + 1))
  144.             return true;
  145.         graph[vertex].front()->set_team(Teams::nope);
  146.     }
  147.     if (isPossible(vertex, Teams::babyface))
  148.     {
  149.         graph[vertex].front()->set_team(Teams::babyface);
  150.         if (isCompetitionPossibleUtil(vertex + 1))
  151.             return true;
  152.         graph[vertex].front()->set_team(Teams::nope);
  153.     }
  154.     return false;
  155. }
  156.  
  157. bool Competition::isCompetitionPossible()
  158. {
  159.  
  160.     if (isCompetitionPossibleUtil(0))
  161.     {
  162.         print_teams();
  163.         return true;
  164.     }
  165.  
  166.     std::clog << "It is not possible!\n";
  167.     return false;
  168. }
  169.  
  170. Competition *Competition::show_competition_schedule()
  171. {
  172.     for (size_t i = 0; i < N; i++)
  173.     {
  174.         for (const auto j : graph[i])
  175.         {
  176.             std::cout << j->get_id() << " ->";
  177.         }
  178.         std::cout << "\n";
  179.     }
  180.     return this;
  181. }
  182.  
  183. int main()
  184. {
  185.     const size_t N = 7;
  186.     Competition *comp = new Competition(N);
  187.     /*
  188.        comp->add(std::make_pair(0,6));
  189.        comp->add(std::make_pair(6,2));
  190.        comp->add(std::make_pair(5,6));
  191.        comp->add(std::make_pair(2,3));
  192.        comp->add(std::make_pair(1,3));
  193.        comp->add(std::make_pair(1,4));
  194.      */
  195.  
  196.     comp->add_all(std::make_pair(0, 6),
  197.                   std::make_pair(6, 2),
  198.                   std::make_pair(5, 6),
  199.                   std::make_pair(2, 3),
  200.                   std::make_pair(1, 3),
  201.                   std::make_pair(1, 4));
  202.  
  203.     comp->show_competition_schedule()->isCompetitionPossible();
  204.  
  205.     delete comp;
  206.     return 0;
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement