poj 1041 John's trip(欧拉回路)

John's trip
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6062 Accepted: 1990 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 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

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.
 
题意:给出一个图,要求以从一个顶点沿着边走下去,每个边只走一次,所有的边都经过后回到原点的字典序最小的路径。输入中每行三个数字想x,y,z,表示结点x到结点y有一条边编号为z。
分析:简单的欧拉回路。欧拉回路:对于一个图可以从一个顶点沿着边走下去,每个边只走一次,所有的边都经过后回到原点的路。一个无向图存在欧拉回路的充要条件是每个顶点的度是偶数且图是连通的,对于有向图存在欧拉回路的条件是每个顶点的出度等于入度。首先判断是否存在欧拉回路,如果存在则用DFS去找欧拉回路。另外,选择好的数据结构很重要。
AC代码:
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <iostream>

using namespace std;
int G[50][2000];        //G[x][z]=y表示x通过边z与y相连
int degree[50];        //保存每个结点的度
int stack[2000];        //保存欧拉路径
bool vis[2000];
int max_street,top;
void Euler(int s)
{
    for(int i=1;i<=max_street;i++)
    if(G[s][i]&&!vis[i])
    {
        vis[i]=true;
        Euler(G[s][i]);
        stack[top++]=i;
    }
}
int main()
{
   int x,y,z,john;
   while(cin>>x>>y)
   {
       if(!x&&!y) break;
       memset(degree,0,sizeof(degree));
       memset(G,0,sizeof(G));
       john=min(x,y);         //john的结点为第一行输入中较小的那个结点
       cin>>z;
       G[x][z]=y;
       G[y][z]=x;
       degree[x]++;
       degree[y]++;
       max_street=0;
       max_street=max(max_street,z);
       while(cin>>x>>y)
       {
           if(!x&&!y) break;
           cin>>z;
           G[x][z]=y;
           G[y][z]=x;
           degree[x]++;
           degree[y]++;
           max_street=max(max_street,z);
       }
       bool flag=true;
       for(int i=1;i<45;i++)        //判断是否存在欧拉回路
       if(degree[i]&1)
       {
           flag=false;
           break;
       }
       if(!flag)
       cout<<"Round trip does not exist."<<endl;
       else
       {
           top=0;
           memset(vis,false,sizeof(vis));
           Euler(john);
           for(int i=top-1;i>=0;i--)
           cout<<stack[i]<<" ";
           cout<<endl;
       }
   }
   return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值