fork download
  1. #include <cstring>
  2. #include <algorithm>
  3. #include <iostream>
  4.  
  5.  
  6. void DFS(int (&used)[20], const int (&m)[20][20], int c, int& maxn, int k, int v) {
  7. used[v] = 1;
  8. k += 1;
  9. if(k > maxn)
  10. maxn = k;
  11. for(int i = 0; i < c; ++i) {
  12. if(!used[i] && m[v][i] == 1) {
  13. DFS(used, m, c, maxn, k, i);
  14. }
  15. }
  16. used[v] = 0;
  17. }
  18.  
  19. int main() {
  20. const long a[20] = {1, 1, 3, 7, 13};
  21. const int c = 5;
  22.  
  23. int m[20][20];
  24. memset(m, 0, sizeof(m));
  25.  
  26. for(int i = 0; i < c; ++i) {
  27. for(int j = i + 1; j < c; ++j) {
  28. if( (a[i] % a[j] == 0) || (a[j] % a[i] == 0) ) {
  29. m[i][j] = m[j][i] = 1; // Creating 2D array
  30. }
  31. }
  32. }
  33.  
  34. int maxn = 0;
  35. for(int i = 0; i < c; ++i) {
  36. int used[20];
  37. int k = 0;
  38. memset(used, 0, sizeof(used));
  39. DFS(used, m, c, maxn, k, i);
  40. }
  41. std::cout << maxn << std::endl;
  42. }
  43.  
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
5