B - diagram of the experimental data on the structure IV: Exploring the maze

Description

A labyrinth underground, its passage is straight, and has a lamp and a crosspoint switch all channels (channels inclusive); How all lighting start from a starting point in the maze and back lights to the starting point?
Input

Continuous T set of data inputs, each data of the first line gives the three positive integers, respectively, an underground maze of nodes N (1 <N <= 1000), the number of edges M (M <= 3000) and the starting node No. S, then M M rows corresponding to the sides, one pair for each row is given a positive integer, indicates the number of vertices associated with the two one side.

Output

If the lamp can be lit all the nodes, the output S from the start and end of a sequence S, the sequence must have edges adjacent vertices, or only the output of the lighting sequence of the junction portion of the lamp, the final output 0, denotes this labyrinth is not communicated FIG.
In order agreed number of smaller nodes access priority access vertex, all lit lamps can be lit, in the same way back to the beginning of the return way.
Sample

Input

1
6 8 1
1 2
2 3
3 4
4 5
5 6
6 4
3 6
1 5
Output

1 2 3 4 5 6 5 4 3 2 1

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
int a[110][110],vis[1100];
int n,k;
int ans[1100];
void dfs(int m)
{
    vis[m] = 1;
    ans[k++] = m;
    for(int i=1;i<=n;i++)
    {
        if(!vis[i]&&a[m][i]==1)
        {
            //vis[i] = 1;
            dfs(i);
            ans[k++]=m;
        }
    }
}
int main()
{
    int t,m,p;
    int u,v;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d %d",&n,&m,&p);
        memset(vis,0,sizeof(vis));
        memset(a,0,sizeof(a));
        k = 0;
        for(int i=0;i<m;i++)
        {
            scanf("%d %d",&u,&v);
            a[u][v] = a[v][u] = 1;
        }
        dfs(p);
        for(int i=0;i<k;i++)
        {
            if(i==0)
                printf("%d",ans[i]);
            else
                printf(" %d",ans[i]);
        }
        if(k!=2*n-1)
            printf(" 0");
        printf("\n");
    }
    return 0;
}

Published 177 original articles · won praise 7 · views 30000 +

Guess you like

Origin blog.csdn.net/Fusheng_Yizhao/article/details/104881148