Advertisement
PopaLepo

Verificare graf eulerian

Jan 13th, 2017
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. ifstream f("graf.txt");
  5. int n,a[100][100],x[100],m=0;
  6.  
  7. void citire(int &n,int a[100][100])
  8. {
  9. f>>n;
  10. for(int i=1;i<=n;i++)
  11. for(int j=1;j<=n;j++)
  12. {f>>a[i][j];
  13. if(a[i][j]==1)
  14. m++;}
  15. m/=2;
  16. }
  17.  
  18. int valid(int k)
  19. {
  20. if(k>1)
  21. if(a[x[k-1]][x[k]]!=1)
  22. return 0;
  23.  
  24. for(int i=1;i<=k-2;i++)
  25. if((x[i]==x[k-1]&&x[i+1]==x[k])||(x[i+1]==x[k-1]&&x[i]==x[k]))
  26. return 0;
  27. return 1 ;}
  28.  
  29. int sol(int k)
  30. {
  31.  
  32. if(k!=m+1)
  33. return 0;
  34.  
  35. if(x[1]!=x[k])
  36. {cout<<x[1]<<" ";
  37. return 0;
  38. }
  39. return 1;
  40. }
  41.  
  42. void tipar(int k)
  43. {
  44. for(int i=1;i<=k;i++)
  45. cout<<x[i]<<" ";
  46. cout<<endl;
  47. }
  48.  
  49. void BKT()
  50. {
  51. int k=1;
  52. x[k]=0;
  53. while(k)
  54. if(x[k]<n)
  55. {
  56. x[k]=x[k]+1;
  57. if(valid(k))
  58. if(sol(k))
  59. {tipar(k);
  60. k=0;}
  61. else
  62. {
  63. k++;
  64. x[k]=0;}}
  65. else
  66. k--;
  67. }
  68.  
  69.  
  70. int main()
  71. {
  72. citire(n,a);
  73. BKT();
  74. f.close();
  75. return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement