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 ;
}

Poj 1041--欧拉回路的更多相关文章

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

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

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

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

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

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

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

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

  5. poj 1041(字典序输出欧拉回路)

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

  6. POJ 1041 John&#39;s trip Euler欧拉回路判定和求回路

    就是欧拉判定,判定之后就能够使用DFS求欧拉回路了.图论内容. 这里使用邻接矩阵会快非常多速度. 这类题目都是十分困难的.光是定义的记录的数组变量就会是一大堆. #include <cstdio ...

  7. [POJ 1041] John's Trip

    [题目链接] http://poj.org/problem?id=1041 [算法] 欧拉回路[代码] #include <algorithm> #include <bitset&g ...

  8. poj 1780 , poj 1392 欧拉回路求前后相互衔接的数字串

    两道题目意思差不多 第一题是10进制 , 第二题是2进制的 都是利用欧拉回路的fleury算法来解决 因为我总是希望小的排在前面,所以我总是先将较小数加入栈,再利用另一个数组接收答案,但是这里再从栈中 ...

  9. poj 2513 欧拉回路+并查集推断是否联通+Trie树

    http://poj.org/problem? id=2513 最初看到 第一感觉---map  一看250000的数据量 果断放弃 然后记得曾经看过.trie取代map.尤其当数据量特别大的时候 学 ...

  10. poj 2337 欧拉回路输出最小字典序路径 ***

    把26个小写字母当成点,每个单词就是一条边. 然后就是求欧拉路径. #include<cstdio> #include<iostream> #include<algori ...

随机推荐

  1. 运用session来控制用户的异地登录被挤下线情况

    在用QQ的过程中我们如果你的账号在另外一台手机上面登录,这是腾讯后台会提醒你异地登录,可能你的账号被盗了,然后你手机上得QQ就会被退出登录,这个时候你就需要重新登录修改密码,以确保账号的安全.那这种被 ...

  2. C#项目中的bin目录和obj目录的区别

    C#项目中的bin目录和obj目录的区别 1.关于bin目录和obj目录 Bin目录用来保存项目生成后程序集,它有Debug和Release两个版本,分别对应的文件夹为bin/Debug和bin/Re ...

  3. SpringMVC-高级参数绑定

    绑定数组 需求 在商品列表页面选中多个商品,然后删除. 需求分析 此功能要求商品列表页面中的每个商品前有一个checkbook,选中多个商品后点击删除按钮把商品id传递给Controller,根据商品 ...

  4. python入门之数据类型之列表、元组、字典

    list 格式: test_list = ["a",123,[1,"b"]] 索引: >>>print(test_list[0]) " ...

  5. axios 在vue中使用

    下载组件: npm install axios --save npm install qs --save //处理对象防止产生跨域问题 引入: 新建axios文件夹,文件下新建index.js文件 i ...

  6. arcengine geometry union操作

    以前得到的结果老是某一个,用下面的方法就可以获取合并后的结果 IGeometry pUnionGeo = null; var bFirst = true; foreach (IGeometry pGe ...

  7. 開玩樹莓派(二):配置IP,實現無顯示器局域網內Putty連接和RDP遠程

    目錄: 開玩樹莓派(一):安裝Raspbian系統 開玩樹莓派(二):配置IP,實現無顯示器局域網內Putty連接和RDP遠程 開玩樹莓派(三):Python編程 開玩樹莓派(四):GPIO控制和遠程 ...

  8. SQL Server插入中文数据出现乱码问题

    我在用sql server存储数据的时候发现中文全变成了问号,我知道中文是特殊的编码.所以在数据库设计的时候包含中文的字段就是nvarchar,但是还是成了问号 好了,不多说了,解决方案如下: 在存储 ...

  9. Android 实现类似于QQ空间相册的点击图片放大,再点后缩小回原来位置

    前几天看到了有人在android5.0上实现了如下图一样的效果,我自己就去搜了下.参考了国外一篇文章和国内的一篇文章,最终实现了想要的效果.具体参考的网址我已经贴到文章末尾,大家可以去英文的那个网站看 ...

  10. (五)mybatis之下载与基本构成

    1.  下载MyBatis. 输入网址:https://github.com/mybatis/mybatis-3/releases 进入Mybatis下载页面,选择第一个选项,然后就可以加载到myba ...