Poj 1041 John's trip

Source: Internet
Author: User
Tags integer numbers
John's triptime limit: 1000 msmemory limit: 65536 kbthis problem will be judged on PKU. Original ID: 1041
64-bit integer Io format: % LLD Java class name: mainspecial 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 quota 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 wocould 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 shocould 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
Inputinput 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. outputoutput 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 12 3 23 1 61 2 52 3 33 1 40 01 2 12 3 21 3 32 4 40 00 0
Sample output
1 2 3 5 4 6 Round trip does not exist.
Sourcecentral Europe 1995: Find the Euler's loop and minimize the Lexicographic Order.
 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 long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 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 168 2 3 269 3 1 670 1 2 571 2 3 372 3 1 473 0 074 */
View code

 

Poj 1041 John's trip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.