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

  1. 4 5
  2. 1 2
  3. 1 4
  4. 2 3
  5. 2 4
  6. 3 4

Sample Output

  1. 1
  2. 2
  3. 3
  4. 4
  5. 2
  6. 1
  7. 4
  8. 3
  9. 2
  10. 4
  11. 1

Hint

OUTPUT DETAILS:

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

 
思路:打印欧拉通路,题目保证有解,直接DFS打印即可,代码如下:
  1. const int maxm = ;
  2. const int maxn = ;
  3.  
  4. struct Node {
  5. int from, to;
  6. Node(int _from, int _to) : from(_from), to(_to){}
  7. };
  8.  
  9. int N, M, vis[maxn*];
  10. vector<int> ans, G[maxm];
  11. vector<Node> edges;
  12.  
  13. void addedge(int u,int v) {
  14. edges.push_back(Node(u, v));
  15. G[u].push_back(edges.size() - );
  16. }
  17.  
  18. void dfs(int x) {
  19. int len = G[x].size();
  20. for(int i = ; i < len; ++i) {
  21. if(!vis[G[x][i]]) {
  22. vis[G[x][i]] = ;
  23. dfs(edges[G[x][i]].to);
  24. ans.push_back(edges[G[x][i]].to);
  25. }
  26. }
  27. }
  28.  
  29. int main() {
  30. scanf("%d%d", &N, &M);
  31. for (int i = ; i < M; ++i) {
  32. int t1, t2;
  33. scanf("%d%d", &t1, &t2);
  34. addedge(t1, t2);
  35. addedge(t2, t1);
  36. }
  37. dfs();
  38. int len = ans.size();
  39. for(int i = ; i < len; ++i)
  40. printf("%d\n", ans[i]);
  41. printf("1\n");
  42. return ;
  43. }

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. VS误删sln项目文件怎么办

    以项目名为Test为例 打开Test/Test目录下的 Test.vcxproj 文件,试着运行一下,退出后提示保存sln文件,选择一个目录即可.

  2. Servlet 学习(八)

    Filter 1.功能 Java Servlet 2.3 中新增加的功能,主要作用是对Servlet 容器的请求和响应进行检查和修改 Filter 本身并不生成请求和响应对象,它只提供过滤作用 在Se ...

  3. 吴裕雄--天生自然PythonDjangoWeb企业开发:学员管理系统后台

    需求 提供一个学员管理系统,一个前台页面,展示现有学员,并供新学员提交申请,一个后台,能够处理申请. pip install django==1.11.2 创建项目 使用控制台进入到一个目录下,具体是 ...

  4. 吴裕雄--天生自然ORACLE数据库学习笔记:表分区与索引分区

    create table ware_retail_part --创建一个描述商品零售的数据表 ( id integer primary key,--销售编号 retail_date date,--销售 ...

  5. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 网格系统实例:嵌套列

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. 【转】ERP系统测试方法

    问题: 1.如何进行ERP系统测试用例设计? 2.ERP系统测试用例设计过程? 3.ERP系统测试用例设计的方法?    ERP系统本身是一种业务流程很复杂,单据报表众多,逻辑性很强的系统,质量保证方 ...

  7. C++11 — lambda表达式(匿名函数)

    C++11中lambda表达式的基本语法格式为: [capture](parameters) -> return_type { /* ... */ } 其中 [] 内为外部变量的传递方式: [] ...

  8. 吴裕雄--天生自然HADOOP操作实验学习笔记:ETL案例

    实验目的 熟悉hadoop生态系统 初步了解大数据点击流分析业务 学会使用hadoop进行数据分析统计 实验原理 hadoop主要有三部分,hdfs做数据存储.mapreduce做数据计算.yarn做 ...

  9. C# Show()与ShowDialog()的区别-----转载

    A.WinForm中窗体显示  显示窗体可以有以下2种方法:  Form.ShowDialog方法 (窗体显示为模式窗体)  Form.Show方法 (窗体显示为无模式窗体) 两者具体区别如下:  1 ...

  10. PDO 小知识

    一.前言 PDO(PHP Data Object)提供了一个通用接口访问多种数据库,即抽象的数据模型支持连接多种数据库. PDO扩展为PHP定义了一个访问数据库的轻量.持久的接口.其本身并不能实现任何 ...