allia

краски

Jan 16th, 2021 (edited)
768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Graph
  5. {
  6.     int n, colors, *paint_current, to_check;
  7.     bool  **arr;
  8.  
  9. public:
  10.     Graph(int x, bool **matrix, int a)
  11.     {
  12.         colors = 1;
  13.         to_check = a;
  14.         n = x;
  15.         arr = matrix;
  16.         paint_current = new int[n]{0};
  17.   }
  18.   void check_color();
  19.   void painting();
  20.   bool check();
  21. };
  22.  
  23. bool Graph::check()
  24. {
  25.   bool x = true;
  26.  
  27.   for (int i = 0; i < n; i++)
  28.    if (paint_current[i] == 0)
  29.     x = false;
  30.  
  31.   return x;
  32. }
  33.  
  34. void Graph::painting()
  35. {
  36.  int current_v = 0, current_plus = 0;
  37.  
  38. while(!check())
  39.  {
  40.    for (int i = 0; i < n; i++)
  41.      if (paint_current[i] == 0)
  42.     {
  43.       current_v = i;
  44.       paint_current[current_v] = colors;
  45.       colors++;
  46.       break;
  47.     }
  48.  
  49.   int j = 0;
  50.  
  51.     while (j < n)
  52.     {
  53.       if (arr[current_v][j] == 0 && paint_current[j] == 0)
  54.       {
  55.         current_plus = j;
  56.         for (int i = 0; i < n; i++)
  57.            arr[current_v][i] += arr[current_plus][i];
  58.         paint_current[current_plus] = paint_current[current_v];
  59.       }
  60.       j++;
  61.     }
  62.  }
  63. colors--;
  64. check_color();
  65. }
  66.  
  67. void Graph::check_color()
  68. {
  69.   if (colors <= to_check)
  70.    cout << "YES";
  71.   else cout << "NO";
  72. }
  73.  
  74. int main()
  75. {
  76.   int n, colors;
  77.   cin >> n >> colors;
  78.  
  79.   bool **arr= new bool*[n];
  80.  
  81.   for (int i= 0; i< n; i++)
  82.    arr[i] = new bool[n];
  83.  
  84.   for (int i = 0; i < n; i++)
  85.    for (int j = 0; j < n; j++)
  86.     {
  87.       cin >> arr[i][j];
  88.       if ( i== j)
  89.        arr[i][j] = 1;
  90.     }
  91.  
  92.   Graph object(n, arr, colors);
  93.   object.painting();
  94. }
Add Comment
Please, Sign In to add comment