Watchcow
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 8800   Accepted: 3832   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...

Source

 
把无向图变成有向图,正反两边存入边的集合,接下来深搜一遍
 
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<map>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const int MAXN = ; //数组得开到两倍,因为这里做处理变成有向图扩大了两倍
struct node {
int to, next;
};
node edge[MAXN];
int head[MAXN], vis[MAXN], n, m, ans;
void dfs( int x ) {
for( int i = head[x]; i != -; i = edge[i].next ) {
if( !vis[i] ) {
vis[i] = ;
dfs(edge[i].to);
}
}
cout << x << endl;
}
int main() {
while( cin >> n >> m ) {
for( int i = ; i < MAXN; i ++ ) {
head[i] = -;
}
ans = ;
memset( vis, , sizeof(vis) );
while( m -- ) {
int x, y;
cin >> x >> y;
edge[ans].to = y;
edge[ans].next = head[x];
head[x] = ans;
ans ++;
edge[ans].to = x;
edge[ans].next = head[y];
head[y] = ans;
ans ++;
}
dfs();
}
return ;
}
 
 

POJ 2230 Watchcow 欧拉图的更多相关文章

  1. [欧拉] poj 2230 Watchcow

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

  2. POJ 2230 Watchcow(欧拉回路:输出点路径)

    题目链接:http://poj.org/problem?id=2230 题目大意:给你n个点m条边,Bessie希望能走过每条边两次,且两次的方向相反,让你输出以点的形式输出路径. 解题思路:其实就是 ...

  3. POJ 2230 Watchcow

    Watchcow Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 2 ...

  4. POJ 2230 Watchcow(有向图欧拉回路)

    Bessie's been appointed the new watch-cow for the farm. Every night, it's her job to walk across the ...

  5. POJ 2230 Watchcow (欧拉回路)

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

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

  7. POJ 2230 Watchcow 【欧拉路】

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

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

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

  9. poj 2230 Watchcow(欧拉回路)

    关键是每条边必须走两遍,重复建边即可,因为确定了必然存在 Euler Circuit ,所以所有判断条件都不需要了. 注意:我是2500ms跑过的,鉴于这道题ac的code奇短,速度奇快,考虑解法应该 ...

随机推荐

  1. JAVA-基础-数据类型转换

    一.类型的转换 java中数据具有类型.这些类型是可以相互进行转换的. 1.自动类型转换 六个和数字相关的基本类型,可以自动由小到大进行类型转换.但是反过来就不行. *注意,在整形自动转浮点型时,有可 ...

  2. S2:类的构造函数

    类的构造函数构造函数名与类名形同,不返回任何值,主要完成对象的初始化工作. (1)在构造函数中,可以给属性设置默认值(2)this只带当前对象 (3)如果不给属性赋初始值,则会以默认值来填充.(4)如 ...

  3. Windows 下配置 Vagrant 环境

    Vagrant是一个基于 Ruby 的工具,用于创建和部署虚拟化开发环境.它使用 Oracle 的开源VirtualBox虚拟化系统. Vagrant 在快速搭建开发环境方面是很赞的,试想一个团队中, ...

  4. PythonDay05

    第五章 今日内容 字典 字典 语法:{'key1':1,'key2':2} 注意:dict保存的数据不是按照我们添加进去的顺序保存的. 是按照hash表的顺序保存的. ⽽hash表 不是连续的. 所以 ...

  5. [TCP/IP]DNS解析

    DNS解析主机的IP地址 host -t A www.baidu.com

  6. java并发编程(十六)----(线程池)java线程池的使用

    上节我们简单介绍了线程池,这次我们就来使用一下.Executors提供四种线程池,分别是:newCachedThreadPool,newFixedThreadPool ,newScheduledThr ...

  7. 有关element 的一些问题(随时更新)

    <el-input></el-input> input  组件中官方自带的change时间是监听失去焦点之后的value变化,要想一只监听value的值变化的话需要使用  @i ...

  8. 聊一聊 SpringBoot 自动配置的原理

    解析思路 我们建立好一个SpringBoot的工程后,我们将从启动类,SpringBootApplication开始进行探究. 开始解析 首先我们建立一个 Springboot的工程.找到启动类,我们 ...

  9. mybatis基础简介

    1.mybatis的加载过程? 程序首先加载mybatis-config.xml文件,根据配置文件创建SQLSessionFactory对象:    然后通过SQLSessionFactory对象创建 ...

  10. OpenStack Telemetry系统架构及实践

    1. 概述 早期OpenStack的计量功能由Ceilometer项目负责,后来Ceilometer一分为四,每个项目负责一个方面的工作.不得不说这是OpenStack开发中的一个特色,比如Cinde ...