Watchcow

Time Limit: 3000MS Memory Limit: 65536K

Total Submissions: 9974 Accepted: 4307 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

USACO 2005 January Silver

这道题当时做的时候,觉得好难啊,学长说这是欧拉回路,然后我一想没学,后来在课程总结中发现原来学了,自己没注意。对全图进行dfs,从规定起点开始,过程中记录经过了哪些边,以保证每条边只经过一次。当一个点的所有边都遍历完成后,把该点入栈。最后依次弹栈得到的就是欧拉路径。被入栈的点都是走投无路的点,如果存在欧拉路径,第一次出现 没有边一定是在走回到起点时,因为其他情况无论怎么走只可能略过一些边,而不可能走进死路,所以若存在欧拉回路,必定在最后一个点的最后一条边回到起始点。

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn 10005
const int maxm 100005
struct Edge
{
int v, next;
}edge[maxm];
int n, m;
int head[maxn];
int ecount;
bool vis[maxm];
void addedge(int a, int b) //接下一个边
{
edge[ecount].v = b;
edge[ecount].next = head[a];
head[a] = ecount++;
}
void dfs(int a)
{
for (int i = head[a]; i != -1; i = edge[i].next)
{
if (vis[i])
continue;
int v= edge[i].v;
vis[i] = true;
dfs(v);
}
printf("%d\n", a + 1); 从栈顶开始向下打印。
}
int main()
{
memset(head, -1, sizeof(head));
memset(vis, 0, sizeof(vis));
ecount = 0;
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++)
{
int a, b;
scanf("%d %d", &a, &b);
a--;
b--;
addedge(a, b);
addedge(b, a);
}
dfs(0);
return 0;
}

POJ 2230 Watchcow 欧拉回路的DFS解法(模板题)的更多相关文章

  1. [欧拉] poj 2230 Watchcow

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

  2. POJ 3264:Balanced Lineup(RMQ模板题)

    http://poj.org/problem?id=3264 题意:给出n个数,还有q个询问,询问[l,r]区间里面最大值和最小值的差值. 思路:RMQ模板题,开两个数组维护最大值和最小值就行. #i ...

  3. 【POJ 2104】 K-th Number 主席树模板题

    达神主席树讲解传送门:http://blog.csdn.net/dad3zz/article/details/50638026 2016-02-23:真的是模板题诶,主席树模板水过.今天新校网不好,没 ...

  4. POJ 2029 Get Many Persimmon Trees (模板题)【二维树状数组】

    <题目链接> 题目大意: 给你一个H*W的矩阵,再告诉你有n个坐标有点,问你一个w*h的小矩阵最多能够包括多少个点. 解题分析:二维树状数组模板题. #include <cstdio ...

  5. POJ 2774 Long Long Message 后缀数组模板题

    题意 给定字符串A.B,求其最长公共子串 后缀数组模板题,求出height数组,判断sa[i]与sa[i-1]是否分属字符串A.B,统计答案即可. #include <cstdio> #i ...

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

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

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

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

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

  9. POJ 2230 Watchcow (欧拉回路)

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

随机推荐

  1. 一个spring 基本知识的微博(怎么加载多个xml、多个property文件、aop配置、监视器)

    http://blog.sina.com.cn/s/blog_61c5866d0100ev44.html

  2. jmeter 信息头Bearer

    1.数据规则 2.登录时获取token信息 3.正则表达式获取token值 说明: (1)引用名称:下一个请求要引用的参数名称,如填写title,则可用${title}引用它. (2)正则表达式: ( ...

  3. [模板]SPFA判负环

    目录 一.BFS法判负环 二.DFS法判负环 三.SPFA判正环 一.BFS法判负环 Code: #include<bits/stdc++.h> #define re register # ...

  4. 使用malloc和free函数进行内存动态分配

    一.在学习c语言里面,内存分配这个话题非常有意思,因为我们平时在开发的时候,如果一不小心没注意内存释放的话,写的的程序很容易出错,所以今天就来回顾一下c语言里面的内存动态分配,下面我们先来看一个实例来 ...

  5. 收集免费的接口服务,做一个api的搬运工

    hello, 大家好,今天给大家推荐的开源项目在某种程度上极大的方便了广大的开发者,这个开源项目统计了网上诸多的免费API,为广大开发者收集免费的接口服务,专心致志做一个API的搬运工,每月定时更新新 ...

  6. python白帽子/黑客/实战编程教程

    Python搜索爬虫抓取超高清视频教程_第一期Python搜索爬虫抓取超高清视频教程_第二期Python搜索爬虫抓取视频教程_第三期Python搜索爬虫抓取视频教程_第四期Python搜索引擎爬虫抓取 ...

  7. redis 分布式锁的 5个坑,真是又大又深

    引言 最近项目上线的频率颇高,连着几天加班熬夜,身体有点吃不消精神也有些萎靡,无奈业务方催的紧,工期就在眼前只能硬着头皮上了.脑子浑浑噩噩的时候,写的就不能叫代码,可以直接叫做Bug.我就熬夜写了一个 ...

  8. C#开发BIMFACE系列32 服务端API之模型对比3:批量获取模型对比状态

    系列目录     [已更新最新开发文章,点击查看详细] 在<C#开发BIMFACE系列31 服务端API之模型对比2:获取模型对比状态>中介绍了根据对比ID,获取一笔记录的对比状态.由于模 ...

  9. GitHub 热点速览 Vol.17:在?各家视频会员要不要?

    作者:HelloGitHub-小鱼干 摘要:经济实用,用作上周的 GitHub 热点的横批再合适不过.先不说 GitHub Trending 上不止一个的会员共享项目,免你找好友刷脸要会员,这项目实在 ...

  10. Ubuntu16.04 安装eclipse

    首先确保自己的Ubuntu已经安装了jdk并且配置好了环境变量 然后在官网下载相应的eclipse安装包: https://www.eclipse.org/downloads/packages/ 下载 ...