分析:起点已知,开个数组来存放路径,注意 vis 数组要初始化!另外,不能忘记了题目还要求回去的路径,只要在 dfs 之后加上就可以了。

#include <bits/stdc++.h>

using namespace std;
int path[1000 * 2 + 10];
int vis[1005];
int gra[1002][1002];
int num = 0;
void dfs(int x, int n)
{
vis[x] = 1;
path[num ++] = x;
for(int i = 1; i <= n; i ++)
{
if(!vis[i] && gra[x][i]){
vis[i] = 1;
dfs(i,n);
path[num ++] = x;
}
}
return ;
}
int main()
{
int t,n,m,s;
while(~scanf("%d",&t)){
while(t--)
{
scanf("%d%d%d",&n,&m,&s);
for(int i = 0; i <= n; i ++)
{
for(int j = 0; j <= n; j ++ )gra[i][j] = 0;
}
int u,v;
for(int i = 0; i < m; i ++){
scanf("%d%d",&u,&v);
gra[u][v] = gra[v][u] = 1;
}
num = 0;
memset(vis,0,sizeof(vis));
dfs(s,n);
for(int i = 0; i < num; i ++)
{
if(i == 0)printf("%d",path[i]);
else printf(" %d",path[i]);
}
if(num != 2 * n - 1)printf(" 0");
printf("\n");
}
}
return 0;
}

最新文章

  1. 运行DbVisualizer报the java_home environment viariable does not point to a working 32-bit JDK OR JRE错误
  2. 实习小记-python中可哈希对象是个啥?what is hashable object in python?
  3. UVa 103 Stacking Boxes --- DAG上的动态规划
  4. Apache httpd.conf的翻译
  5. openStack images
  6. poj2013---二维数组指针使用
  7. C#之自己定义的implicit和explicit转换
  8. Badboy安装与使用
  9. springboot~ EventListener事件监听的使用
  10. DOM中获取宽高、位置总结
  11. OS + CentOS / http_proxy / https_proxy / dalishangwang / repo
  12. Laravel 中通过自定义分页器分页方法实现伪静态分页链接以利于 SEO
  13. (Android数据传递)Intent消息传递机制 “Intent”“数据传递”
  14. C语言进阶之路(一)----C语言的内存四区模型
  15. 执行shell脚本时提示bad interpreter:No such file or directory的解决办法
  16. Redis(八)-- Redis分布式锁实现
  17. Hibernate学习(5)- session的get与load方法对比
  18. Jquery中.bind()、.live()、.delegate()和.on()之间的区别详解
  19. pt-query-digest(percona toolkit)小解
  20. EL表达式获取对象属性的原理

热门文章

  1. nodeJs+vue安装教程详解 相信
  2. Flask无法访问(127.0.0.1:5000)的问题解决方法
  3. 【转载】C#指定文件夹下面的所有内容复制到目标文件夹下面
  4. js钩子函数实现一个简单动画
  5. vue打包后.woff字体文件路径问题处理
  6. nginx: [emerg] directive &quot;upstream&quot; has no opening &quot;{&quot; in /application/nginx-1.6.3/conf/nginx.conf:13 ...
  7. 基于CentOS设置和使用Yum软件仓库
  8. OAuth2在微服务架构中的应用
  9. 电池管理系统(BMS)
  10. python函数式编程-匿名函数