POJ 1041 John's trip 详细解释欧拉回路判定概念+逆序输出路径+【模板套用】
发布日期:2021-06-29 14:37:32 浏览次数:2 分类:技术文章

本文共 4224 字,大约阅读时间需要 14 分钟。

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.

分析

在这里插入图片描述

欧拉回路:对于一个图可以从一个顶点沿着边走下去,每个边只走一次,所有的边都经过后回到原点的路。(一笔画问题)。

欧拉回路的判定

判断欧拉路是否存在的方法

有向图:图连通,有一个顶点出度大入度1,有一个顶点入度大出度1,其余都是出度=入度。

无向图:图连通,只有两个顶点是奇数度,其余都是偶数度的。

判断欧拉回路是否存在的方法

有向图:图连通,所有的顶点出度=入度。

无向图:图连通,所有顶点都是偶数度

这道题其实并不用判连通,因为常识告诉我们不会有一条路是封闭着的,所以我们只用判断每个定点的度数即可,由于本题是有向图,如果出现一个顶点的度数是偶数度,那就不会存在欧拉回路了

另外,选择好的数据结构很重要。

题目要求我们按照字典序最小的顺序来输出,我们考虑用栈的思想来存我们的路径至res数组中,最后用dfs找欧拉回路,然后逆序打印即可

最后,此题的输入也是有点深奥的,我一开始没看懂,那个z居然是下标的意思,然后采用do…while…配合输入这种方式。

AC

#include
#include
#include
#define mst(a) memset(a,0,sizeof(a))using namespace std;const int maxn=20000+8;int angleNum[maxn]; //表示顶点度数int used[maxn]; //判断顶点是否走过int n,k;int res[maxn]; //路径数组int cnt=0;struct node{
//结构体定义 起点和终点 int a; int b;}rng[maxn];bool is_OK() //此方法用于判定每个顶点的度数{
for(int i=1;i<=n;i++) if(angleNum[i]%2) //如果存在一个顶点度数为偶数度,那么就不存在欧拉回路 return false; //仅仅对于有向图而言 return true;}void dfs(int x){
for(int i=1;i<=k;i++) {
if(!used[i]&&(rng[i].a==x||rng[i].b==x)) //这里处理有点特殊,我们不确定 {
//走的是每个分支的起点还是终点 used[i]=1; dfs(rng[i].a+rng[i].b-x); res[++cnt]=i; } }}int main(){
ios::sync_with_stdio(false); //cin提速 cin.tie(0); int x,y,z; while(cin>>x>>y&&(x+y)) {
int point=min(x,y); //求出我们的源点 n=max(x,y); mst(angleNum); mst(used); cnt=k=0; do{
cin>>z; rng[z].a=x; rng[z].b=y; ++k; angleNum[x]++; angleNum[y]++; n=max(n,max(x,y)); //求出最大的顶点数 }while(cin>>x>>y&&(x+y)); if(!is_OK()) {
cout<<"Round trip does not exist."<
=1;i--) //逆序输出路径 {
if(i!=1) cout<
<<" "; else cout<
<

学如逆水行舟,不进则退

转载地址:https://chocolate.blog.csdn.net/article/details/97623783 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:HDU 2681 MM Programming Club(miaos的线段树维护+ycy的暴力贪心)
下一篇:POJ 1040 Transportation (DFS+剪枝+回溯)

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月13日 14时40分25秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

Python抓取哔哩哔哩up主信息:只要爬虫学的好,牢饭吃的早 2019-04-29
有个码龄5年的程序员跟我说:“他连wifi从来不用密码” 2019-04-29
领导让我整理上个季度的销售额,幸好我会Python数据分析,你猜我几点下班 2019-04-29
【Python爬虫实战】为何如此痴迷Python?还不是因为爱看小姐姐图 2019-04-29
零基础自学Python,你也可以实现经济独立! 2019-04-29
ElasticSearch与Mysql对比(ElasticSearch常用方法大全,持续更新) 2019-04-29
数字化转型的主干道上,华为云以“三大关键”成企业智能化推手 2019-04-29
数字化为何不走“捷”“径”? 2019-04-29
和总裁、专家交朋友,华为云助推政企智能化升级又做到前面去了 2019-04-29
BCOP章鱼船长,6月22日晚上8点上线薄饼 2019-04-29
为战疫助力,半导体功不可没 2019-04-29
了解这些操作,Python中99%的文件操作都将变得游刃有余! 2019-04-29
知道如何操作还不够!深入了解4大热门机器学习算法 2019-04-29
只有经历过,才能深刻理解的9个编程道理 2019-04-29
发现超能力:这些数据科学技能助你更高效专业 2019-04-29
AI当道,人工智能将如何改变金融业? 2019-04-29
消除性别成见,技术领域需要更多“乘风破浪的姐姐” 2019-04-29
7行代码击败整个金融业,这对20多岁的爱尔兰兄弟是如何做到的? 2019-04-29
2020十大编程博客:私藏的宝藏编程语言博客大放送! 2019-04-29
编程中的角色选择:哪类工作角色最适合你? 2019-04-29