Advertisement
Guest User

Untitled

a guest
May 21st, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. const int N=13;
  6.  
  7. struct node
  8. {
  9. int val;
  10. node*next;
  11. };
  12.  
  13. struct v
  14. {
  15. string color="white";
  16. int td;
  17. int tf;
  18. int par=-1;
  19. //bool visited=false;
  20. };
  21.  
  22. void DFS_visit(bool A[][N], v ver[], int u, int &time, node*&head)
  23. {
  24. time++;
  25. ver[u].td=time;
  26. ver[u].color="grey";
  27. for(int i=0; i<N; i++)
  28. {
  29. if(A[u][i] && ver[i].color=="white")
  30. {
  31. ver[i].par=u;
  32. DFS_visit(A, ver, i, time, head);
  33. }
  34. }
  35. ver[u].color="black";
  36. node*p=new node;
  37. p->val=u;
  38. p->next=head;
  39. head=p;
  40. time++;
  41. ver[u].tf=time;
  42. }
  43.  
  44. void DFS(bool A[][N], v ver[], node*&head)
  45. {
  46. int time=0;
  47. for(int i=0; i<N; i++)
  48. {
  49. if(ver[i].color=="white")
  50. DFS_visit(A, ver, i, time, head);
  51. }
  52. }
  53.  
  54. bool if_hamiltonian(bool A[][N], node*head)
  55. {
  56. while(head->next!=NULL)
  57. {
  58. if(!A[head->val][head->next->val]) return false;
  59. head=head->next;
  60. }
  61. return true;
  62. }
  63.  
  64. int main()
  65. {
  66. v ver[N];
  67. bool A[N][N];
  68. for(int i=0; i<N; i++)
  69. for(int j=0; j<N; j++)
  70. {
  71. A[i][j]=false;
  72. }
  73. A[0][1]=true;
  74. A[2][0]=true;
  75. A[3][2]=true;
  76. A[4][5]=true;
  77. A[5][3]=true;
  78. A[6][9]=true;
  79. A[7][6]=true;
  80. A[8][7]=true;
  81. A[9][10]=true;
  82. A[10][12]=true;
  83. A[11][4]=true;
  84. A[12][11]=true;
  85. node*head=NULL;
  86. DFS(A, ver, head);
  87. if(if_hamiltonian(A, head)) cout << "Tak" << endl;
  88. else cout << "Nie" << endl;
  89. while(head!=NULL)
  90. {
  91. cout << head->val << " ";
  92. head=head->next;
  93. }
  94. cout << endl;
  95. return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement