UVA302 UVALive5517 John's trip【欧拉回路+DFS】

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. All junctions in the town had different numbers. Each street was connecting exactly two (not necessarily different) 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 1st street 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

The output file consists of 2 line blocks corresponding to the blocks of the input file. The first 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.’. The second line of each block is empty.

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.

问题链接UVA302 UVALive5517 John's trip

问题简述

  m(m<=44)个城镇,每个城镇有连接n条街道,Johnny想要从某个城镇出发,经过所有街道并且每条街道通过一次,回到出发点,找出一个可行的路线,依次输出经过的街道编号。如果有多条路线,选择字典序(按街道)最小的一条输出。

问题分析

  一个典型的欧拉回路问题,算一下各个节点(城镇)的入度是必要的,就可以判定是否是一个欧拉回路。算入度,只需要知道各条边就可以了,不需要保存图。

  为了按照字典顺序输出结果,则需要存储图,对图做DFS计算。

  这个题与参考链接是同一个题,只是输出格式不同。

程序说明

  程序中图的表示(存储)比较特殊,方便按字典顺序输出结果。

题记:(略)

参考链接POJ1041 John's trip【欧拉回路+DFS】

AC的C++语言程序如下:

/* UVA302 UVALive5517 John's trip */

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>

using namespace std;

const int M = 44;
const int N = 1995;
int g[M + 1][N], din[M + 1];        // 为了DFS方便,图g[u][w]=v,u和v是城镇,w是街道
int maxw, cnt, ans[N], vis[N];

void dfs(int s)
{
    for(int i = 1; i <= maxw; i++)
        if(vis[i] == 0 && g[s][i]) {
            vis[i] = 1;
            dfs(g[s][i]);
            ans[cnt++] = i;
        }
}

int main()
{
    int u, v, w, start = 0;
    while(~scanf("%d%d", &u, &v) && (u || v)) {
        memset(g, 0, sizeof(g));
        memset(din, 0, sizeof(din));
        start = min(u, v);
        maxw = 0;
        while(u != 0 && v != 0) {
            scanf("%d", &w);
            g[u][w] = v;
            g[v][w] = u;
            din[u] = 1 - din[u]; // 节点入度只需要判奇偶性
            din[v] = 1 - din[v];
            maxw = max(maxw, w);
            scanf("%d%d", &u, &v);
        }

        // 检查是否有入度为奇数的节点
        int k = 1;
        for(; k <= M; k++)
            if(din[k])
                break;
        if(k <= M)
            printf("Round trip does not exist.\n");
        else {
            cnt = 0;
            memset(vis, 0, sizeof(vis));
            dfs(start);

            // 输出结果
            for(int i = cnt -1; i >= 0; i--)
                if(i == 0)
                    printf("%d\n", ans[i]);
                else
                    printf("%d ", ans[i]);
        }
        printf("\n");
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/81289610