POJ 1041 John's trip
John's trip
Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 104164-bit integer IO format: %lld Java class name: Main
Special Judge
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
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
解题:求欧拉回路,还要字典序最小。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 2010; 18 int g[50][maxn],degree[50],stk[maxn],tp,n,m; 19 bool vis[maxn]; 20 void dfs(int u) { 21 for(int i = 1; i <= n; i++) { 22 if(!vis[i] && g[u][i]) { 23 vis[i] = true; 24 dfs(g[u][i]); 25 stk[tp++] = i; 26 } 27 } 28 } 29 int main() { 30 int x,y,z,st,i; 31 while(scanf("%d %d",&x,&y),x||y) { 32 n = m = tp = 0; 33 memset(vis,false,sizeof(vis)); 34 memset(degree,0,sizeof(degree)); 35 memset(g,0,sizeof(g)); 36 st = min(x,y); 37 scanf("%d",&z); 38 n = max(n,z); 39 m = max(m,max(x,y)); 40 g[x][z] = y; 41 g[y][z] = x; 42 degree[x]++; 43 degree[y]++; 44 while(scanf("%d %d",&x,&y),x||y) { 45 scanf("%d",&z); 46 n = max(n,z); 47 m = max(m,max(x,y)); 48 g[x][z] = y; 49 g[y][z] = x; 50 degree[x]++; 51 degree[y]++; 52 } 53 for(i = 1; i <= m; i++) 54 if(degree[i]&1) break; 55 if(i <= m) puts("Round trip does not exist."); 56 else { 57 dfs(st); 58 tp--; 59 printf("%d",stk[tp--]); 60 while(tp >= 0) printf(" %d",stk[tp--]); 61 puts(""); 62 } 63 } 64 return 0; 65 } 66 /* 67 1 2 1 68 2 3 2 69 3 1 6 70 1 2 5 71 2 3 3 72 3 1 4 73 0 0 74 */
夜空中最亮的星,照亮我前行