John's trip
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8641   Accepted: 2893   Special Judge

Description

Little Johnny has got a new car. He decided to drive around the town to visit his friends. Johnny wanted to visit all his friends, but there was many of them. In each street he had one friend. He started thinking how to make his trip as short as possible. Very soon he realized that the best way to do it was to travel through each street of town only once. Naturally, he wanted to finish his trip at the same place he started, at his parents' house.

The streets in Johnny's town were named by integer numbers from 1 to
n, n < 1995. The junctions were independently named by integer
numbers from 1 to m, m <= 44. No junction connects more than 44
streets. All junctions in the town had different numbers. Each street
was connecting exactly two junctions. No two streets in the town had the
same number. He immediately started to plan his round trip. If there
was more than one such round trip, he would have chosen the one which,
when written down as a sequence of street numbers is lexicographically
the smallest. But Johnny was not able to find even one such round trip.

Help Johnny and write a program which finds the desired shortest
round trip. If the round trip does not exist the program should write a
message. Assume that Johnny lives at the junction ending the street
appears first in the input with smaller number. All streets in the town
are two way. There exists a way from each street to another street in
the town. The streets in the town are very narrow and there is no
possibility to turn back the car once he is in the street

Input

Input
file consists of several blocks. Each block describes one town. Each
line in the block contains three integers x; y; z, where x > 0 and y
> 0 are the numbers of junctions which are connected by the street
number z. The end of the block is marked by the line containing x = y =
0. At the end of the input file there is an empty block, x = y = 0.

Output

Output
one line of each block contains the sequence of street numbers (single
members of the sequence are separated by space) describing Johnny's
round trip. If the round trip cannot be found the corresponding output
block contains the message "Round trip does not exist."

Sample Input

1 2 1
2 3 2
3 1 6
1 2 5
2 3 3
3 1 4
0 0
1 2 1
2 3 2
1 3 3
2 4 4
0 0
0 0

Sample Output

1 2 3 5 4 6
Round trip does not exist. 题意:小明要从自己家走遍每一条街道有且仅只有一次访问每一个朋友然后回到自己家,这些路径都有一个权值,按照这些路径的字典序输出走法. 题解:很明显的欧拉回路,首先判断是否为欧拉回路,即每个点的度都是偶数,如果满足欧拉回路,先对每个结点所连接的边按照边的编号排序,这里利用 vector 中的 pair是最好不过了.然后进行一次深搜就行了.
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
using namespace std;
const int N = ;
vector < pair<int,int> > edge[N];
bool vis[N]; ///标记哪些边被访问过了
int in[N];
int out[N],ans[N],cnt;
void init()
{
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(vis,false,sizeof(vis));
for(int i=;i<N;i++) edge[i].clear();
cnt = ;
}
void addEdge(int u,int v,int w){
edge[u].push_back(make_pair(w,v));
edge[v].push_back(make_pair(w,u));
}
void dfs(int u){
for(int i=;i<edge[u].size();i++){
int e = edge[u][i].first;
int v = edge[u][i].second;
if(!vis[e]){
vis[e] = true;
dfs(v);
ans[cnt++] = e;
}
}
}
bool cmp(pair<int,int> a,pair<int,int> b){
return a.first<b.first;
}
int main()
{
int x,y,z,m;
while(scanf("%d%d",&x,&y)!=EOF)
{
int MIN = N,m=;
if(x==&&y==) break;
scanf("%d",&z);
init();
in[x]++,out[x]++;
in[y]++,out[y]++;
addEdge(x,y,z);
MIN = min(MIN,min(x,y));
while(scanf("%d%d",&x,&y)!=EOF)
{
if(x==&&y==) break;
scanf("%d",&z);
in[x]++,out[x]++;
in[y]++,out[y]++;
addEdge(x,y,z);
MIN = min(MIN,min(x,y));
}
bool flag = false;
for(int i=;i<N;i++){
if(in[i]%==||out[i]%==){
flag = true;
break;
}
if(edge[i].size()) sort(edge[i].begin(),edge[i].end(),cmp);
}
if(flag) {
printf("Round trip does not exist.\n");
continue;
}
dfs(MIN);
for(int i=cnt-;i>=;i--){
printf("%d ",ans[i]);
}
printf("\n");
}
}
												

poj 1041(字典序输出欧拉回路)的更多相关文章

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

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

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

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

  3. poj 2337 有向图输出欧拉路径

    Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10186   Accepted: 2650 Descrip ...

  4. ZOJ 3204 Connect them(字典序输出)

    主要就是将最小生成树的边按字典序输出. 读取数据时,把较小的端点赋给u,较大的端点号赋值给v. 这里要用两次排序,写两个比较器: 第一次是将所有边从小到大排序,边权相同时按u从小到大,u相同时按v从小 ...

  5. 二叉排序树:HUD3999-The order of a Tree(二叉排序树字典序输出)

    The order of a Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  6. UVA 796 - Critical Links 无向图字典序输出桥

    题目:传送门 题意:给你一个无向图,你需要找出里面的桥,并把所有桥按字典序输出 这一道题就是用无向图求桥的模板就可以了. 我一直错就是因为我在输入路径的时候少考虑一点 错误代码+原因: 1 #incl ...

  7. 拓扑排序详解(梅开二度之dfs版按字典序输出拓扑路径+dfs版输出全部拓扑路径

    什么是拓扑排序? 先穿袜子再穿鞋,先当孙子再当爷.这就是拓扑排序! 拓扑排序说白了其实不太算是一种排序算法,但又像是一种排序(我是不是说了个废话qwq) 他其实是一个有向无环图(DAG, Direct ...

  8. poj 1041(欧拉回路+输出字典序最小路径)

    题目链接:http://poj.org/problem?id=1041 思路:懒得写了,直接copy吧:对于一个图可以从一个顶点沿着边走下去,每个边只走一次,所有的边都经过后回到原点的路.一个无向图存 ...

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

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

随机推荐

  1. bzoj2089&2090: [Poi2010]Monotonicity

    双倍经验一眼题... f[i][1/2]表示以i结尾,当前符号应该是</>的最长上升子序列, 用BIT优化转移就好 =的话就不用说了吧= = #include<iostream> ...

  2. [CodeVs1050]棋盘染色2(状态压缩DP)

    题目大意:有一个5*N(≤100)的棋盘,棋盘中的一些格子已经被染成了黑色,求最少对多少格子染色,所有的黑色能连成一块. 这题卡了我1h,写了2.6k的代码,清明作业一坨还没做啊...之前一直以为这题 ...

  3. mysql数据库----视图、触发器、存储过程、函数、事务、索引、其他语句

    一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. SELECT * FROM ( S ...

  4. Struts2-从值栈中获取数据-EL表达式从值栈获取

    从值栈获取数据 1 使用struts2的标签+ognl表达式获取值栈数据 (1)<s:property value=”ognl表达式”/> 获取字符串

  5. zabbix 监控 Esxi

    一.下载模板 http://www.zabbix.org/wiki/Zabbix_Templates 我这边ESXI 版本是6.0的,但是模板只有5,也可以用 二.添加模板 配置 ---> 模板 ...

  6. Lucene 索引与检索架构图

  7. CronTrigger 表达式 (调度配置)

    CronTrigger 的用途更广,相比基于特定时间间隔进行调度安排的 SimpleTrigger,CronTrigger 主要适用于基于日历的调度安排.例如:每星期二的 16:38:10 执行,每月 ...

  8. uva 1639 Candy (对数处理精度)

    https://vjudge.net/problem/UVA-1639 有两个盒子各有n(n≤2*10 5 )个糖,每天随机选一个(概率分别为p,1-p),然后吃一颗糖. 直到有一天,打开盒子一看,没 ...

  9. 算法专题-STL篇

    这篇文章着重记录c++中STL的用法.主要粗略的介绍其用法,以知识点的形式呈现其功能,不会深入源码分析其工作原理. 排序和检索. sort(a,a+n),对a[0]往后的n个元素(包括a[0])进行排 ...

  10. JobEngine 基于quartz.net 跨平台作业框架

    github:https://github.com/zzhi/JobEngine 基于quartz.net 的跨平台作业框架 quartz.net(https://github.com/quartzn ...