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. ssh后台执行

    1 执行scp命令,开始传输2 scp传输开始后,用ctrl+z,再以bg命令将其转入后台运行. 3以exit命令安全退出即可令scp继续执行,不受关闭shell的影响.

  2. acid(数据库事务正确执行的四个基本要素的缩写)

    ACID,指数据库事务正确执行的四个基本要素的缩写.包含:原子性(Atomicity).一致性(Consistency).隔离性(Isolation).持久性(Durability).一个支持事务(T ...

  3. bzoj3173: [Tjoi2013]最长上升子序列(fhqtreap)

    这题用fhqtreap可以在线. fhqtreap上维护以i结尾的最长上升子序列,数字按从小到大加入, 因为前面的数与新加入的数无关, 后面的数比新加入的数小, 所以新加入的数对原序列其他数的值没有影 ...

  4. hdu6166 Senior Pan

    Senior Pan Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  5. Python与R的争锋:大数据初学者该怎样选?

    在当下,人工智能的浪潮席卷而来.从AlphaGo.无人驾驶技术.人脸识别.语音对话,到商城推荐系统,金融业的风控,量化运营.用户洞察.企业征信.智能投顾等,人工智能的应用广泛渗透到各行各业,也让数据科 ...

  6. webstorm 激活破解方法大全

    webstorm 作为最近最火的前端开发工具,也确实对得起那个价格,但是秉着勤俭节约的传统美德,我们肯定是能省则省啊. 方法一:(更新时间:2018/1/23)v3.3 注册时,在打开的License ...

  7. phpstorm改变文件编码由utf变为gbk

    PhpStorm是一个轻量级且便捷的PHP IDE,其旨在提供用户效率,可深刻理解用户的编码,提供智能代码补全,快速导航以及即时错误检查. 由于PHPStorm编辑器默认是UTF-8编码 如果开发的是 ...

  8. 为VSCODE添加右键菜单

    参考:https://blog.csdn.net/GreekMrzzJ/article/details/82194913 1.创建一个名为vscode.reg的空文本文件,填入下列内容 Windows ...

  9. Codeforces 221 A. Little Elephant and Function

    A. Little Elephant and Function time limit per test 2 seconds memory limit per test 256 megabytes in ...

  10. 数学&搜索:博弈论之极大极小搜索与alpha-beta减枝

    目的是寻找最优的方案使得自己能够利益最大化. 基本思想就是假设自己(A)足够聪明,总是能选择最有利于自己的方案,而对手(B)同样足够聪明,总会选择最不利A的方案 对抗搜索就是对于先手来说,取后手中状态 ...