Advertisement
allia

компоненты

Dec 22nd, 2020 (edited)
1,038
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class Graph
  5. {
  6.   private:
  7.   bool orient;
  8.   int n, **arr, **vse_rebra, shet_reber, comp = 0, *dlina_comp;
  9.  
  10.   public:
  11.   Graph (int **matrix, int x, int shet)
  12.   {
  13.     n = x;
  14.     arr = matrix;
  15.     shet_reber = shet;
  16.   }
  17.   void deep(int x, int *R);
  18.   void deep_second(int *R, int x);
  19.   void get_component();
  20.   void poisk_comp();
  21.   int get_comp();
  22. };
  23.  
  24. void Graph::deep(int x, int *R)
  25. {
  26.   R[x] = comp;
  27.   for (int i = 0; i < n; i++)
  28.     if (arr[x][i] && !R[i])
  29.         deep(i, R);
  30. }
  31.  
  32. void Graph::get_component()
  33. {
  34.   int *R = new int[n];
  35.  
  36.   for (int i = 0; i < n; i++)
  37.      R[i] = 0;
  38.  
  39.   for (int i = 0; i < n; i++)
  40.     if (!R[i])
  41.     {
  42.       comp++;
  43.       deep(i, R);
  44.     }
  45.  
  46.   dlina_comp = new int[n];
  47.  
  48.   for(int i = 0; i < n; i++)
  49.    dlina_comp[i] = 0;
  50.  
  51.   for(int i = 0; i < n; i++)
  52.    dlina_comp[R[i]-1]++;
  53.  
  54.   cout << comp << endl;
  55. }
  56.  
  57. void Graph::deep_second(int *R, int x)
  58. {
  59.   R[x] = x+1;
  60.   cout << R[x] << " ";
  61.   for (int i = 0; i < n; i++)
  62.     if (arr[x][i] != 0 && R[i] == 0)
  63.       deep_second (R, i);
  64. }
  65.  
  66. void Graph::poisk_comp()
  67. {
  68.   int i, comp = 0, *R = new int[n], k = 0;
  69.  
  70.   for (i = 0; i < n; i++)
  71.    R[i] = 0;
  72.  
  73.  for (int i = 0; i < n; i++)
  74.     if (!R[i])
  75.       {
  76.         cout << dlina_comp[k] << " ";
  77.         deep_second(R, i);
  78.         k++;
  79.         cout << endl;
  80.       }
  81. }
  82.  
  83. int main()
  84. {
  85.  int n, shet_reber = 0, x, shisl_comp = 1, shet = 0;
  86.  cin >> n;
  87.  
  88. int **arr = new int*[n];
  89.  
  90. for (int i =0; i < n; i++)
  91.  arr[i] = new int[n];
  92.  
  93. for (int i =0; i < n; i++)
  94.   for (int j =0; j < n; j++)  
  95.      {
  96.        cin >> arr[i][j];
  97.         if (arr[i][j] == 1)
  98.          shet_reber++;
  99.      }
  100.  
  101.  shet_reber /= 2;
  102.  Graph object(arr, n, shet_reber);
  103.  object.get_component();
  104.  object.poisk_comp();
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement