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

Hint

OUTPUT DETAILS:

Bessie starts at 1 (barn), goes to 2, then 3, etc...

 
思路:打印欧拉通路,题目保证有解,直接DFS打印即可,代码如下:
const int maxm = ;
const int maxn = ; struct Node {
int from, to;
Node(int _from, int _to) : from(_from), to(_to){}
}; int N, M, vis[maxn*];
vector<int> ans, G[maxm];
vector<Node> edges; void addedge(int u,int v) {
edges.push_back(Node(u, v));
G[u].push_back(edges.size() - );
} void dfs(int x) {
int len = G[x].size();
for(int i = ; i < len; ++i) {
if(!vis[G[x][i]]) {
vis[G[x][i]] = ;
dfs(edges[G[x][i]].to);
ans.push_back(edges[G[x][i]].to);
}
}
} int main() {
scanf("%d%d", &N, &M);
for (int i = ; i < M; ++i) {
int t1, t2;
scanf("%d%d", &t1, &t2);
addedge(t1, t2);
addedge(t2, t1);
}
dfs();
int len = ans.size();
for(int i = ; i < len; ++i)
printf("%d\n", ans[i]);
printf("1\n");
return ;
}

Day4 - D - Watchcow POJ - 2230的更多相关文章

  1. 欧拉回路输出(DFS,不用回溯!)Watchcow POJ 2230

    Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8109   Accepted: 3551   Special Judge D ...

  2. [欧拉] poj 2230 Watchcow

    主题链接: http://poj.org/problem? id=2230 Watchcow Time Limit: 3000MS   Memory Limit: 65536K Total Submi ...

  3. POJ 2230 Watchcow(欧拉回路:输出点路径)

    题目链接:http://poj.org/problem?id=2230 题目大意:给你n个点m条边,Bessie希望能走过每条边两次,且两次的方向相反,让你输出以点的形式输出路径. 解题思路:其实就是 ...

  4. 【POJ 2230】 Watchcow

    [题目链接] http://poj.org/problem?id=2230 [算法] 欧拉回路 [代码] #include <algorithm> #include <bitset& ...

  5. POJ 2230 Watchcow

    Watchcow Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 2 ...

  6. POJ 2230 Watchcow(有向图欧拉回路)

    Bessie's been appointed the new watch-cow for the farm. Every night, it's her job to walk across the ...

  7. POJ 2230 Watchcow (欧拉回路)

    Watchcow Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5258   Accepted: 2206   Specia ...

  8. POJ 2230 Watchcow && USACO Watchcow 2005 January Silver (欧拉回路)

    Description Bessie's been appointed the new watch-cow for the farm. Every night, it's her job to wal ...

  9. POJ 2230 Watchcow 【欧拉路】

    Watchcow Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6336   Accepted: 2743   Specia ...

随机推荐

  1. Linux下给mysql创建用户并分配权限

    // fe_group 用户名// fe 数据库名// 123456 密码 1.新建用户 //登录MYSQL @>mysql -u root -p @>密码 //创建用户 mysql> ...

  2. 创业复盘实战营总结----HHR计划----创业课

    一句话总结课程核心点. 一共4节在线课: 第一节课:<创业学习> 投资人最看重的是CEO的快速学习能力,根据IPO思维模型,如果一共CEO每天input大量的信息,高效的process,而 ...

  3. teraterm中log中加入时间戳

    步骤: 1.Setup->Additional settings->log->Timestamp(Local Time) 2.记录log.File->log(Teraterm. ...

  4. 吴裕雄--天生自然PYTHON爬虫:使用Selenium爬取大型电商网站数据

    用python爬取动态网页时,普通的requests,urllib2无法实现.例如有些网站点击下一页时,会加载新的内容,但是网页的URL却没有改变(没有传入页码相关的参数),requests.urll ...

  5. 《Java Spring框架》Spring IOC 源码分析

    1.下载源码 源码部署:https://www.cnblogs.com/jssj/p/11631881.html 并不强求,最好是有源码(方便理解和查问题). 2. 创建子项目 Spring项目中创建 ...

  6. Spark入门:第4节 Spark程序:1 - 9

    五. Spark角色介绍 Spark是基于内存计算的大数据并行计算框架.因为其基于内存计算,比Hadoop中MapReduce计算框架具有更高的实时性,同时保证了高效容错性和可伸缩性.从2009年诞生 ...

  7. 【PAT甲级】1013 Battle Over Cities (25 分)(并查集,简单联通图)

    题意: 输入三个整数N,M,K(N<=1000,第四个数据1e5<=M<=1e6).有1~N个城市,M条高速公路,K次询问,每次询问输入一个被敌军占领的城市,所有和该城市相连的高速公 ...

  8. [原]OpeanLayers3 For ArcGIS MapServer

    由于OpenLayers3比较新,百度能找到的demo很少,自己不得不参考官方给出的Examples来依葫芦画瓢了,地图服务采用的局方给的ArcGIS MapServer,先上图: 这个例子是按照官方 ...

  9. 尝试实现一个简单的C语言string类型

    用过`C++/Java/python/matlab/JS`等语言后,发现都能很轻松的使用string类型,而C只能这样: char str[] = "hello world"; o ...

  10. C++中文件的读取操作,如何读取多行数据,如何一个一个的读取数据

    练习8.1:编写函数.接受一个istream&参数,返回值类型也是istream&.此函数必须从给定流中读取数据,直至遇到文件结束标识时停止. #include <iostrea ...