Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int N=13;
- struct node
- {
- int val;
- node*next;
- };
- struct v
- {
- string color="white";
- int td;
- int tf;
- int par=-1;
- //bool visited=false;
- };
- void DFS_visit(bool A[][N], v ver[], int u, int &time, node*&head)
- {
- time++;
- ver[u].td=time;
- ver[u].color="grey";
- for(int i=0; i<N; i++)
- {
- if(A[u][i] && ver[i].color=="white")
- {
- ver[i].par=u;
- DFS_visit(A, ver, i, time, head);
- }
- }
- ver[u].color="black";
- node*p=new node;
- p->val=u;
- p->next=head;
- head=p;
- time++;
- ver[u].tf=time;
- }
- void DFS(bool A[][N], v ver[], node*&head)
- {
- int time=0;
- for(int i=0; i<N; i++)
- {
- if(ver[i].color=="white")
- DFS_visit(A, ver, i, time, head);
- }
- }
- bool if_hamiltonian(bool A[][N], node*head)
- {
- while(head->next!=NULL)
- {
- if(!A[head->val][head->next->val]) return false;
- head=head->next;
- }
- return true;
- }
- int main()
- {
- v ver[N];
- bool A[N][N];
- for(int i=0; i<N; i++)
- for(int j=0; j<N; j++)
- {
- A[i][j]=false;
- }
- A[0][1]=true;
- A[2][0]=true;
- A[3][2]=true;
- A[4][5]=true;
- A[5][3]=true;
- A[6][9]=true;
- A[7][6]=true;
- A[8][7]=true;
- A[9][10]=true;
- A[10][12]=true;
- A[11][4]=true;
- A[12][11]=true;
- node*head=NULL;
- DFS(A, ver, head);
- if(if_hamiltonian(A, head)) cout << "Tak" << endl;
- else cout << "Nie" << endl;
- while(head!=NULL)
- {
- cout << head->val << " ";
- head=head->next;
- }
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement