POJ2230(打印欧拉回路)
| Time Limit: 3000MS | Memory Limit: 65536K | |||
| Total Submissions: 7473 | Accepted: 3270 | Special Judge | ||
Description
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
* Lines 2..M+1: Two integers denoting a pair of fields connected by a path.
Output
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
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=;
struct Edge{
int to,net;
}es[MAXN];
int head[MAXN],tot;
int n,m;
void addedge(int u,int v)
{
es[tot].to=v;
es[tot].net=head[u];
head[u]=tot++;
} int path[MAXN],top;
int vis[MAXN];
void dfs(int u)
{
for(int i=head[u];i!=-;i=es[i].net)
{
if(!vis[i])
{
vis[i]=;
dfs(es[i].to);
}
}
path[top++]=u;//必须放在for循环之后
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(vis,,sizeof(vis));
memset(head,-,sizeof(head));
tot=;
top=;
for(int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
dfs();
for(int i=;i<top;i++)
{
printf("%d\n",path[i]);
}
}
return ;
}
POJ2230(打印欧拉回路)的更多相关文章
- Watchcow(POJ2230+双向欧拉回路+打印路径)
题目链接:http://poj.org/problem?id=2230 题目: 题意:给你m条路径,求一条路径使得从1出发最后回到1,并满足每条路径都恰好被沿着正反两个方向经过一次. 思路:由于可以回 ...
- UVa 10054 (打印欧拉回路) The Necklace
将每个颜色看成一个顶点,对于每个珠子在两个颜色之间连一条无向边,然后求欧拉回路. #include <cstdio> #include <cstring> + ; int G[ ...
- Uvaoj10054 - The Necklace
/* 题意:打印欧拉回路! 思路:开始时不明白,dfs为什么是后序遍历? 因为欧拉回路本身是一条回路,那么我们在dfs时,可能存在提前找到回路,这条回路可能不是欧拉回路, 因为没有遍历完成所有的边!如 ...
- poj2月题解
竟然生日前一天poj破百,不错不错,加速前进! poj2437 由于泥泞不重叠,所以按其实左边排个序再统计一遍即可(如果不是刚好盖满就尽量往后盖) poj2435 细节bfs poj2230 求欧拉回 ...
- UVA 10054 The Necklace(欧拉回路,打印路径)
题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- Uva 10054 欧拉回路 打印路径
看是否有欧拉回路 有的话打印路径 欧拉回路存在的条件: 如果是有向图的话 1.底图必须是连通图 2.最多有两个点的入度不等于出度 且一个点的入度=出度+1 一个点的入度=出度-1 如果是无向图的话 1 ...
- John's trip(POJ1041+欧拉回路+打印路径)
题目链接:http://poj.org/problem?id=1041 题目: 题意:给你n条街道,m个路口,每次输入以0 0结束,给你的u v t分别表示路口u和v由t这条街道连接,要输出从起点出发 ...
- poj2230 欧拉回路
http://poj.org/problem?id=2230 Description Bessie's been appointed the new watch-cow for the farm. E ...
- POJ2230 Watchcow【欧拉回路】
Watchcow Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6172Accepted: 2663 Special Judge ...
随机推荐
- maven创建web工程Spring配置文件找不到
使用maven创建web工程,将Spring配置文件applicationContext.xml放在src/resource下,用eclipse编译时提示class path resource [ap ...
- Codeforces Round #273 (Div. 2) A , B , C 水,数学,贪心
A. Initial Bet time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 关于sublime text 3 pylinter的错误提示
刚开始用windows下sublime text 3写python,搭建完以后,按ctrl+b可以build,然后保存时候一直提示. Fatal pylint error: x:/python: ca ...
- Thinkpad E440个性化设置:如何/禁用关闭触摸板?
#如何禁用/关闭触摸版 默认情况下,ThinkPad E440是不支持触摸板的关闭功能,如果要关闭的话,需要去官方下载相应的鼠标驱动 UltraNav. 下载地址:http://think.lenov ...
- js工厂方法
工厂方法与简单工厂的区别在于工厂方法没有switch条件分支,实例化哪一个子类放到了客户端(可以利用反射),这样整个工厂和产品体系都没有修改的变化,而只是扩展的变化,这就完全符合了开放-封闭原则的精神 ...
- SSIS的控制流之Foreach循环容器和序列容器
上一篇介绍了For循环容器的使用.本篇将介绍Foreach循环容器和序列容器的使用. Foreach循环容器 Foreach循环容器定义包中的控制流.其循环的实现类似于编程语言中的Foreach循环结 ...
- spring: @Pointcut给重复的注解/切点定义表达式
代码如下: package ch2.test; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.a ...
- python中利用正则表达式匹配ip地址
现在有一道题目,要求利用python中re模块来匹配ip地址,我们应如何着手? 首先能想到的是ip地址是数字,正则表达式是如何匹配数字的呢? \d或[0-9] 对于这个问题,不要一下子上来就写匹配模式 ...
- MySQL 基础数据类型优化(如何选择数据类型)
前言: 最近在看高性能 MySQL,记录写学习笔记: 高性能 MySQL 学习笔记(二) Schema与数据类型优化 笔记核心内容:MySQL 如何选择正确的数 ...
- Hbase 使用方法
列出所有 table¶ hbase(main):> list 新增 table¶ A . 直接增加一個表 t2 hbase(main):> create 't2' B . 增加一個擁有 ' ...