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;
}

POJ 2230 Watchcow && USACO Watchcow 2005 January Silver (欧拉回路)的更多相关文章

  1. usaco 月赛 2005 january watchcow

    2013-09-18 08:13 //By BLADEVIL var n, m :longint; pre, other :..] of longint; last :..] of longint; ...

  2. usaco 月赛 2005 january sumset

    2013-09-18 08:23 打表找规律 w[i]:=w[i-1];  奇 w[i]:=w[i-1]+w[i div 2];     偶 //By BLADEVIL var w :..] of l ...

  3. USACO月赛2005 january volume

    2013-09-18 08:12 由题可知,ans=∑i  ∑j(x[i]-x[j]) 最后整理完之后应该是不同系数的X[i]相加,所以这道题就成了求不同x[i]的系数 对于X[i],它需要减前面(i ...

  4. [欧拉] poj 2230 Watchcow

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

  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 (欧拉回路)

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

  7. POJ 2230 Watchcow 【欧拉路】

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

  8. POJ 2230 Watchcow 欧拉图

    Watchcow Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8800   Accepted: 3832   Specia ...

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

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

随机推荐

  1. Vue.js——60分钟browserify项目模板快速入门

    概述 在之前的一系列vue.js文章,我们都是用传统模式引用vue.js以及其他的js文件的,这在开发时会产生一些问题. 首先,这限定了我们的开发模式是基于页面的,而不是基于组件的,组件的所有代码都直 ...

  2. js框架总结

    参考地址 http://www.techweb.com.cn/network/system/2015-12-23/2245809.shtml https://www.cnblogs.com/mbail ...

  3. Windows下CURL扩展无效之终极解决办法。

    本地开发环境使用WAMP快速搭建,在使用PHP的CURL时可能会存在无法载入情况,这里提供终极解决方法. 1.在php.ini配置文件中启用 php_curll.dll 扩展: (环境已经自动附带 l ...

  4. hdu6446 Tree and Permutation

    没啥好说的,拆一下贡献就完事了.记dis(x,y)为树上x到y的最短路径,设长度为n的排列中有f(n)个里面x和y相邻(不考虑x和y的顺序),那么f(n)=(n-2)! (n-1) 2,显然这个f(n ...

  5. mysql中LIKE和REGEXP

    mysql中LIKE和REGEXP都可以用来字符匹配 正则表达式REGEXP是为复杂搜索指定模式的强大方式. like用法   LIKE一般与通配符(%)和(_)两个使用 如例 SELECT prod ...

  6. Xcode一个project生成多个App

    网上有很多奇奇怪怪的复杂的方案,其实误导了,方法很简单: 打开项目属性 修改Bundle Identifiler 随便生成了四个App,如下所示:

  7. 51nod 1290 Counting Diff Pairs | 莫队 树状数组

    51nod 1290 Counting Diff Pairs | 莫队 树状数组 题面 一个长度为N的正整数数组A,给出一个数K以及Q个查询,每个查询包含2个数l和r,对于每个查询输出从A[i]到A[ ...

  8. 【ZJOI 2018】 历史(lct)

    历史 题目描述 九条可怜是一个热爱阅读的女孩子. 这个世界有 $n$ 个城市,这 $n$ 个城市被恰好 $n-1$ 条双向道路联通,即任意两个城市都可以互相到达.同时城市 $1$ 坐落在世界的中心,占 ...

  9. 案例:Redis在京东的使用技术模型图

    http://www.100ec.cn  2013年09月27日17:30  中国电子商务研究中心 我要投稿 产品服务   (中国电子商务研究中心讯)Redis是一个开源的,遵守BSD许可协议的key ...

  10. Java考试题之六

    QUESTION 134 Given:11. class Snoochy {12. Boochy booch;13. public Snoochy() { booch = new Boochy(thi ...