#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<cstring>

const int MAX_SUM=110;

using namespace std;

typedef struct ArcNode
{
    int adj;
    struct ArcNode *nextarc;
}ArcNode;

typedef struct VerNode
{

    int data;
    ArcNode *firstarc;
}VerNode, Adjlist[MAX_SUM];

typedef struct
{
    Adjlist verlist;
    int vexnum,arcnum;
}Graph;

void createUDG(Graph &G)
{
    cin>>G.vexnum>>G.arcnum;
    for(int i=1;i<=G.vexnum;i++)
    {
        cin>>G.verlist[i].data;
        G.verlist[i].firstarc=NULL;
    }
    for(int i=1;i<=G.arcnum;i++)
    {
        int x,y;
        cin>>x>>y;
        ArcNode *p1=new ArcNode;
        ArcNode *p2=new ArcNode;
        p1->adj=y;
        p1->nextarc=G.verlist[x].firstarc;
        G.verlist[x].firstarc=p1;

        p2->adj=y;
        p2->nextarc=G.verlist[y].firstarc;
        G.verlist[y].firstarc=p2;
    }
}

void CreateDG(Graph &G)
{
    cin>>G.vexnum>>G.arcnum;
    for(int i=1;i<=G.vexnum;i++)
    {
        cin>>G.verlist[i].data;
        G.verlist[i].firstarc=NULL;
    }
    for(int i=1;i<=G.arcnum;i++)
    {
        int x,y;
        cin>>x>>y;
        ArcNode *p1=new ArcNode;

        p1->adj=y;
        p1->nextarc=G.verlist[x].firstarc;
        G.verlist[x].firstarc=p1;
    }
}

int in[MAX_SUM], out[MAX_SUM];
void getInfo(Graph G)
{
    for(int i=1;i<=G.vexnum;i++)
    {
        ArcNode* p=G.verlist[i].firstarc;
        while(p!=NULL)
        {
            in[p->adj]++;
            out[i]++;
            p=p->nextarc;
        }
    }
}

void printGraph(Graph G)
{
    for(int i=1;i<=G.vexnum;i++)
    {
        ArcNode *p=G.verlist[i].firstarc;
        printf("%d:", i);
        while(p!=NULL)
        {
            cout<<p->adj;
            if(p->nextarc!=NULL)    cout<<" ";
            p=p->nextarc;
        }
        cout<<endl;
    }
}


bool vis[MAX_SUM];
void bfs(Graph G)
{
    memset(vis, 0, sizeof(vis));
    queue<int> Q;
    cout<<"v1 ";
    Q.push(1);
    vis[1]=true;
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        ArcNode* p=G.verlist[u].firstarc;
        while(p!=NULL)
        {

            if(!vis[p->adj])
            {
                printf("v%d ", p->adj);
                vis[p->adj]=1;
                Q.push(p->adj);
            }
            p=p->nextarc;
        }
    }
}
//递归
void dfs(Graph G,int x)
{
    memset(vis, 0, sizeof(vis));
    ArcNode *p=G.verlist[x].firstarc;
    vis[x]=1;
    printf("v%d ", x);
    while(p!=NULL)
    {
        if(!vis[p->adj])    dfs(G, p->adj);
        p=p->nextarc;
    }
}
//非递归
void dfs(Graph G,int x)
{
    memset(vis, 0 ,sizeof(vis));
    stack<int>S;
    S.push(x);
    vis[x]=1;
    printf("v%d ", x);
    while(!S.empty())
    {
        int tt=S.top();
        ArcNode *p=G.verlist[tt].firstarc;
        while(p&&vis[p->adj])   p=p->nextarc;
        while(p&&!vis[p->adj])
        {
            printf("v%d ", p->adj);
            vis[p->adj]=true;
            S.push(p->adj);
            p=G.verlist[p->adj].firstarc;
        }
        if(p==NULL) S.pop();
    }
}

void dfsTraverse(Graph G)
{
    for(v=0;v<G.vexnum;v++)
        vis[v]=false;
    for(v=0;v<G.vexnum;v++)
        if(!vis[v]) dfs(G, v);
}