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
题目链接: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 (无向图欧拉回路)