poj2230 欧拉回路
http://poj.org/problem?id=2230
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
Hint
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 欧拉回路的更多相关文章
- 0x66 Tarjan算法与无向图联通性
bzoj1123: [POI2008]BLO poj3694 先e-DCC缩点,此时图就变成了树,树上每一条边都是桥.对于添加边的操作,相当于和树上一条路径构环,导致该路径上所有边都不成为桥.那么找这 ...
- Watchcow(POJ2230+双向欧拉回路+打印路径)
题目链接:http://poj.org/problem?id=2230 题目: 题意:给你m条路径,求一条路径使得从1出发最后回到1,并满足每条路径都恰好被沿着正反两个方向经过一次. 思路:由于可以回 ...
- POJ2230 Watchcow【欧拉回路】
Watchcow Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6172Accepted: 2663 Special Judge ...
- poj2230 Watchcow【欧拉回路】【输出路径】(遍历所有边的两个方向)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4392 题目大意: 一个图,要将每条边恰好遍历两遍,而且要以不同的方向,还要回到原点. dfs解法 ...
- POJ2230(打印欧拉回路)
Watchcow Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 7473 Accepted: 3270 Specia ...
- POJ2230Watchcow[欧拉回路]
Watchcow Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 7512 Accepted: 3290 Specia ...
- ACM/ICPC 之 混合图的欧拉回路判定-网络流(POJ1637)
//网络流判定混合图欧拉回路 //通过网络流使得各点的出入度相同则possible,否则impossible //残留网络的权值为可改变方向的次数,即n个双向边则有n次 //Time:157Ms Me ...
- [poj2337]求字典序最小欧拉回路
注意:找出一条欧拉回路,与判定这个图能不能一笔联通...是不同的概念 c++奇怪的编译规则...生不如死啊... string怎么用啊...cincout来救? 可以直接.length()我也是长见识 ...
- ACM: FZU 2112 Tickets - 欧拉回路 - 并查集
FZU 2112 Tickets Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u P ...
随机推荐
- Scrum _GoodJob
作为长大的大三老腊肉,我们已经在长大生活了两年多,对于什么是长大人最想完善的校园需求.最想拥有的校园服务媒介也有了更加深切的体会. 于是,GoodJob小团队blingbling闪现啦!! GoodJ ...
- MySQL学习(二)数据类型
截取书中内容留作学习.... 1.整数类型 2.浮点数与定点数类型 3.日期时间类型 向数据库中插入当前系统时间:CURRENT_TIME或者NOW() 4.文本字符串类型 MySQL枚举类型:cre ...
- 开始学习git
今天看着廖雪峰的git使用教程开始学习git.不过没有将项目托管在github上,而是选择托管在了码云上. 看着明白做起来还是出了些问题,不过好在最后都解决了.果然眼高手低要不得. 试着将自己平时学习 ...
- jquery.dataTables列中内容居中问题?求解?
.table > tbody > tr > td { vertical-align: middle; }
- 20165322 预备作业3 Linux安装及学习
Linux安装及学习 安装部分 由于是第一次接触虚拟机知识,之前也没什么了解,我选择完全按照老师教程里的安装vbox虚拟机. 虚拟机安装的过程很顺利,不做详细讲解. 出现的问题 在启动我新建的虚拟电脑 ...
- HDU 1575 Tr A 【矩阵经典2 矩阵快速幂入门】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1575 Tr A Time Limit: 1000/1000 MS (Java/Others) Me ...
- [19/03/24-星期日] 容器_Collection(集合、容器)之List(表,有顺序可重复)
一. 概念&方法 Collection 表示一组对象,它是集中.收集的意思.Collection接口的两个子接口是List.Set接口. 由于List.Set是Collection的子接口,意 ...
- [18/11/26] this关键字、static关键字和静态块(及语句块)
1.this关键字 this的本质就是“创建好的对象的地址”! 由于在构造方法调用前,对象已经创建.因此,在构造方法中也可以使用this代表“当前对象” [用法] 1. 在程序中产生二义性之处 ...
- 2018.11.29 Struts2中拦截器的学习&项目的实际运用
struts2官方架构 拦截器的创建 第一种方式 第二种方式 看源码AbstractInterceptor 底层已经帮我们写过这些方法了 第三种方式(推荐) 拦截器API学习 放行 前后处理 不放行, ...
- eclipse properties 文件查看和编辑插件
*.properties属性文件,如果文件中包含中文,会出现乱码.为了解决这个问题,可以为Eclipse安装Properties Editor插件解决这个问题. 步骤 1 安装Properties ...