Advertisement
allia

эйлеров цикл

Jan 9th, 2021 (edited)
1,029
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. // решала совместно с Максимом Ткачевым
  2. #include <iostream>
  3. using namespace std;
  4. int m = 0;
  5.  
  6. struct Graph
  7. {
  8.     int n;
  9.     bool** arr;
  10.  
  11.     Graph(int x)
  12.     {
  13.         n = x;
  14.         arr = new bool* [n] {0};
  15.         for (int i = 0; i < n; i++)
  16.             arr[i] = new bool[n] {0};
  17.     }
  18.  
  19.     void new_rebro(int a, int b)
  20.     {
  21.         arr[a][b] = 1;
  22.     }
  23.  
  24.     void delete_rebro(int a, int b)
  25.     {
  26.         arr[a][b] = 0;
  27.         arr[b][a] = 0;
  28.     }
  29.    
  30.     void DFS(int start, bool* visited)
  31.     {
  32.         m++;
  33.         visited[start] = true;
  34.         for (int i = 0; i < n; i++)
  35.             if (arr[start][i] == true and visited[i] == false)
  36.                 DFS(i, visited);
  37.     }
  38.  
  39.     int izolation()
  40.     {
  41.         int num = 0;
  42.         bool temp = false;
  43.         for (int i = 0; i < n; i++)
  44.         {
  45.             temp = false;
  46.             for (int j = 0; j < n; j++)
  47.                 if (arr[i][j] == true)
  48.                 {
  49.                     temp = true;
  50.                     break;
  51.                 }
  52.             if (temp == false)
  53.                 num++;
  54.         }
  55.         return num;
  56.     }
  57.  
  58.     void poisk_cycle(int current = 0)
  59.     {
  60.         int invite = 0, out = 0, chislo_reber = 0;
  61.  
  62.         for (int i = 0; i < n; i++)
  63.         {
  64.             for (int j = 0; j < n; j++)
  65.             {
  66.                 if (arr[i][j] == true)
  67.                     out++;
  68.                 if (arr[j][i] == true)
  69.                     invite++;
  70.             }
  71.             if (out != invite)
  72.             {
  73.                 cout << "-1";
  74.                 return;
  75.             }
  76.             else
  77.             {
  78.                 chislo_reber += out;
  79.                 out = 0;
  80.                 invite = 0;
  81.             }
  82.         }
  83.         int to_delete = 0;
  84.  
  85.         cout << current + 1 << " ";
  86.  
  87.         while (to_delete < chislo_reber)
  88.             for (int i = n-1; i >= 0; i--)
  89.                 if (arr[current][i] == true)
  90.                 {
  91.                     delete_rebro(current, i);
  92.  
  93.                     bool* visited = new bool[n]{0};
  94.                     m = 0;
  95.                     DFS(i, visited);
  96.                     delete[] visited;
  97.          
  98.           int cant_access = izolation();
  99.                       if (m + cant_access == n || cant_access == n)
  100.                       {
  101.                           to_delete += 2;
  102.                           cout << i + 1 << " ";
  103.                           current = i;
  104.                           break;
  105.                       }
  106.                       else
  107.                       {
  108.                           new_rebro(i, current);
  109.                           new_rebro(current, i);
  110.                       }
  111.                 }
  112.     }
  113. };
  114.  
  115. int main()
  116. {
  117.     int n, elem;
  118.     cin >> n;
  119.     Graph object(n);
  120.  
  121.     for (int i = 0; i < n; i++)
  122.         for (int j = 0; j < n; j++)
  123.         {
  124.             cin >> elem;
  125.             if (elem == 1)
  126.                 object.new_rebro(i, j);
  127.         }
  128.  
  129.     object.poisk_cycle(1);
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement