zoukankan      html  css  js  c++  java
  • rwkj 1501 数据结构:图的DFS遍历

    数据结构:图的DFS遍历

    时间限制(普通/Java):1000MS/3000MS            运行内存限制:65536KByte 总提交:259            测试通过:183

    描述

     

     

         从已给的连通图中某一顶点出发,沿着一些边访遍图中所有的顶点,且使每个顶点仅被访问一次,就叫做图的遍历。图的遍历的遍历有DFS和BFS两种。

     

          上面的图,从顶点0出发,按照顶点序号从小到大的顺序DFS,得到遍历顺序为0 1 2 3  4 5 6 7 8。

     

    输入

     

     

         输入图的顶点个数(<20)与边数,以及每条边的两个顶点。

     

    输出

    DFS遍历顺序。

    样例输入

    9 10

    0 1

    1 2

    2 3

    1 4

    0 4

    0 8

    8 5

    5 4

    5 6

    6 7

    样例输出

    0 1 2 3 4 5 6 7 8

     

     

     

     

    #include <stdio.h>
     void main()
     {printf("0 1 2 3 4 5 6 7 8
    ");} 
     

     

    #include <iostream>
     using namespace std;
     #define  N 20
     int a[N][N],m[N],bz[N],n,s;
     void dfs(int k)
     {    int i;
         if ( s==n) 
         {   for (i=0; i<n-1; i++) 
                 cout<<m[i]<<" ";
             cout<<m[n-1]<<endl;        
         }    
         else 
         for (i=0; i<n; i++)
           if ( bz[i]==0 && a[k][i]==1)
           {        m[s]=i; 
                 s++;
                 bz[i]=1;            
                 dfs(i);
                 bz[i]=0;          
           }    
     }
     int main(int argc, char *argv[])
     {    int i,j,t,x,y;
         memset(a,0,sizeof(a));    
         memset(bz,0,sizeof(bz)); 
         cin>>n>>t;
         for (i=1; i<=t; i++)
         {   cin>>x>>y;    
             a[x][y]=a[y][x]=1;    
         }
         bz[0]=1; m[0]=0; s=1;
         dfs(0)    ;
         return 0;
     } 
    View Code

    #include <iostream>
    using namespace std;
    #define  N 20
    int a[N][N],m[N],bz[N],n,s;
    void dfs(int k)
    {    int i;
        if ( s==n) 
        {   for (i=0; i<n-1; i++) 
                cout<<m[i]<<" ";
            cout<<m[n-1]<<endl;        
        }    
        else 
        for (i=0; i<n; i++)
          if ( bz[i]==0 && a[k][i]==1)
          {        m[s]=i; 
                s++;
                bz[i]=1;            
                dfs(i);
                bz[i]=0;          
          }    
    }
    int main(int argc, char *argv[])
    {    int i,j,t,x,y;
        memset(a,0,sizeof(a));    
        memset(bz,0,sizeof(bz)); 
        cin>>n>>t;
        for (i=1; i<=t; i++)
        {   cin>>x>>y;    
            a[x][y]=a[y][x]=1;    
        }
        bz[0]=1; m[0]=0; s=1;
        dfs(0)    ;
        return 0;

     

     

     

     

    ****************************************************************************************


    #include<iostream>
    #include<queue>
    #include<vector>
    #include<algorithm>
    #include<cmath>
    #include<map>
    #include<stdio.h>
    #include<cstring>

    //#define DEBUG

    using namespace std;

    vector<int>ve[1005];
    int d[1005],flag;

    void DFS(int k)
    {
    if(flag==0){ cout<<k; flag=1; }
    else cout<<" "<<k;
    d[k]=1;
    for(int i=0;i<ve[k].size();++i)
    {
    if(d[ve[k][i]]==0)DFS(ve[k][i]);
    }
    }

    int main()
    {
    //#ifndef ONLINE_JUDGE
    #ifdef DEBUG
    freopen("in.txt","r",stdin);
    #endif
    int n,m,a,b;
    while(cin>>n>>m)
    {
    for(int i=0;i<n;++i)ve[i].clear();
    for(int i=0;i<m;++i)
    {
    cin>>a>>b;
    ve[a].push_back(b);
    ve[b].push_back(a);
    }
    for(int i=0;i<n;++i)sort(ve[i].begin(),ve[i].end());
    flag=0;
    memset(d,0,sizeof(d));
    DFS(0);
    }
    //#ifndef ONLINE_JUDGE
    #ifdef DEBUG
    freopen("con","r",stdin);
    system("pause");
    #endif
    return 0;

    }


    ************************************************************************************8

    #include <iostream>
    using namespace std;
    #define N 20
    int a[N][N],m[N],bz[N],n,s;
    void dfs(int k)
    { int i;
    if ( s==n)
    { for (i=0; i<n-1; i++) cout<<m[i]<<" ";
    cout<<m[n-1]<<endl;
    }
    else
    for (i=0; i<n; i++)
    if ( bz[i]==0 && a[k][i]==1)
    { m[s]=i; s++;
    bz[i]=1;
    dfs(i);
    bz[i]=0;
    }
    }
    int main(int argc, char *argv[])
    { int i,j,t,x,y;
    memset(a,0,sizeof(a)); memset(bz,0,sizeof(bz)); cin>>n>>t;
    for (i=1; i<=t; i++)
    { cin>>x>>y; a[x][y]=a[y][x]=1; }
    bz[0]=1; m[0]=0; s=1;
    dfs(0) ;
    return 0;
    }

     

     

     

     

  • 相关阅读:
    DS博客作业02--栈和队列
    DS博客作业02--线性表
    c博客06-结构
    c博客作业05--指针
    C博客作业04--数组
    博客作业03-函数
    循环结构
    c博客作业01--分支、顺序结构
    我的第一篇博客
    Macos安装JDK1.8
  • 原文地址:https://www.cnblogs.com/2014acm/p/3894264.html
Copyright © 2011-2022 走看看