UVa 302 - John's trip

contents

  1. 1. Problem
  2. 2. Input
  3. 3. Output
  4. 4. Sample Input
  5. 5. Sample Output
  6. 6. Solution

Problem

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, tex2html_wrap_inline32 . 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
3
4
5
6
7
8
9
10
11
12
13
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
1 2 3 5 4 6
Round trip does not exist.

Solution

題目描述:

每一條路上都有編號,並且每個編號唯一且個數小於 44。

求一條經過所有邊且不重複的環道。

題目解法:

記得注意起始是第一條邊最小編號的點。

之後對於每一個點相鄰邊根據編號進行排序即可,按著窮舉思路由小開始枚舉即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <deque>
using namespace std;
struct edge {
int to, label;
edge(int a=0, int b=0):
to(a), label(b) {}
bool operator<(const edge &x) const {
return label < x.label;
}
};
#define MAXN 2048
vector<edge> g[MAXN];
int label_used[MAXN];
deque<int> path;
void dfs(int u) {
for(int i = 0; i < g[u].size(); i++) {
if(label_used[g[u][i].label] == 0) {
label_used[g[u][i].label] = 1;
dfs(g[u][i].to);
path.push_front(g[u][i].label);
}
}
}
int main() {
int x, y, z;
while(true) {
for(int i = 0; i < MAXN; i++)
g[i].clear();
int e = 0, start = 0;
while(scanf("%d %d", &x, &y) == 2) {
if(x == 0 && y == 0)
break;
/* Johnny lives at the junction ending the 1st street
input with smaller number. */
if(e == 0)
start = min(x, y);
scanf("%d", &z);
g[x].push_back(edge(y, z));
g[y].push_back(edge(x, z));
e++;
}
if(e == 0) break;
int odd = 1;
for(int i = 0; i < MAXN; i++) {
odd &= g[i].size()%2 == 0;
}
if(!odd) {
puts("Round trip does not exist.\n");
continue;
}
for(int i = 0; i < MAXN; i++)
sort(g[i].begin(), g[i].end());
memset(label_used, 0, sizeof(label_used));
path.clear();
dfs(start);
for(int i = 0; i < path.size(); i++)
printf("%d%c", path[i], i == path.size()-1 ? '\n' : ' ');
puts("");
}
return 0;
}