UVA302 John's trip(欧拉回路)
欧拉回路
attention:
- 如果有多组解,按字典序输出。
- 起点为每组数据所给的第一条边的编号较小的路口
- 每次输出完额外换一行
- 保证连通性
每次输入数据结束后,先用入度判断图是否满足回路的条件。
满足的话跑一遍dfs即可。
需要注意格式。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
template <typename T> inline T min(T &a,T &b) {return a<b ?a:b;}
template <typename T> inline T max(T &a,T &b) {return a>b ?a:b;}
int mxd,st,to[][],tot,ans[],in[];
bool vis[];
inline void dfs(int x){
for(int i=;i<=mxd;++i)
if(!vis[i]&&to[x][i]){
vis[i]=;
dfs(to[x][i]);
ans[++tot]=i;
}
}
int main(){
int u,v,w; bool ed=;
while(scanf("%d%d",&u,&v)){
if(!u&&!v){
if(ed) break;
bool ok=;
for(int i=;i<=;++i) if(in[i]&) {ok=; break;} //入度判断
if(ok){
memset(vis,,sizeof(vis));
dfs(st);
while(tot-) printf("%d ",ans[tot--]); //逆序输出
printf("%d\n",ans[tot--]);
}
else printf("Round trip does not exist.\n");
memset(in,,sizeof(in));
memset(to,,sizeof(to));
ed=; st=mxd=;
printf("\n"); //额外换行
continue;
}ed=;
scanf("%d",&w);
st= st ? st:min(u,v);
mxd=max(mxd,w);
to[u][w]=v; ++in[v];
to[v][w]=u; ++in[u];
}return ;
}
UVA302 John's trip(欧拉回路)的更多相关文章
- poj 1041 John's trip 欧拉回路
题目链接 求给出的图是否存在欧拉回路并输出路径, 从1这个点开始, 输出时按边的升序输出. 将每个点的边排序一下就可以. #include <iostream> #include < ...
- poj 1041 John's trip——欧拉回路字典序输出
题目:http://poj.org/problem?id=1041 明明是欧拉回路字典序输出的模板. 优先队列存边有毒.写跪.学习学习TJ发现只要按边权从大到小排序连边就能正常用邻接表了! 还有一种存 ...
- POJ1041 John's trip
John's trip Language:Default John's trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...
- Java实现John's trip(约翰的小汽车)
1 问题描述 John's trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8998 Accepted: 3018 Sp ...
- POJ 1041 John's trip 无向图的【欧拉回路】路径输出
欧拉回路第一题TVT 本题的一个小技巧在于: [建立一个存放点与边关系的邻接矩阵] 1.先判断是否存在欧拉路径 无向图: 欧拉回路:连通 + 所有定点的度为偶数 欧拉路径:连通 + 除源点和终点外都为 ...
- John's trip(POJ1041+欧拉回路+打印路径)
题目链接:http://poj.org/problem?id=1041 题目: 题意:给你n条街道,m个路口,每次输入以0 0结束,给你的u v t分别表示路口u和v由t这条街道连接,要输出从起点出发 ...
- poj1041 John's trip——字典序欧拉回路
题目:http://poj.org/problem?id=1041 求字典序欧拉回路: 首先,如果图是欧拉图,就一定存在欧拉回路,直接 dfs 即可,不用 return 判断什么的,否则TLE... ...
- POJ1041 John's trip 【字典序输出欧拉回路】
题目链接:http://poj.org/problem?id=1041 题目大意:给出一个连通图,判断是否存在欧拉回路,若存在输出一条字典序最小的路径. 我的想法: 1.一开始我是用结构体记录边的起点 ...
- 【poj1041】 John's trip
http://poj.org/problem?id=1041 (题目链接) 题意 给出一张无向图,求字典序最小欧拉回路. Solution 这鬼畜的输入是什么心态啊mdzz,这里用vector储存边, ...
随机推荐
- Redis+Keepalived实现高可用
使用redis哨兵可以在主服务器出现故障的时候自动切换主从,但是从服务器的IP不同于原主服务器的IP还需要在客户端手动修改IP才能生效 下面使用keepalived实现VIP自动漂移 keepaliv ...
- Python:正则表达式概念
#正则表达式内容非常多,网上的学习资源也是目不暇接,我从中筛选学习并且整理出以下 的学习笔记 一.正则表达式匹配过程: 1.依次拿出表达式和文本中的字符比较 2.如果每一个字符都能匹配,则匹配成功:一 ...
- iOS安装包重签笔记
https://blog.csdn.net/skylin19840101/article/details/60583893
- https://validator.w3.org
https://validator.w3.org/nu/?doc=http%3A%2F%2Fdev.mysql.com%2Fdoc%2Frefman%2F5.7%2Fen%2Fmanual-info. ...
- are not called implicitly
php.net <?php class BaseClass{ function __construct() { print "In BaseClass constructor<b ...
- Spring Boot中的自定义start pom
start pom是springboot中提供的简化企业级开发绝大多数场景的一个工具,利用好strat pom就可以消除相关技术的配置得到自动配置好的Bean. 举个例子,在一般使用中,我们使用基本的 ...
- 【Python虫师】多窗口定位
<注意>iframe框架 iframe也称作嵌入式框架,嵌入式框架和框架网页类似,它可以把一个网页的框架和内容嵌入在现有的网页中. 框架(framework)是一个基本概念上的结构,用于去 ...
- cookie和session的使用
http://www.cnblogs.com/linguoguo/p/5106618.html 1:在控制器中添加session和cookie @RequestMapping("/getin ...
- python接口测试中安装whl格式的requests第三方模块
下载 安装 requests第三方模块 下载:http://docs.python-requests.org/en/latest/user/install/#install 我下载是 https:// ...
- python3专业版安装及破解
1.网址 https://www.jetbrains.com/pycharm/download/#section=windows,打开页面,点击下载专业版 2.这是下载好的文件,双击运行即可. //详 ...