POJ 1041 John's trip (无向图欧拉回路)

John‘s trip

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7433   Accepted: 2465   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.

Source

Central Europe 1995

题目链接:http://poj.org/problem?id=1041

题目大意:一个人从他家开始,通过城里的每条街道一次且仅一次,输出环城之行的街道编号,如果有多组解,输出字典序最小的

输入x y为两个路口编号,z为街道编号

题目分析:本题要求计算无向图的欧拉回路,使得经过边的字典序最小,因为题目说的很明确,每条街道都有通向另一条街道的路,因此图肯定是个连通图,判断并获取欧拉路的方法如下:

1)在输入城市交通信息的同时构造无向图,计算每个节点的度数,结点的最小编号st和边序号的最大值n

2)搜索所有结点,若存在度数为奇数的结点,则失败退出

3)从st出发通过dfs搜索计算欧拉回路,为了保证欧拉回路的最小字典序,按照编号递增的顺序寻找当前结点的相连边,因为递归的缘故,最后得到的欧拉回路是反序的

4)反序输出欧拉回路

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

struct NODE
{
    int x, y;
}nd[2005];
bool vis[2005];
int deg[50], ans[2005];
int st, n, len;

void get(int x, int y, int num)
{
    n = max(n, num);
    st = min(x, y);
    nd[num].x = x;
    nd[num].y = y;
    deg[x]++;
    deg[y]++;
}

bool exist()
{
    for(int i = 1; i <= 50; i++)
        if(deg[i] % 2)
            return false;
    return true;
}

void DFS(int now) //这里注意vis[st]一开始不能设为true,因为是回路
{
    for(int i = 1; i <= n; i++)
    {
        if(!vis[i] && ((nd[i].x == now) || (nd[i].y == now)))
        {
            vis[i] = true;
            DFS(nd[i].x + nd[i].y - now);
            ans[len++] = i;
        }
    }
}

int main()
{
    int x, y, num;
    while(scanf("%d %d", &x, &y) && (x + y))
    {
        memset(deg, 0, sizeof(deg));
        scanf("%d", &num);
        n = 0;
        get(x, y, num);
        while(scanf("%d %d", &x, &y) && (x + y))
        {
            scanf("%d", &num);
            get(x, y, num);
        }
        if(exist())
        {
            len = 0;
            memset(vis, false, sizeof(vis));
            DFS(st);
            for(int i = len - 1; i > 0; i--)
                printf("%d ", ans[i]);
            printf("%d \n", ans[0]);
        }
        else
            printf("Round trip does not exist.\n");
    }
}

POJ 1041 John's trip (无向图欧拉回路)

时间: 2024-10-07 02:01:31

POJ 1041 John's trip (无向图欧拉回路)的相关文章

POJ 1041 John&#39;s trip 无向图的【欧拉回路】路径输出

欧拉回路第一题TVT 本题的一个小技巧在于: [建立一个存放点与边关系的邻接矩阵] 1.先判断是否存在欧拉路径 无向图: 欧拉回路:连通 + 所有定点的度为偶数 欧拉路径:连通 + 除源点和终点外都为偶数 有向图: 欧拉回路:连通 + 所有点的入度 == 出度 欧拉路径:连通 + 源点 出度-入度=1 && 终点 入度 - 出度 = 1 && 其余点 入度 == 出度: 2.求欧拉路径 : step 1:选取起点(如果是点的度数全为偶数任意点为S如果有两个点的度数位奇数取一

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

就是欧拉判定,判定之后就可以使用DFS求欧拉回路了.图论内容. 这里使用邻接矩阵会快很多速度. 这类题目都是十分困难的,光是定义的记录的数组变量就会是一大堆. #include <cstdio> #include <cstring> #include <stack> #include <vector> using namespace std; struct Edge { int ed, des; Edge(int e = 0, int d = 0) : ed

poj 1041 John&#39;s trip 欧拉回路

题目链接 求给出的图是否存在欧拉回路并输出路径, 从1这个点开始, 输出时按边的升序输出. 将每个点的边排序一下就可以. 1 #include <iostream> 2 #include <vector> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <cmath> 7 #include <map> 8 #include

poj1041 John&#39;s trip,无向图求欧拉回路路径

点击打开链接 无向图求欧拉回路: 1.图连通 2.所有顶点的度数位偶数 #include <cstdio> #include <cstring> #include <stack> #include <queue> #include <algorithm> using namespace std; const int mt = 2000; const int ms = 50; bool vis[mt+5]; int g[ms][mt+5]; int

poj1041 John&#39;s trip (无向图求欧拉回路方案)

John's trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5950   Accepted: 1946   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 frien

poj 1386 Play on Words(有向图欧拉回路)

1 /* 2 题意:单词拼接,前一个单词的末尾字母和后一个单词的开头字母相同 3 思路:将一个单词的开头和末尾单词分别做两个点并建一条有向边!然后判断是否存在欧拉回路或者欧拉路 4 5 再次强调有向图欧拉路或欧拉回路的判定方法: 6 (1)有向图G为欧拉图(存在欧拉回路),当且仅当G的基图连通,且所有顶点的入度等于出度. 7 (2)有向图G为半欧拉图(存在欧拉道路),当且仅当G的基图连通,且存在顶点u的入度比出度大1.v的入度比出度小1, 8 其它所有顶点的入度等于出度(顶点u,v的个数必须都是

John&#39;s trip

[题目描述] John想要拜访每位朋友,且每条道路他都只走一次. John希望从家里出发,拜访完所有朋友后回到自己的家,且总的路程最短.John意识到如果可以然后返回起点应该是最短的路径.写一个程序帮助John找到这样的路径.给出的每条街连接两个路口,最多有1995条街,最多44个路口.街编号由1到n, 路口分别编号1到m. 输入:每个用例一个数据块:每行表示一条街,由三个整数组成:x,y,z. z为这条街的编号,x和y表示这条街连接的两个路口的编号.(实际数据中可能是自环).John住在一个输

POJ 3352 Road Construction 使得无向图边变双连通图

点击打开链接 Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8168   Accepted: 4106 Description It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of

POJ 1637 Sightseeing tour (混合图欧拉回路,网络最大流)

http://poj.org/problem?id=1637 Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7498   Accepted: 3123 Description The city executive board in Lund wants to construct a sightseeing tour by bus in Lund, so that tourists can