UVA302 John's trip

欧拉回路

attention:

  1. 如果有多组解,按字典序输出。
  2. 起点为每组数据所给的第一条边的编号较小的路口
  3. 每次输出完额外换一行
  4. 保证连通性

每次输入数据结束后,先用入度判断图是否满足回路的条件。

满足的话跑一遍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(欧拉回路)的更多相关文章

  1. poj 1041 John's trip 欧拉回路

    题目链接 求给出的图是否存在欧拉回路并输出路径, 从1这个点开始, 输出时按边的升序输出. 将每个点的边排序一下就可以. #include <iostream> #include < ...

  2. poj 1041 John's trip——欧拉回路字典序输出

    题目:http://poj.org/problem?id=1041 明明是欧拉回路字典序输出的模板. 优先队列存边有毒.写跪.学习学习TJ发现只要按边权从大到小排序连边就能正常用邻接表了! 还有一种存 ...

  3. POJ1041 John's trip

    John's trip Language:Default John's trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...

  4. Java实现John's trip(约翰的小汽车)

    1 问题描述 John's trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8998 Accepted: 3018 Sp ...

  5. POJ 1041 John's trip 无向图的【欧拉回路】路径输出

    欧拉回路第一题TVT 本题的一个小技巧在于: [建立一个存放点与边关系的邻接矩阵] 1.先判断是否存在欧拉路径 无向图: 欧拉回路:连通 + 所有定点的度为偶数 欧拉路径:连通 + 除源点和终点外都为 ...

  6. John's trip(POJ1041+欧拉回路+打印路径)

    题目链接:http://poj.org/problem?id=1041 题目: 题意:给你n条街道,m个路口,每次输入以0 0结束,给你的u v t分别表示路口u和v由t这条街道连接,要输出从起点出发 ...

  7. poj1041 John's trip——字典序欧拉回路

    题目:http://poj.org/problem?id=1041 求字典序欧拉回路: 首先,如果图是欧拉图,就一定存在欧拉回路,直接 dfs 即可,不用 return 判断什么的,否则TLE... ...

  8. POJ1041 John's trip 【字典序输出欧拉回路】

    题目链接:http://poj.org/problem?id=1041 题目大意:给出一个连通图,判断是否存在欧拉回路,若存在输出一条字典序最小的路径. 我的想法: 1.一开始我是用结构体记录边的起点 ...

  9. 【poj1041】 John's trip

    http://poj.org/problem?id=1041 (题目链接) 题意 给出一张无向图,求字典序最小欧拉回路. Solution 这鬼畜的输入是什么心态啊mdzz,这里用vector储存边, ...

随机推荐

  1. PAT甲1101 Quick Sort

    1101 Quick Sort (25 分) There is a classical process named partition in the famous quick sort algorit ...

  2. poj2406 Power Strings 【KMP】

    Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc&quo ...

  3. python 结巴分词(jieba)详解

    文章转载:http://blog.csdn.net/xiaoxiangzi222/article/details/53483931 jieba “结巴”中文分词:做最好的 Python 中文分词组件 ...

  4. CodeForces - 779D String Game 常规二分

    题意:给你两个串,S2是S1 的一个子串(可以不连续).给你一个s1字符下标的一个排列,按照这个数列删数,问你最多删到第几个时S2仍是S1 的一个子串. 题解:二分删掉的数.判定函数很好写和单调性也可 ...

  5. hadoop的两类配置文件及3种启动/关闭方式

    hadoop配置文件 默认配置文件:四个模块相对应的jar包中:$HADOOP_HOME/share/hadoop        *core-default.xml        *hdfs-defa ...

  6. [INS-20802] Oracle Net Configguration Assistant faild

    Redhat/Centos 安装oracle11gR2时出现以下错误: [INS-20802] Oracle Net Configuration Assistant failed 查看对应日志文件,信 ...

  7. 前端开发组件化设计vue,react,angular原则漫谈

    前端开发组件化设计vue,react,angular原则漫谈 https://www.toutiao.com/a6346443500179505410/?tt_from=weixin&utm_ ...

  8. noip做题记录+挑战一句话题解?

    因为灵巧实在太弱辽不得不做点noip续下命QQAQQQ 2018 积木大赛/铺设道路 傻逼原题? 然后傻逼的我居然检查了半天是不是有陷阱最后花了差不多一个小时才做掉我做过的原题...真的傻逼了我:( ...

  9. 你可能用得到的9段CSS代码

    一.opacity兼容 .transparent {    filter: alpha(opacity=50);/* internet explorer */    -khtml-opacity: 0 ...

  10. python中操作mysql

    import pymysql # 连接数据库 connect = pymysql.Connect( host='localhost', port=3306, user='root', passwd=' ...