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 ...
随机推荐
- iOS应用网络安全之HTTPS
移动互联网开发中iOS应用的网络安全问题往往被大部分开发者忽略,iOS9和OS X 10.11开始Apple也默认提高了安全配置和要求.本文以iOS平台App开发中对后台数据接口的安全通信进行解析和加 ...
- strspn() 和 strcspn() 函数【转】
本文转载自:https://flyer103.wordpress.com/2011/06/03/strspn-%E5%92%8C-strcspn-%E5%87%BD%E6%95%B0/ 前几天在看一本 ...
- Zabbix3.2 客户端安装
查看客户端环境: # cat /etc/redhat-release CentOS Linux release (Core) # uname -r -.el7.x86_64 Centos7 客户端: ...
- 使用iframe,注销以后,点击某个标签,跳转到的登录页面位于标签中
当使用iframe时,要么会话过期,要么手动注销,如果此时再进行操作,可能遇到跳转到登录页面,可是登录页面会位于标签执行,并没有全屏显示,即位于当前的iframe中,此时的解决方式很简单,只需要在登录 ...
- java入门了解04
1.异常的体系:---------|Throwable --------------| Error (错误) 错误一般是由于jvm或者是硬件引发的问题,所以我们一般都不会通过代码去处理.------- ...
- 考勤助手ER图2.0版本所存在的问题
如图所示,考勤助手ER图2.0版本中,缺少个人信箱这一问题虽然不具有逻辑层面的问题,但是它是不满足需求的,也就是说这样的设计无法满足小组对考勤助手最初的设计.其次,就设计层面来分析,弱联系是数据库查询 ...
- nodejs mysql 操作数据库方法二
node.js 开发指南 – Node.js 连接 MySQL 并进行数据库操作 通常在NodeJS开发中我们经常涉及到操作数据库,尤其是 MySQL ,作为应用最为广泛的开源数据库则成为我们的首选, ...
- 发布新版本遇见java.lang.ClassNotFoundException
今天发布新版本到测试环境,服务器在启动时报了java.lang.ClassNotFoundException .刚开始我以为是代码中jar引的不对从而导致找不到相关类,后来在本地试了下发现项目可以正常 ...
- mogon操作数据库
返回的本来就是promise redis是内存数据库,更适合放session等一些东西.而mongo不是.
- phalcon查询:单条查询,多条查询,多表查询
单条查询, $order = \OrderMain::findFirst("oid='" . $oid . "'"); 多条查询, $shop = \Order ...