http://poj.org/problem?id=2230

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...

题解:

一个图是欧拉图,那么他的子图也是一个欧拉图,只需dfs即可

#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=1e5+10;
struct node{
int v;
int next;
}G[MAXN];
int head[MAXN],cnt;
bool vis[MAXN];
int ans[MAXN];
void add(int u,int v)
{
G[++cnt].v=v;
G[cnt].next=head[u];
head[u]=cnt;
}
int n,m,k=0;
int dfs(int u)
{
for (int i = head[u]; i!=-1 ; i=G[i].next) {
if(!vis[i])
{
vis[i]= true;
dfs(G[i].v);
ans[k++]=G[i].v;
}
}
}
int main() {
while (scanf("%d%d",&n,&m)!=EOF)
{
cnt=0,k=0;
memset(head,-1, sizeof(head));
memset(vis,false, sizeof(vis));
int u,v;
for (int i = 0; i <m ; ++i) {
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs(1);
for (int i = 0; i <k ; ++i) {
printf("%d\n",ans[i]);
}
printf("1\n");
}
return 0;
}
//poj2230

  

poj2230 欧拉回路的更多相关文章

  1. 0x66 Tarjan算法与无向图联通性

    bzoj1123: [POI2008]BLO poj3694 先e-DCC缩点,此时图就变成了树,树上每一条边都是桥.对于添加边的操作,相当于和树上一条路径构环,导致该路径上所有边都不成为桥.那么找这 ...

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

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

  3. POJ2230 Watchcow【欧拉回路】

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

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

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

  5. POJ2230(打印欧拉回路)

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

  6. POJ2230Watchcow[欧拉回路]

    Watchcow Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 7512   Accepted: 3290   Specia ...

  7. ACM/ICPC 之 混合图的欧拉回路判定-网络流(POJ1637)

    //网络流判定混合图欧拉回路 //通过网络流使得各点的出入度相同则possible,否则impossible //残留网络的权值为可改变方向的次数,即n个双向边则有n次 //Time:157Ms Me ...

  8. [poj2337]求字典序最小欧拉回路

    注意:找出一条欧拉回路,与判定这个图能不能一笔联通...是不同的概念 c++奇怪的编译规则...生不如死啊... string怎么用啊...cincout来救? 可以直接.length()我也是长见识 ...

  9. ACM: FZU 2112 Tickets - 欧拉回路 - 并查集

     FZU 2112 Tickets Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u P ...

随机推荐

  1. Nginx+Tomcat+Session 高性能群集搭建

    随着IT行业的发展,linux服务器在企业中应用广泛,人们对linux上的应用服务要求也越来越高,早先的apache服务器.apache有优点也 有不足,apache渐渐不能满足人们的要求,目前ngi ...

  2. Redux概念简述

    react可以写出一些比较简单的一些项目,但是只能写出很简单的一些项目,原因是什么呢,原因是react是一个非常轻量级的是视图层框架,打开官网可以看到大大的一行字,A JavaScript libra ...

  3. build.gradle中的dependencies

    demo_myna中的build.gradle中的dependencies是依赖项目.比如之前开发的一个项目A,现在新的项目B要使用项目A的功能,那么把项目A作为类库关联进来,这样b就能直接使用A的功 ...

  4. c#运用this.invoke() 在多线程时对UI进行修改

    什么是进程呢?当一个程序开始运行时,它就是一个进程,进程所指包括运行中的程序和程序所使用到的内存和系统资源.而一个进程又是由多个线程所组成的,线程是程序中的一个执行流,每个线程都有自己的专有寄存器(栈 ...

  5. ConcurrentMap注意事项

    java.util.concurrent.ConcurrentMap接口和ConcurrentHashMap实现类 只能在键不存在时将元素加入到map中, 只有在键存在并映射到特定值时才能从map中删 ...

  6. SqlSugar之SqlQueryDynamic返回值处理

    现在有个需求,有一张表每个月表名都会变的,但结构是一样的,我们不能再用类映射来完成的,我不能每个月都去手动添加,我们只能使用sql语句来完成这个需求.为了方便我这边选择的是SqlQueryDynami ...

  7. gcc扩展语法一:在表达式中的语句和声明

    在GNU C中包含在括号中的复合语句可以作为一个表达式.这就允许你在表达式中使用循环,switch和局部变量. 以前复合语句是包含在大括号中语句序列.在这种构造中,圆括号包围在大括号中.如下面的例子: ...

  8. EasyUI加zTree使用解析 easyui修改操作的表单回显方法 验证框提交表单前验证 datagrid的load方法

    带参提交一次查询,从服务器加载新数据.这是一个神奇的方法 $('#dg').datagrid('load',{ code: '01', name: 'name01' }); easyui修改操作的回显 ...

  9. iOS 获取APP的CPU、内存等信息

    目标是开发一个SDK,嵌入到APP里面,用来统计当前APP的实时CPU.内存等信息 2015.11.17 http://stackoverflow.com/questions/12889422/ios ...

  10. c# 分析SQL语句中的表操作

    最近写了很多方向的总结和demo.基本包含了工作中的很多方面,毕竟c#已经高度封装并且提供了很多类库.前面已经总结了博文.最近2天突然感觉前面的SQL分析阻组件的确麻烦,也注意看了下.为了方便大家学习 ...