0
点赞
收藏
分享

微信扫一扫

链式向前星 DFS

#include<stdio.h> 

 #include<stdlib.h> 

 #include<string.h> 

 #include<math.h> 

 #include<iostream> 

 #include<algorithm> 

 using namespace std; 



 const int sizen=10000; 

 bool mark[sizen]; 

 struct ele 

 { 

     int to; 

     int w; 

     int next; 

 }p[sizen]; 

 int V,E,e; 

 int head[sizen]; 



 void add(int x,int y,int z) 

 { 

     p[e].to=y; 

     p[e].w=z; 

     p[e].next=head[x]; 

     head[x]=e++; 

 } 



 void DFS(int x,int n) 

 { 

     int i; 

     printf("%d\n",x); 

     mark[x]=true; 

     for(i=head[x];i!=-1;i=p[i].next) 

         if(!mark[p[i].to]) 

             DFS(p[i].to,n); 

 } 



 int main() 

 { 

     int x,y,z; 

     int i; 

     while(scanf("%d%d",&V,&E)!=EOF) 

     { 

         memset(head,-1,sizeof(head)); 

         memset(mark,false,sizeof(mark)); 

         e=0; 

         while(E--) 

         { 

             scanf("%d%d%d",&x,&y,&z); 

             add(x,y,z); 

             add(y,x,z); 

         } 

         for(i=1;i<=V;i++) 

             if(!mark[i]) 

                 DFS(i,V); 

     } 

     return 0; 

 }

举报

相关推荐

0 条评论