Description

Bessie's been appointed the new watch-cow for the farm. Every night, it's her job to walk across the farm and make sure that no evildoers are doing any evil. She begins at the barn, makes her patrol, and then returns to the barn when she's done.

If she were a more observant cow, she might be able to just walk each of M (1 <= M <= 50,000) bidirectional trails numbered 1..M between N (2 <= N <= 10,000) fields numbered 1..N on the farm once and be confident that she's seen everything she needs to see. But since she isn't, she wants to make sure she walks down each trail exactly twice. It's also important that her two trips along each trail be in opposite directions, so that she doesn't miss the same thing twice.

A pair of fields might be connected by more than one trail. Find a path that Bessie can follow which will meet her requirements. Such a path is guaranteed to exist.

Input

  • Line 1: Two integers, N and M.

  • Lines 2..M+1: Two integers denoting a pair of fields connected by a path.

Output

  • Lines 1..2M+1: A list of fields she passes through, one per line, beginning and ending with the barn at field 1. If more than one solution is possible, output any solution.

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

分析:

题目上要求的是从1号点出发,走过一个回路之后再回到1号点,但是要求的是同一条路径要按照相反的方向各走一遍,到这里我们必须理解到一点就是,对于图上的点来所,有且仅有一个点要走3次,其余的点都要走两次。

由于是无向边,而且每条边要求正反各走一次,所以一定存在欧拉回路。存图时把每条无向边看成两条相反的有向边,直接利用欧拉回路求解。

但是这样的路径走法可能有许多种,我们只需要输出其中一种即可。

代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int n,m;
struct Node
{
int next ;
int to;
} node[100005];
int head[20009];
int Count=0;
void addEdg(int u,int v)//图正反方向都要存储一遍
{
Count++;
node[Count].to=v;
node[Count].next=head[u];
head[u]=Count;
Count++;
node[Count].to=u;
node[Count].next=head[v];
head[v]=Count; }
bool vis[20009];
void dfs(int u)
{
for(int i=head[u]; i ; i=node[i].next)
{
if(vis[i]==1)continue;//该边已经走过了,就不能够再走了
vis[i]=1;
dfs(node[i].to);
}
cout<<u<<endl;
}
int main()
{
int u,v;
scanf("%d%d",&n,&m);
for(int i=0; i<m; i++)
{
scanf("%d%d",&u,&v);
addEdg(u,v);
}
dfs(1);
return 0;
}

最新文章

  1. Dreamweaver扩展注意事项
  2. android studio无法关联源码
  3. Jquery调用Webservice传递Json数组
  4. BZOJ 1898 Swamp 沼泽鳄鱼(矩阵)
  5. 线段树(单点更新)HDU1166、HDU1742
  6. asp.net2.0app开发。
  7. SQLite Code配置DbConfiguration
  8. Ionic2中集成第三方控件Sweetalert
  9. html5应用程序缓存
  10. vue 解决IE不能用的问题
  11. 最小的N个和(堆)
  12. Ex 2_27 矩阵A的平方是A自乘后的乘积,即AA..._第三次作业
  13. Radar Installation POJ - 1328(贪心)
  14. 关闭VS2015的WPF UI调试工具
  15. Linux内核分析(第七周)
  16. 【BZOJ-3672】购票 树分治 + 斜率优化DP
  17. WDK10+VS2015 驱动环境搭建
  18. iptables阐述防火墙
  19. rocketmq双主发送消息 SLAVE_NOT_AVAILABLE 状态
  20. JVM生命周期

热门文章

  1. [转帖]Cgroups 与 Systemd
  2. C++的继承与多态
  3. QT源码解析(七)Qt创建窗体的过程,作者“ tingsking18 ”(真正的创建QPushButton是在show()方法中,show()方法又调用了setVisible方法)
  4. linux c 判断文件存在,遍历文件,随机修改文件内容
  5. 相见恨晚的 scala - 01 [ 基础 ]
  6. python2 python3共存解决方案
  7. oracle exp导出加上过滤条件
  8. MT【147】又见最大最小
  9. 【BZOJ1079】【SCOI2008】着色方案
  10. 洛谷 P4363 [九省联考2018]一双木棋chess 解题报告