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. 题目大意:john要去拜访他的朋友,要经过每一条街道,问是否存在这样的路径 题解 :要经过每一条边,就是欧拉回路,欧拉回路存在的充要条件是:无向图中节点的度为偶数,有向图中节点的入度等于出度 代码是仿照别人写的,以前没碰到过这种题
/*poj1041 第一次看到还以为是flord最小环问题,结果发现是欧拉回路上网看了一下大佬们的思路
然后自己在写一遍代码,加深一下印象
欧拉回路:每一条边都要经过,充要条件是:无向图的节点度为偶数个
有向图的节点入度等于出度*/
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
int street_cnt;//街道的个数
int street[][];//街道链接的两端
int map[][];//map[i][j]表示从i点经过j点到达的路径
bool visited[];//判断有没有访问过
int degree[];//存储节点的入度
int Stack[];//用一个栈来存放路径
int stack_top;//来表示栈头元素
int home;
void record(int x,int y,int z)
{
street[z][]=x;//街道两端链接的家
street[z][]=y;
map[x][z]=y;//通过这条街道可以到达的朋友家
map[y][z]=x;
++degree[x];//然后节点度数要增加
++degree[y];
}
void DFS(int j)
{
for(int i=;i<=street_cnt;i++)
{
if(!visited[i]&&map[j][i])
{
visited[i]=true;
DFS(map[j][i]);//到了当前的朋友家然后开始下一个朋友
Stack[stack_top++]=i;
}
}
}
int main() {
while (true) {
int x, y, z;
scanf("%d%d", &x, &y);
if (x == && y == ) {
break;
}
scanf("%d", &z);
memset(map, false, sizeof(map));
memset(degree, , sizeof(degree));
record(x, y, z);
home = x < y ? x : y;
street_cnt = ;
while (true) {
scanf("%d%d", &x, &y);
if (x == && y == ) {
break;
}
scanf("%d", &z);
record(x, y, z);
++street_cnt;
}
bool flag = true;
//欧拉回路存在的充要条件是每个顶点的度数都为偶数
for (int i = ; i <= ; ++i) {
if (degree[i] % != ) {
flag = false;
break;
}
}
if (flag == false) {
printf("Round trip does not exist.\n");
continue;
}
memset(visited, false, sizeof(visited));
stack_top = ;
DFS(home);
for (int i = stack_top - ; i > ; --i) {
printf("%d ", Stack[i]);
}
printf("%d\n", Stack[]);
}
return ;
}

最新文章

  1. oracle 修改字符集支持中文
  2. 现代软件工程作业 第二章 学习github笔记
  3. python之import机制
  4. mysqldumpslow使用说明
  5. initialSize,maxTotal,maxIdle,minIdle,maxWaitMillis
  6. C++ cout 如何保留小数输出
  7. 引用KBC.PetroSIM.Interop的dll,在代码中调用时出现 80040154 没有注册类 的错误
  8. luogu P5303 [GXOI/GZOI2019]逼死强迫症
  9. 爬虫基础--IO多路复用单线程异步非阻塞
  10. C# 枚举转列表
  11. Problem UVA11134-Fabled Rooks(贪心)
  12. 大项目小细节---onbeforeunload增强用户体验
  13. LeetCode Rotatelmage
  14. location匹配
  15. Mysql 创建数据库命令
  16. 4. Tomcat内存溢出解决
  17. 如何更改Oracle字符集避免乱码
  18. php 数据集转换树、递归重组节点信息多维数组(转)
  19. Swift - 用CATransform3DMakeRotation实现翻页效果
  20. Lombok简化Java代码的好工具

热门文章

  1. Bear and Tower of Cubes Codeforces - 680D
  2. 洛谷 P1593 因子和 || Sumdiv POJ - 1845
  3. vi/vim 中批量在行插入或删除指定字符
  4. 在CentOS 6.7 64位安装PHP的PDO_OCI扩展 Installing PDO_OCI extension on CentOS 6.7 64bit
  5. CF1072C Cram Time
  6. 利用nodejs读取数据库数据生成树结构的json数据
  7. [windows]命令行关机或重启电脑
  8. vue.js与react.js相比较的优势
  9. 关闭windows7/8的自动升级到windows10
  10. swift 接水果游戏ios源码