Advertisement
PlanttPastes

ИИ - Алгоритми 2021 задачи за решавање: Боксерски турнир

Oct 19th, 2021
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. enum boxer_colour // the colour
  4. {
  5.     UNDEFINED, // non-visited
  6.     RED,
  7.     BLUE
  8. };
  9. struct boxer // a boxer point
  10. {
  11.     boxer_colour colour;
  12.     vector <int> boxing_with;
  13.     boxer()
  14.     {
  15.         colour = UNDEFINED;
  16.         return;
  17.     }
  18. };
  19. boxer_colour opposite(boxer_colour c)
  20. {
  21.     switch (c)
  22.     {
  23.     case RED:
  24.         return BLUE;
  25.     case BLUE:
  26.         return RED;
  27.     case UNDEFINED:
  28.         return UNDEFINED;
  29.     default:
  30.         return UNDEFINED;
  31.     }
  32. }
  33. int main()
  34. {
  35.     int boxers_count, edges; // size of boxers array
  36.     cin >> boxers_count >> edges;
  37.     vector <boxer> boxers(boxers_count); // boxers
  38.     for (int i = 0; i < edges; i++) // input edges
  39.     {
  40.         int a, b;
  41.         cin >> a >> b;
  42.         boxers[a].boxing_with.push_back(b);
  43.         boxers[b].boxing_with.push_back(a);
  44.     }
  45.     queue <int> bfs_queue; // BFS queue
  46.     bfs_queue.push(0); // add boxer 0 to BFS queue
  47.     boxers[0].colour = RED;
  48.     while (!bfs_queue.empty()) // BFS
  49.     {
  50.         int boxer_index = bfs_queue.front(); // get check index for boxer
  51.         bfs_queue.pop();
  52.         for (int i = 0; i < boxers[boxer_index].boxing_with.size(); i++) // check all edges connecting to boxer
  53.         {
  54.             if (boxers[boxers[boxer_index].boxing_with[i]].colour == boxers[boxer_index].colour)
  55.             {
  56.                 cout << "NE"; // two neighbour boxers have same pants colour
  57.                 return 0;
  58.             }
  59.             if (boxers[boxers[boxer_index].boxing_with[i]].colour == UNDEFINED)
  60.             {
  61.                 boxers[boxers[boxer_index].boxing_with[i]].colour = opposite(boxers[boxer_index].colour);
  62.                 bfs_queue.push(boxers[boxer_index].boxing_with[i]); // colour with opposite colour
  63.             }
  64.         }
  65.     }
  66.     cout << "DA"; // all pants are given
  67.     return 0;
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement