Poj 1041 Problem Solving report John's trip

Source: Internet
Author: User
Tags integer numbers
John's trip
Time limit:1000 ms   Memory limit:65536 K
Total submissions:6042   Accepted:1981   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 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

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
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 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.
It is still Euler's loop, but note that if there is a single point, we should ignore it. We use vectors to do it. The questions of Euler's loop are the same and there is nothing new.
#include <iostream>#include<stdio.h>#include<algorithm>#include<vector>using namespace std;struct tree{int index;int to;};int in[2000],down[2000],step,last,fron,result[200000];bool re[200000];vector<tree> p[2000];void dfs(int u,int x){      int i,minn,j;      bool zhe;      for(i=0;i<p[u].size();i++)      {            minn=0x4f4f4f4f;zhe=true;           for(j=0;j<p[u].size();j++)            {                  if(re[p[u][j].index])                  {                      last=p[u][j].index;                        if(last<minn)                        {                        minn=last;                        fron=j;                        zhe=false;                        }                  }            }            if(zhe)                  break;           if(re[minn])            {                  re[minn]=false;                  dfs(p[u][fron].to,p[u][fron].index);            }      }      if(x!=-1)      {            result[step++]=x;      }}int  fin(int x){      if(down[x]!=x)      {            down[x]=fin(down[x]);      }      return down[x];}int main(){     int i,a,b,s,x,y,maxn,k;     bool zhe;     tree temp;   while(scanf("%d%d",&x,&y)!=EOF)   {         k=0;maxn=0;         for(i=0;i<2000;i++)         {               p[i].clear();               down[i]=i;               in[i]=0;         }         if(x==0&&y==0)            break;         scanf("%d",&s);         re[s]=true;         temp.index=s;         temp.to=y;         p[x].push_back(temp);         temp.index=s;         temp.to=x;         p[y].push_back(temp);         in[x]++;         in[y]++;         a=fin(x);         b=fin(y);         if(a!=b)         {               down[a]=b;               k++;         }         maxn=x;         if(y>x)            maxn=y;         while(scanf("%d%d",&x,&y)!=EOF)         {                 a=fin(x);                  b=fin(y);            if(a!=b)            {               down[a]=b;                k++;            }               if(x>maxn)                  maxn=x;               if(y>maxn)                  maxn=y;               if(x==0&&y==0)                  break;               scanf("%d",&s);               re[s]=true;                  temp.index=s;                  temp.to=y;                  p[x].push_back(temp);                  temp.index=s;                  temp.to=x;                  p[y].push_back(temp);               in[x]++;               in[y]++;         }         zhe=true;         for(i=1;i<=maxn;i++)         {               if(in[i]%2)               {                     zhe=false;                     break;               }         }         if(zhe==false)         {               printf("Round trip does not exist.\n");         }         else         {               step=1;            dfs(1,-1);            i=step-1;            printf("%d",result[i]);            i--;            for(;i>0;i--)                  printf(" %d",result[i]);            printf("\n");         }   }    return 0;}

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.