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]; 

 int Map[sizen][sizen]; 



 void DFS(int x,int n) 

 { 

     int i; 

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

     mark[x]=true; 

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

     { 

         if(!mark[i]&&Map[x][i]) 

             DFS(i,n); 

     } 

 } 



 int main() 

 { 

     int V,E; 

     int i; 

     int x,y; 

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

     { 

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

         memset(Map,0,sizeof(Map)); 

         while(E--) 

         { 

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

             Map[x][y]=Map[y][x]=true; 

         } 

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

             if(!mark[i]) 

                 DFS(i,V); 

     } 

     return 0; 

 }

举报

相关推荐

0 条评论