Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. //graf neorientat cu n noduri si m muchii.
  4. using namespace std;
  5. int a[30][30], n, m, viz[30], L[30], C[30], k, x, y;
  6. void citire()
  7. {
  8. ifstream f("graf.in");
  9. int b, c;
  10. f>>n>>m;
  11. while(f>>b>>c)
  12. {
  13. a[c][b]=a[b][c]=1;
  14. }
  15. f.close();
  16. }
  17. void lant_x_y(int z, int pas)
  18. {
  19. int i, j;
  20. if(z==y)
  21. {
  22. for(j=1; j<pas; j++)
  23. {
  24. cout<<L[j]<<" ";
  25. }
  26. cout<<endl;
  27. }
  28. else
  29. {
  30. for(i=1; i<=n; i++)
  31. {
  32. if(a[z][i]==1 && !viz[i])
  33. {
  34. L[pas]=i;
  35. viz[i]=1;
  36. lant_x_y(i,pas+1);
  37. viz[i]=0;
  38. }
  39. }
  40. }
  41. }
  42. void lant_k(int z, int pas)
  43. {
  44. int i, j;
  45. if(pas==k+2)
  46. {
  47. for(j=1; j<pas; j++)
  48. {
  49. cout<<L[j]<<" ";
  50. }
  51. cout<<endl;
  52. }
  53. else
  54. {
  55. for(i=1; i<=n; i++)
  56. {
  57. if(a[z][i]==1 && !viz[i])
  58. {
  59. L[pas]=i;
  60. viz[i]=1;
  61. lant_k(i,pas+1);
  62. viz[i]=0;
  63. }
  64. }
  65. }
  66. }
  67. void ciclu_x(int z, int pas)
  68. {
  69. int i, j;
  70. if(a[z][C[1]] && pas>3)
  71. {
  72. for(j=1; j<pas; j++)
  73. {
  74. cout<<C[j]<<" ";
  75. }
  76. cout<<C[1];
  77. cout<<endl;
  78. }
  79. else
  80. {
  81. for(i=1; i<=n; i++)
  82. {
  83. if(a[z][i]==1 && !viz[i])
  84. {
  85. C[pas]=i;
  86. viz[i]=1;
  87. ciclu_x(i,pas+1);
  88. viz[i]=0;
  89. }
  90. }
  91. }
  92. }
  93. void ciclu_k(int z, int pas)
  94. {
  95. int i, j;
  96. if(a[z][C[1]] && pas==k+1)
  97. {
  98. for(j=1; j<pas; j++)
  99. {
  100. cout<<C[j]<<" ";
  101. }
  102. cout<<C[1];
  103. cout<<endl;
  104. }
  105. else
  106. {
  107. for(i=1; i<=n; i++)
  108. {
  109. if(a[z][i]==1 && !viz[i])
  110. {
  111. C[pas]=i;
  112. viz[i]=1;
  113. ciclu_k(i,pas+1);
  114. viz[i]=0;
  115. }
  116. }
  117. }
  118. }
  119. void ciclu_ham(int z, int pas)
  120. {
  121. int i, j;
  122. if(a[z][C[1]] && pas==n+1)
  123. {
  124. for(j=1; j<pas; j++)
  125. {
  126. cout<<C[j]<<" ";
  127. }
  128. cout<<C[1];
  129. cout<<endl;
  130. }
  131. else
  132. {
  133. for(i=1; i<=n; i++)
  134. {
  135. if(a[z][i]==1 && !viz[i])
  136. {
  137. C[pas]=i;
  138. viz[i]=1;
  139. ciclu_ham(i,pas+1);
  140. viz[i]=0;
  141. }
  142. }
  143. }
  144. }
  145. int main()
  146. {
  147. citire();
  148. /*cin>>x>>y;
  149. viz[x]=1;
  150. L[1]=x;
  151. lant_x_y(x,2);
  152. viz[x]=0;
  153. cout<<"Introduceti lungimea lantului ";
  154. cin>>k;
  155. for(int i=1; i<=n; i++)
  156. {
  157. viz[i]=1;
  158. L[1]=i;
  159. lant_k(i, 2);
  160. viz[i]=0;
  161. }
  162. cout<<"Introduceti primul nod ";
  163. cin>>x;
  164. viz[x]=1;
  165. L[1]=x;
  166. ciclu_x(x, 2);
  167. viz[x]=0;
  168. cin>>k;
  169. for(int i=1; i<=n; i++)
  170. {
  171. viz[i]=1;
  172. C[1]=i;
  173. ciclu_k(i,2);
  174. viz[i]=0;
  175. }*/
  176. cin>>x;
  177. viz[x]=1;
  178. C[1]=x;
  179. ciclu_ham(x,2);
  180. viz[1]=0;
  181. return 0;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement