提交时间:2022-06-19 16:50:28

运行 ID: 41971

#include<bits/stdc++.h> using namespace std; const int M=101,N=201; int n,m,cnt; int h[M],e[N],ne[N],idx; bool state[M]; void add(int x,int y){ idx++,e[idx]=y,ne[idx]=h[x],h[x]=idx; } void dfs(int x){ state[x]=1,cnt++; cout<<x<<" "; if(cnt==n) cout<<e[h[x]]; for(int i=h[x];i!=-1;i=ne[i]){ if(!state[e[i]]) dfs(e[i]); } } int main(){ memset(h,-1,sizeof(h)); cin>>n>>m; while(m--){ int x,y; cin>>x>>y; add(x,y),add(y,x); } dfs(1); return 0; }