#include< stdio.h> #include< stdlib.h> struct node{ int vertex; int initial,end; struct node *next; } *arr[50], *p, *q, *p2, *p3; int visited[50]={0}; int t=1; void DFS_main(int); void DFS(struct node *); int main(){ int u,v,n,i; scanf("%d",&n); for(i=1;i<=n;i++){ arr[i]=(struct node *)malloc(sizeof(struct node)); arr[i]->vertex=i; arr[i]->next=NULL; } while(5){ scanf("%d%d",&u,&v); if(u==-1&&v==-1) break; p=arr[u]; while(p->next!=NULL) p=p->next; q=(struct node *)malloc(sizeof(struct node)); q->vertex=v; q->next=NULL; p->next=q; } p=arr[1]; printf("The DFS for the Graph is\n"); DFS_main(n); printf("out of dfs\n"); printf("\n"); for(i=1;i<=n;i++){ //p2=arr[i]; printf("%d %d %d\n",arr[i]->vertex,arr[i]->initial,arr[i]->end); } return 0; } void DFS_main(int n){ int i; for(i=1;i<=n;i++){ //printf("start vertex...%d\n",arr[i]->vertex); p=arr[i]; if(visited[p->vertex]==0) DFS(p); } } void DFS(struct node *p1){ visited[p1->vertex]=1; p1->initial=t++; printf("%d ---->",p1->vertex); p2=p1; while((p2=p2->next)!=NULL){ if(visited[p2->vertex]==0){ printf("%d \n",p2->vertex); p3=arr[p2->vertex]; DFS(p3); } } p1->end=t++; //printf(" %d\n",p1->end); }