#include<iostream>
#define N 100
using namespace std;
struct Node{
int data;
Node *link;
};
struct stack{
Node* stackarray[N];
int sp;
};
stack s;
struct Queue{
Node* queuearray[N];
int rear;
int front;
};
Queue q;
Node Adjlist[9];
bool visited[9];
void construct(Node*,int);
void DFSh(int);
int push(stack *,Node*&);
void pop(stack *,Node*&);
int isempty(stack *);
int isfull(stack *);
void visit(Node*);
Node* findAdj(Node *);
void DFS(Node*);
int main(void){
for(int i=1;i<=8;i++){
Adjlist[i].link=NULL;
Adjlist[i].data=i;}
Node *head1=&Adjlist[1];
Node *head2=&Adjlist[2];
Node *head3=&Adjlist[3];
Node *head4=&Adjlist[4];
Node *head5=&Adjlist[5];
Node *head6=&Adjlist[6];
Node *head7=&Adjlist[7];
Node *head8=&Adjlist[8];
construct(head1,2);
construct(head1,3);
construct(head2,4);
construct(head2,5);
construct(head3,6);
construct(head3,7);
construct(head4,2);
construct(head4,8);
construct(head5,2);
construct(head5,8);
construct(head6,3);
construct(head6,8);
construct(head7,3);
construct(head7,8);
construct(head8,4);
construct(head8,5);
construct(head8,6);
construct(head8,7);
s.sp=-1;
for(int i=1;i<=8;i++)
visited[i]=false;
DFS(head1);
return 0;
}
void construct(Node* ptr,int x){
Node *ppp=new Node;
ppp->data=x;
ppp->link=NULL;
while(ptr->link!=NULL){
ptr=ptr->link;
}
ptr->link=ppp;
}
void DFSh(int v){
visited[v]=true;
cout<<"Visit Vertex:"<<v<<" "<<endl;
Node *w;
for(w=&Adjlist[v];w;w=w->link)
if(!visited[w->data])
DFSh(w->data);
}
void DFS(Node *ptr){
Node *Vx,*Vy;
push(&s,ptr);
while(!(isempty(&s))){
pop(&s,Vx);
if(visited[Vx->data]==false){
visit(Vx);
Vy=findAdj(Vx);
while(Vy!=NULL){
push(&s,Vy);
Vy=findAdj(Vx);
}
}
}
}
void visit(Node *x){
visited[x->data]=true;
cout<<"Visit Vertex:"<<x->data<<" "<<endl;
}
Node* findAdj(Node *ptr){
Node *pk=NULL;
ptr=ptr->link;
while(ptr!=NULL){
if(visited[ptr->data]==false)
return ptr;
ptr=ptr->link;
}
return pk;
}
int push(stack *p,Node* &x){
if(isfull(p))
return 1;
p->sp++;
p->stackarray[p->sp]=x;
}
void pop(stack *p,Node *&x){
x=p->stackarray[p->sp--];
}
int isempty(stack *p){
if(p->sp==-1)
return 1;
else
return 0;
}
int isfull(stack *p){
if(p->sp==N-1)
return 1;
else
return 0;
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNkZWZpbmUgTiAxMDAKCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgoKc3RydWN0IE5vZGV7CiAgICAgICAgCiAgICAgaW50IGRhdGE7ICAgCiAgICAgTm9kZSAqbGluazsKICAgICAgCiAgICAgCiAgICAgICAKICAgICAgICB9OwogICAgICAgIApzdHJ1Y3Qgc3RhY2t7CiAgICAgICBOb2RlKiBzdGFja2FycmF5W05dOwogICAgICAgaW50IHNwOwogICAgICAgCiAgICAgICAKICAgICAgIH07CgpzdGFjayBzOyAgICAgICAgCgoKc3RydWN0IFF1ZXVlewogICAgICAgIE5vZGUqIHF1ZXVlYXJyYXlbTl07CiAgICAgICAgaW50IHJlYXI7CiAgICAgICAgaW50IGZyb250OyAgICAgICAgICAgICAgCiAgICAgICAgfTsKICAgICAgICAKUXVldWUgcTsKTm9kZSBBZGpsaXN0WzldOwpib29sIHZpc2l0ZWRbOV07CgoKCnZvaWQgY29uc3RydWN0KE5vZGUqLGludCk7CnZvaWQgREZTaChpbnQpOwppbnQgcHVzaChzdGFjayAqLE5vZGUqJik7CnZvaWQgcG9wKHN0YWNrICosTm9kZSomKTsKaW50IGlzZW1wdHkoc3RhY2sgKik7CmludCBpc2Z1bGwoc3RhY2sgKik7CnZvaWQgdmlzaXQoTm9kZSopOwpOb2RlKiBmaW5kQWRqKE5vZGUgKik7CnZvaWQgREZTKE5vZGUqKTsKCgppbnQgbWFpbih2b2lkKXsKICAgIGZvcihpbnQgaT0xO2k8PTg7aSsrKXsKICAgICAgQWRqbGl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ICAgIAogICAgREZTKGhlYWQxKTsKICAgIAogICAgICAgcmV0dXJuIDA7Cn0KCnZvaWQgY29uc3RydWN0KE5vZGUqIHB0cixpbnQgeCl7CiAgICAgIE5vZGUgKnBwcD1uZXcgTm9kZTsKICAgICAgcHBwLT5kYXRhPXg7CiAgICAgIHBwcC0+bGluaz1OVUxMOwogICAgICAKICAgICAgd2hpbGUocHRyLT5saW5rIT1OVUxMKXsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICBwdHI9cHRyLT5saW5rOwogICAgICAgCiAgICAgfQogICAgIHB0ci0+bGluaz1wcHA7CiAgICAgCiAgICAgfQoKICAgICAKICAgICAKdm9pZCBERlNoKGludCB2KXsKICAgICAgdmlzaXRlZFt2XT10cnVlOwogICAgIAogICAgICBjb3V0PDwiVmlzaXQgVmVydGV4OiI8PHY8PCIgIjw8ZW5kbDsKICAgICAgTm9kZSAqdzsKICAgICAgZm9yKHc9JkFkamxpc3Rbdl07dzt3PXctPmxpbmspCiAgICAgICAgaWYoIXZpc2l0ZWRbdy0+ZGF0YV0pCiAgICAgICAgICAgREZTaCh3LT5kYXRhKTsKICAgICB9CgoKdm9pZCBERlMoTm9kZSAqcHRyKXsKICAgICAgCiAgICAgIE5vZGUgKlZ4LCpWeTsKICAgICAgCiAgICAgIHB1c2goJnMscHRyKTsKICAgICAgd2hpbGUoIShpc2VtcHR5KCZzKSkpewogICAgICAgICAgIAogICAgICAgICAgIHBvcCgmcyxWeCk7ICAgICAgICAgICAgICAKICAgICAgICBpZih2aXNpdGVkW1Z4LT5kYXRhXT09ZmFsc2UpewogICAgICAgICAgICAgdmlzaXQoVngpOwogICAgICAgICAgICAgVnk9ZmluZEFkaihWeCk7ICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICB3aGlsZShWeSE9TlVMTCl7CiAgICAgICAgICAgICAgICAgICAgcHVzaCgmcyxWeSk7ICAgICAgCiAgICAgICAgICAgICAgICAgICAgVnk9ZmluZEFkaihWeCk7ICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9ICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfSAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAKICAgICAKICAgICAKICAgICAKICAgICAKICAgICAKCgoKCnZvaWQgdmlzaXQoTm9kZSAqeCl7CiAgICAgCiAgICAgIHZpc2l0ZWRbeC0+ZGF0YV09dHJ1ZTsKICAgICAgY291dDw8IlZpc2l0IFZlcnRleDoiPDx4LT5kYXRhPDwiICI8PGVuZGw7CiAgICAgCiAgICAgfQogICAgIApOb2RlKiBmaW5kQWRqKE5vZGUgKnB0cil7CiAgICAgICAgTm9kZSAqcGs9TlVMTDsKICAgICAgICBwdHI9cHRyLT5saW5rOwogICAgICAgIAogICAgIHdoaWxlKHB0ciE9TlVMTCl7ICAKICAgICAgICAgaWYodmlzaXRlZFtwdHItPmRhdGFdPT1mYWxzZSkKICAgICAgICAgIHJldHVybiBwdHI7CiAgICAgICAgICBwdHI9cHRyLT5saW5rOwogICAgICAgICAgfQogICAgICAgICAgCiAgICAgcmV0dXJuIHBrOwogICAgIAogICAgIH0gIAogICAgIAogICAgIAogICAgIAppbnQgcHVzaChzdGFjayAqcCxOb2RlKiAmeCl7CiAgICAgaWYoaXNmdWxsKHApKQogICAgIHJldHVybiAxOwogICAgIHAtPnNwKys7CiAgICAgcC0+c3RhY2thcnJheVtwLT5zcF09eDsKICAgICAKICAgICB9CiAgICAgCnZvaWQgcG9wKHN0YWNrICpwLE5vZGUgKiZ4KXsKICAgICAKICAgICB4PXAtPnN0YWNrYXJyYXlbcC0+c3AtLV07ICAgICAKICAgICAKICAgICAgCiAgICAgfSAKICAgICAKaW50IGlzZW1wdHkoc3RhY2sgKnApewogICAgIGlmKHAtPnNwPT0tMSkKICAgICByZXR1cm4gMTsKICAgICBlbHNlCiAgICAgcmV0dXJuIDA7ICAgICAgCiAgICAgfQogICAgIAppbnQgaXNmdWxsKHN0YWNrICpwKXsKICAgIGlmKHAtPnNwPT1OLTEpICAgCiAgICByZXR1cm4gMTsKICAgIGVsc2UKICAgIHJldHVybiAwOyAgIAp9CiAgICA=