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. vmware克隆的linux机器网络不通

    当我使用vmware的完全克隆功能克隆出两台虚拟机之后,登录发现网络不通,仔细检查发现几个问题,由于克隆之后默认的eth0网卡在系统中会变成eth1,导致之前的eth0网卡配置信息无法加载,网络不通, ...

  2. php文件编程

    一:文件常见操作 流的概念:当数据从程序(内存)->文件(磁盘),我们称为输出流,当数据从文件(磁盘)->程序(内存),我们称为输入流 1,获取文件信息 <?php //打开文件 f ...

  3. 【Spring实战】—— 13 AspectJ注解切面

    前面了解了典型的AOP基于配置的使用方法,下面介绍下如何依赖于注解来实现AOP. 基于注解降低了配置文件的复杂程度,但是引入了程序间的耦合,其中的优劣待用户自己判断了. 需要注意的是,确定Aspect ...

  4. python入门11 元组tuple

    tuple元组是一种不可变数据类型,也是一种序列,因此可用序列的各类方法,比如切片和索引 #coding:utf-8 #/usr/bin/python """ 2018- ...

  5. 『看球笔记』20140208利物浦VS阿森纳,十字重剑与蜜蜂飞舞

      红圈位置42%对58%的控球率,是不是觉得比分很不符?我们卖个关子,最后再说这个.   十字重剑     第一次任意球防守,俩后卫盯俩后卫,负责斯科特尔的是科斯切尔尼,而默特萨克盯图雷.     ...

  6. POJ 1503 大整数

    之前做的大整数,都是一位一位操作. 优化方案:压缩方案. 模板: + - *  操作符重载 #include<cstdio> #include<iostream> #inclu ...

  7. POJ 2531 深搜剪枝

    题意:全局最大割. 分析:有相应的算法,数据量很小,可以枚举源点,汇点,最大流. 这里用DFS,状态定义:分成两个集合,刚开始S集合全部点,然后一个一个放,这是一个回溯的过程. 没剪枝也过了. 剪枝技 ...

  8. 2018.12.2 Mac环境下mysql图形化界面的Navicat premium 12 中文版安装与激活

    软件链接: https://pan.baidu.com/s/1ZUNLQ1DW9rQZUzDXQn2rWQ 提取码: 8i78 复制这段内容后打开百度网盘手机App,操作更方便哦 注意最新版 12.0 ...

  9. caffe卷积层实现

    下图是jiayangqing在知乎上的回答,其实过程就是把image转换成矩阵,然后进行矩阵运算 卷积的实现在conv_layer层,conv_layer层继承了base_conv_layer层,ba ...

  10. 【题解】洛谷P3200 [HNOI2009] 有趣的数列(卡特兰数+质因数分解)

    洛谷P3200:https://www.luogu.org/problemnew/show/P3200 思路 这题明显是卡特兰数的题型咯 一看精度有点大 如果递推卡特兰数公式要到O(n2) 可以证明得 ...