标签:
Time Limit: 3000MS | Memory Limit: 65536K | |||
Total Submissions: 7473 | Accepted: 3270 | Special Judge |
Description
Input
Output
Sample Input
4 5 1 2 1 4 2 3 2 4 3 4
Sample Output
1 2 3 4 2 1 4 3 2 4 1
#include <cstdio> #include <cstring> using namespace std; const int MAXN=100005; struct Edge{ int to,net; }es[MAXN]; int head[MAXN],tot; int n,m; void addedge(int u,int v) { es[tot].to=v; es[tot].net=head[u]; head[u]=tot++; } int path[MAXN],top; int vis[MAXN]; void dfs(int u) { for(int i=head[u];i!=-1;i=es[i].net) { if(!vis[i]) { vis[i]=1; dfs(es[i].to); } } path[top++]=u;//必须放在for循环之后 } int main() { while(scanf("%d%d",&n,&m)!=EOF) { memset(vis,0,sizeof(vis)); memset(head,-1,sizeof(head)); tot=0; top=0; for(int i=0;i<m;i++) { int u,v; scanf("%d%d",&u,&v); addedge(u,v); addedge(v,u); } dfs(1); for(int i=0;i<top;i++) { printf("%d\n",path[i]); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/program-ccc/p/5815428.html