poj1041 John's Trip (no-map-Euler-loop scheme)

Source: Internet
Author: User
Tags integer numbers

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 visit He friends. Johnny wanted to visit all he friends, but there was many of them. In each street he had one friend. He started thinking how to make the His-trip as short as possible. Very soon he realized that's the best-to-do it's-to-travel through, each street's only once. Naturally, he wanted to finish he-trip at the same place he started, at his parents ' house.

The streets in Johnny's town were named by an 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 streets. All junctions in the town had different numbers. Each street is connecting exactly the junctions. No. Streets in the town had the same number. He immediately started to plan his round trip. If there is more than one such round trips, he would has chosen the one which, when written down as a sequence of street Numbers is lexicographically the smallest. But Johnny is not able to find even one such round trips.

Help Johnny and write a program which finds the desired shortest round trips. If The round trip does isn't 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 is the town is. There exists a is from each street to another street in the town. The streets in the town was very narrow and there is no possibility to turn back the car once he's in the street

Input

Input file consists of several blocks. Each block is 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 is 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 was an empty block, x = y = 0.

Output

Output one line of each block contains the sequence of the street numbers (single members of the sequence is separated by spa CE) describing Johnny ' s round trip. If The round trip cannot is 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

<span style= "FONT-SIZE:14PX;" >1 2 3 5 4 6 </span><p style= "FONT-SIZE:14PX;" >round trip does not exist.</p><p style= "FONT-SIZE:14PX;" ></p><p><strong><span style= "FONT-SIZE:18PX;" > This topic from half a morning to do this time, first the algorithm to understand, and then is to find the bug nearly three hours!! Bug When judging the equal time to use two equal signs live less write a!!!! Drunk ~. The core idea is that, in the case of having a Euler loop, this can be judged from the sufficient condition that all points are even numbers, and then start Dfs from the smallest side of the label, since there is a process of ordering the adjacency table for you, then inevitably the DFS process that rises along the label of the Edge. Encounter can not get through, then there must be a ring at this time, otherwise there will be no way, then the whole map is a ring. When encountering the minimum order ring (assuming existence) and unable to continue the Dfs backtracking, the back side is bound to the last of the Euler loop, directly push, otherwise it will appear along the Euler loop "too fast" return to the original point. Encounter the smallest sequence ring and can not go through the case, at this point from the minimum order loop to traverse the DFS and follow the order of the edge of the ascending sequence of deep search other rings, then the path inside to the last to save is the reverse of the Euler path, reverse a bit on the DA ~</span></ Strong></p><p style= "FONT-SIZE:14PX;" ><pre name= "code" class= "CPP" > #include <iostream> #include <sstream> #include <algorithm># include<cstdio> #include <string.h> #include <cctype> #include <string> #include <cmath> #include <vector> #include <stack> #include <queue> #include <map> #include<set>using namespace STD;//INPUT:ADJ global variable, adj "I" indicates whether there is a solution from all sides that are connected from node I. There is no direction, and Path (global) saves the Euler loop. const int Maxn=2000;const int Maxm=1000000;int father[maxn];vector< pair<int,int > > Adj[maxn];bool VIS[MAXM ];int getfather (int x) {return x==father[x]?x:father[x]=getfather (father[x]);//int root=a;//int temp;//whi Le (father[root]!=root)//root=father[root];//while (father[a]!=root)//{//temp=a;//A=father[a    ];//father[temp]=root;//}}void Add (int x,int y,int z) {adj[x].push_back (Make_pair (z,y)); Adj[y].push_back (Make_pair (z,x));}        Vector<int >path;void dfs (int u) {for (int it=0; it<adj[u].size (); it++) if (!vis[adj[u][it].first])            {vis[adj[u][it].first]=1;            DFS (Adj[u][it].second);        Path.push_back (Adj[u][it].first);    }}bool solve () {for (int i=0; i<maxn; i++) father[i]=i; for (int i=0, i<maxn; i++) {for (int J =0; J < Adj[i].size () ;        ++J) {father[getfather (i)]=getfather (Adj[i][j].second);    }} int origin=-1;            for (int i=0; i<maxn; i++) if (Adj[i].size ()) {if (Adj[i].size ()%2==1) return 0;            if (origin==-1) origin=i;            if (Getfather (i)!=getfather (origin)) return 0;        Sort (Adj[i].begin (), Adj[i].end ());    } path.clear ();    memset (vis,0,sizeof (VIS));    if (Origin!=-1) Dfs (origin);    Reverse (Path.begin (), Path.end ()); return 1;}    int main () {int x, y, Z;        while (scanf ("%d%d", &x,&y)!=eof) {if (x==0&&y==0) break;        scanf ("%d", &z);        for (int i=0; i<maxn; i++) adj[i].clear ();        Add (x, y, z);            while (scanf ("%d%d", &x,&y)!=eof) {if (y==0&&x==0) break;            scanf ("%d", &z);        Add (x, y, z);            } if (solve ()) {int i;           for (i=0; I<path.size ()-1; i++)     printf ("%d", path[i]);        printf ("%d\n", Path[i]);    } else printf ("Round trip does not exist.\n"); } return 0;}


poj1041 John's Trip (no-map-Euler-loop scheme)

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.