Advertisement
a53

CicluL

a53
Mar 18th, 2022
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cassert>
  4.  
  5. using namespace std;
  6.  
  7. ifstream fin("ciclul.in");
  8. ofstream fout("ciclul.out");
  9.  
  10. int n , A[21][21], L , x[21], uz[21], gasit = false;
  11.  
  12. bool OK(int k)
  13. {
  14. if(k == 1)
  15. return true;
  16. if(A[x[k-1]][x[k]] == 0)
  17. return false;
  18. return true;
  19. }
  20.  
  21. void Back(int k)
  22. {
  23. for(int i = 1; i <= n && ! gasit; i ++)
  24. if(uz[i] == 0)
  25. {
  26. uz[i] = 1;
  27. x[k] = i;
  28. if(OK(k))
  29. {
  30. if(k == L)
  31. {
  32. if(A[x[k]][x[1]] == 1)
  33. {
  34. for(int i = 1; i <= k ; i ++)
  35. fout << x[i] << " ";
  36. fout << x[1] << '\n';
  37. gasit = true;
  38. }
  39. }
  40. else
  41. Back(k + 1);
  42. }
  43. uz[i] = 0;
  44. }
  45. }
  46.  
  47. int main()
  48. {
  49. int i , j, m;
  50. fin >> n >> m;
  51. while(m > 0)
  52. {
  53. fin >> i >> j;
  54. if(A[i][j] == 1)
  55. cout << i << " " << j << endl;;
  56. A[i][j] = A[j][i] = 1;
  57. m --;
  58. }
  59. fin >> L;
  60. Back(1);
  61. return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement