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. hdu4027Can you answer these queries?【线段树】

    A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use ...

  2. ubuntu16.04下安装artoolkit5

    目前对AR技术的常见理解就是CV(Computer Vision)+CG(Computer Graphic).CV的方法很多,简单些比如FREAK+ICP(ARToolKit中的NFT),复杂些就是S ...

  3. javascript 字符串去空格

    1.正则去空格 a.去掉字符串中所有空格 " hello world ".replace(/\s+/g,"");//helloworld b.去掉字符串左边空格 ...

  4. linux UVC and hardware viewer

    至于从哪个版本开始内核支持UVC,官方的话是“Linux 2.6.26 and newer includes the Linux UVC driver natively.” 1.查看摄像头ID: [r ...

  5. Databases: MySQL tRIGger--chinese character-set php

    DELIMITER |create TRIGGER tr_calllog_insert after insert on messagescalllog for each row beginIF mes ...

  6. Find a way--hdu2612

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2612 广搜题 注意:可能存在一个@两人都不能达到: 3 3 Y#@ .M# @.. #include ...

  7. 在django项目中自定义manage命令(转)

    add by zhj 是我增加的注释 原文:http://www.cnblogs.com/holbrook/archive/2012/03/09/2387679.html 我们都用过Django的dj ...

  8. Library Publication 时遇到 "more than one library with package name" 错误的解决方法

    Library Publication 是 Gradle 在0.9.0 时增加的一个新特性,它的作用是让Lib也能发布不同的版本 在这之前,Lib只能发布release版本,你的项目中依赖的所有Lib ...

  9. pycharm中python模板代码自动生成

    # -*- coding: utf-8 -*- """ ------------------------------------------------- File Na ...

  10. git快速入门 push/clone/reset/merge/切换分支全都有

    本文介绍git快速入门,从安装/创建init / 发布push/版本回退reset / branch分支切换/合并分支merge 这些基本的操作都有涉及,方便新人快速入手,有需要的朋友mark一下.首 ...