SDUT- 2107 数据结构实验之图论二:图的深度遍历

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int a[105][105];
int vis[105];
int qu[10005], front, tail, flag, n;
void init()
{
    memset(a, 0, sizeof(a));
    memset(vis, 0, sizeof(vis));
    front = tail = flag = 0;
}
void dfs(int x)
{
    if(flag == 0){printf("%d", x); flag = 1;}
    else printf(" %d", x);
    vis[x] = 1;
    for(int i = 0; i < n; i++)
    {
        if(a[x][i] == 1 && vis[i] == 0)dfs(i);
    }
}
int main()
{
    int t, m, i, j;
    scanf("%d", &t);
    while(t--)
    {
        init();
        scanf("%d %d", &n, &m);
        for(i = 1; i <= m; i++)
        {
            int u, v;
            scanf("%d %d", &u, &v);
            a[u][v] = a[v][u] = 1;
        }
        dfs(0);
        printf("\n");
    }
    return 0;
}

 

    原文作者:数据结构之图
    原文地址: https://blog.csdn.net/miracle_qsh/article/details/83033053
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞