POJ1041(Euler Tour)

1041:John's trip

时间限制:

1000ms

内存限制:

65536kB

描述

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. Thejunctions 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 islexicographically 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 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. (由此可见,不可能一开始就只有0 0输入的)

输出

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."

样例输入

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

样例输出

1 2 3 5 4 6

Round trip does not exist.

Help1: 这道题很不错,由于图已经保证连通,首先用度数是否是偶数,判断图是否是欧拉图,然后,输出最小升序,就成了一大难题,百科上有代码,这题让我理解了深搜的又一强大功能,其实就是每次都从小往大的搜,先搜得一个最小序环,然后对环上的每一点进行搜索,其实对于欧拉图而言,每个点要么就只剩一个点,什么也搜不到了,要么还有一个环,只要把环上路径全都插入到对应位置上,用栈存路径,每次只有回溯到当前点,就是说当前点的后继都已经搜过了的时候,才把当前点入栈,这样一来倒着输出,就能得到一个欧拉回路,而且是最小升序。

 

#include"iostream"
#include"cstring"
#include"stack"
#include"stdlib.h"
using namespace std;
const int edge=1994;
const int vertix=44;
//const int e=1994;//边数
//const int v=44;//节点数
int count[edge+1+1];//记录各个节点邻边的个数(需要+1+1是因为保留额外的一位储存0作为监视哨)
int link_uv[vertix+1][edge+1];//link_uv[u][e]=v表示u通过边e可以到达v;卧槽,原来link在POJ编译时候是关键字来的,狂哇不止,改了link_uv
int adj[vertix+1][edge+1];//adj[u][i]=e表示u的第i条邻边是e
bool vis[edge+1];//已访问的边 
stack<int> ans;//存储结果
int num;//当前节点的数目,同时作为监视哨;它的确定需要一定技巧,见下面
int cmp(const void *a,const void *b)
{
 return *(int *)a-*(int *)b;
}
void euler(int u)
{
 int v,e;
 for(int i=1;i<=count[u];i++)    
 {                                                  
  e=adj[u][i];        
  //cout<<"e: "<<e<<" "<<"vis[e]"<<vis[e]<<endl;
  if(!vis[e])                        
  {      
  vis[e]=true;      
  v=link_uv[u][e];
  euler(v);                
  //cout<<"e: "<<e<<" "; 
  ans.push(e);   
  }//end if
 }//end for  
}                                  
int main()      
{        
 int x,y;       
 int z;          
 int start;   
 cin>>x>>y;                                        
   while(x&&y)    
   {         
    num=0; 
    start=(x<y)?x:y;  
    memset(count,0,sizeof(count));
    memset(adj,0,sizeof(adj));
    memset(link_uv,0,sizeof(link_uv));       
    memset(vis,false,sizeof(vis)); 
    while(!ans.empty())//清空ans栈
     ans.pop();
    do
    {        
     cin>>z;
     adj[x][++ count[x]]=z,adj[y][++count[y]]=z;//增加节点邻边
     link_uv[x][z]=y,link_uv[y][z]=x;//增加x->y的联系
    }while(cin>>x>>y&&!(x==0&&y==0));
    int success=true;
    int i;//监视哨
    for(i=1;;i++)
    {
     if(count[i]==0||(count[i]&1))//到达终点后一位或者存在节点的度为奇数
      break;
    }
    if(count[i]!=0)
    {
      cout<<"Round trip does not exist.\n"; 
   goto Mark;
    }
    num=i-1;//获取num值
  for(int i=1;i<=num;i++)
  {
   qsort(adj[i]+1,count[i],sizeof(int),cmp);
  }
    euler(start);
   //输出成功的结果
    while(!ans.empty())
    {
     cout<<ans.top()<<" ";
     ans.pop();
    }
    cout<<endl;
Mark:
    cin>>x>>y;
   }
}

原文地址:https://www.cnblogs.com/lzhitian/p/2138641.html