Watchcow
Time Limit: 3000MS
Memory Limit: 65536K
Total Submissions: 6172Accepted: 2663
Special Judge

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

Hint

OUTPUT DETAILS:

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

题目大意:给你一个N个点的图,M条双向边,从原点1出发,两个方向各走

一遍。最后回到原点。输出整个路径。

从1開始。到1结束。

共2*M+1行。

思路:DFS遍历,vis数组标记已遍历的边。

DFS的思想等效于先找一个环。然后对环上全部点递归DFS。而且把这些递归

产生的路插入这个环中。

最重要的地方是在哪里保存路径。由于DFS函数的结

束顺序就是点的回溯顺序。所以应该在DFS回溯完之后再记录当前点的序号,

也就是now的值。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN = 10010;
const int MAXM = 100010; int head[MAXN],N,M;
struct EdgeNode
{
int to;
int w;
int next;
}; EdgeNode Edges[MAXM]; int ans[MAXM],ansi;
bool vis[MAXM];
void DFS(int now)
{
int k;
for(k = head[now]; k != -1; k = Edges[k].next)
{
if(!vis[k])
{
vis[k] = true;
DFS(Edges[k].to);
}
}
ans[ansi++] = now;
} int main()
{
while(cin >> N >> M)
{
int x,y;
memset(Edges,0,sizeof(Edges));
memset(head,-1,sizeof(head));
memset(ans,0,sizeof(ans));
memset(vis,0,sizeof(vis));
int j = 0;
for(int i = 0; i < M; ++i)
{
cin >> x >> y;
Edges[j].to = y;
Edges[j].w = 1;
Edges[j].next = head[x];
head[x] = j;
j++;
Edges[j].to = x;
Edges[j].w = 1;
Edges[j].next = head[y];
head[y] = j;
j++;
}
ansi = 0;
DFS(1);
for(int i = 0; i < ansi; ++i)
cout << ans[i] << endl;
} return 0;
}

POJ2230 Watchcow【欧拉回路】的更多相关文章

  1. Watchcow(POJ2230+双向欧拉回路+打印路径)

    题目链接:http://poj.org/problem?id=2230 题目: 题意:给你m条路径,求一条路径使得从1出发最后回到1,并满足每条路径都恰好被沿着正反两个方向经过一次. 思路:由于可以回 ...

  2. poj2230 Watchcow【欧拉回路】【输出路径】(遍历所有边的两个方向)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4392 题目大意: 一个图,要将每条边恰好遍历两遍,而且要以不同的方向,还要回到原点. dfs解法    ...

  3. POJ22230 Watchcow (欧拉回路)

    Watchcow Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6477   Accepted: 2823   Specia ...

  4. POJ 2230 Watchcow 欧拉回路的DFS解法(模板题)

    Watchcow Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9974 Accepted: 4307 Special Judg ...

  5. POJ2230 Watchcow

    原题链接 类欧拉回路,要求每条边被正反各经过一次,且从\(1\)出发并回到\(1\). 只需每次搜索该点的边时,将该点的边对应的邻接表头及时修改为下一条即可,因为邻接表恰好储存了正反方向的边,所以及时 ...

  6. POJ2230(打印欧拉回路)

    Watchcow Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 7473   Accepted: 3270   Specia ...

  7. 【转】欧拉回路&特殊图下的哈密顿回路题集

    转自:http://blog.csdn.net/shahdza/article/details/7779385 欧拉回路[HDU]1878 欧拉回路 判断3018 Ant Trip 一笔画问题1116 ...

  8. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  9. poj2月题解

    竟然生日前一天poj破百,不错不错,加速前进! poj2437 由于泥泞不重叠,所以按其实左边排个序再统计一遍即可(如果不是刚好盖满就尽量往后盖) poj2435 细节bfs poj2230 求欧拉回 ...

随机推荐

  1. oracle系统包——dbms job用法(oracle定时任务)

    用于安排和管理作业队列,通过使用作业,可以使ORACLE数据库定期执行特定的任务. 一.dbms_job涉及到的知识点1.创建job:variable jobno number;dbms_job.su ...

  2. Xcode工程添加第三方文件的详细分析 Create folder references for any added folders

    在开发iOS项目的时候需要导入第三方的库文件,但是通过Xcode导入第三方源文件的时候会提示一些信息,不知所以然. 现在看到的文档都是针对Xcode3的,针对Xcode4的说明很少,现在分享出来. 官 ...

  3. 跟我学android-使用Eclipse开发第一个Android应用(三)

    打开Eclipse,选择 File—New –Android Application Project Application Name  就是我们的 应用名称,也是我们在手机应用程序列表里看到的名称. ...

  4. c#读取进程列表判断程序是否已经启动(转)

    方法一: using System.Diagnostics; Process[] vProcesses = Process.GetProcesses(); foreach (Process vProc ...

  5. JS中 submit提交与Form表单里的onsubmit的调用问题?

    最近在开发中遇到了表单提交前验证的问题,用一个普通的button按钮代替submit按钮,在提交前触发这个button的onclick事件,在其事件中触发form的submit事件.问题出现了: &l ...

  6. ASP.NET程序从IIS6移植到IIS7时出现500.22错误

    最可能的原因:  •    此应用程序在 system.web/httpModules 节中定义配置.  可尝试的操作:  •    将配置迁移到 system.webServer/modules 节 ...

  7. 成都Python工程师招聘

    最近公司开放python岗位,机会非常不错. 数据相关的岗位,可以接触很多好玩技术.成都是新建研发中心,整体技术气氛很不错. 坐标:成都 行业/部门:金融,数据部门 基本要求: python精通,学习 ...

  8. centos+nginx+uwsgi+virtualenv+flask 多站点环境搭建

    环境: centos x64 6.6 nginx 1.6.2 python 2.7.9 uwsgi 2.0.9 virtualenv 12.0.5 flask 0.10.1 正文: 1.安装nginx ...

  9. HDU 5224 Tom and paper(最小周长)

    HDU 5224 Tom and paper(最小周长) Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d &a ...

  10. Xcode升级导致插件失效的解决办法-b

    作为iOS界的攻城师,每一次水果发布新的Xcode开发版本时,我们都会跟进,然而那些好用的Xcode插件都会莫名的失灵...对此我各种百度,在这里,我将跟大家分享我是如何解决这些问题的.当然,我的方案 ...