Advertisement
Guest User

Untitled

a guest
May 25th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.94 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4.  
  5.  
  6. bool is_that_complete(int **matrix, int v)
  7. {
  8.     for (int i = 0;i<v; i++)
  9.     {
  10.         for (int j = 0; j < v; j++)
  11.         {
  12.             if (i == j) continue;
  13.             if (matrix[i][j] == 0) return false;
  14.         }
  15.     }
  16.  
  17.     return true;
  18. }
  19.  
  20. bool is_that_cycle(int **matrix, int v)
  21. {
  22.     int licznik = 0;
  23.  
  24.     int *odwiedzone = new int[v];
  25.     for (int i = 0; i < v; i++)
  26.     {
  27.         odwiedzone[i] = -1;
  28.  
  29.     }
  30.  
  31.     int s = 0;
  32.     for (int i = 0; i < v; i++)
  33.     {
  34.         for (int j = 0; j < v; j++)
  35.         {
  36.             if (matrix[s][j] == 1 && odwiedzone[j] == -1)
  37.             {
  38.                 odwiedzone[j] = 1;
  39.  
  40.                 matrix[s][j] = 0;
  41.                 matrix[j][s] = 0;
  42.                 licznik++;
  43.                 s = j;
  44.                 j = 0;
  45.  
  46.                 break;
  47.  
  48.             }
  49.         }
  50.     }
  51.  
  52.     bool tr = true;
  53.    
  54.     for (int i = 0; i <v; i++)
  55.     {
  56.         if (odwiedzone[i] == -1)
  57.         {
  58.             tr = false;
  59.             break;
  60.         }
  61.     }
  62.  
  63.     delete[] odwiedzone;
  64.     if (licznik == v && s == 0 && tr == true) return true;
  65.     else return false;
  66. }
  67.  
  68. int main()
  69. {
  70.     int n, v;
  71.     scanf("%d", &n);
  72.  
  73.     for (int k = 0; k < n; k++)
  74.     {
  75.         //start of allocation
  76.  
  77.         scanf("%d", &v);
  78.  
  79.         int **matrix = new int*[v];
  80.  
  81.         for (int i = 0; i < v; i++)
  82.         {
  83.             matrix[i] = new int[v];
  84.         }
  85.         int *colors = new int[v];
  86.  
  87.         for (int i = 0; i < v; i++)
  88.         {
  89.             colors[i] = 0;
  90.         }
  91.  
  92.         char *mat = new char[v*v + 1];
  93.  
  94.         scanf("%s", mat);
  95.  
  96.         for (int i = 0; i < v*v; i++)
  97.         {
  98.             if (mat[i] == '1') matrix[i / v][i%v] = 1;
  99.             else matrix[i / v][i%v] = 0;
  100.         }
  101.  
  102.         //end of allocation
  103.  
  104.         //calculations start
  105.  
  106.         //Is it complete graph?
  107.         if (is_that_complete(matrix, v))
  108.         {
  109.             printf("True\n");
  110.         }
  111.         //Is it an odd cycle?
  112.         else if (v % 2 == 1)
  113.         {
  114.             if (is_that_cycle(matrix, v))
  115.             {
  116.                 printf("True\n");
  117.             }
  118.             else printf("False\n");
  119.         }
  120.         else printf("False\n");
  121.        
  122.  
  123.         //calculations end
  124.  
  125.         //deallocation start
  126.  
  127.         for (int i = 0; i < v; i++)
  128.         {
  129.             delete[] matrix[i];
  130.         }
  131.  
  132.         delete[] matrix;
  133.         delete[] mat;
  134.  
  135.         //deallocation end
  136.     }
  137.  
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement