POJ 1041 John's trip(无向图欧拉回路 && 路径)

来源:互联网 发布:淘宝机构招聘信息 编辑:程序博客网 时间:2024/04/29 00:52
John's trip
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7433 Accepted: 2465 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 12 3 23 1 61 2 52 3 33 1 40 01 2 12 3 21 3 32 4 40 0

0 0

每行输入的三个数(x,y,z),表示由x到y的街道编号为z(街道由1~n命名n<1995,街道口由1~m命名 m<=44),问能否从街道1出发,每条街道遍历一遍,再回到街道1,如若存在两条以上的环,按字典序输出,不存在输出"Round trip does not exist."。其实就是判断无向图是否连通,是的话输出欧拉回路的路径,无向图连通要保证每个点的度为偶数,然后可以按街道编号递增的顺序dfs。

#include <iostream>#include <cstdio>#include <cstring>#include <stdlib.h>#define N 2000using namespace std;struct node{    int x,y;} q[N];int du[N],ans[N],b[N];    int mi,ma,k;bool connect(){    for(int i=1;i<=44;i++)        if(du[i] && du[i]%2==1)           return true;     return false;}void dfs(int r){    for(int i=1;i<=ma;i++)    {        if(!b[i] && (q[i].x==r||q[i].y==r))   //第i条边是否与r相连         {            b[i]=1;            dfs(q[i].x+q[i].y-r);            ans[k++]=i;        }    }}int main(){    int m,n,s;    while(scanf("%d%d",&n,&m) && (m||n))    {        scanf("%d",&s);        memset(du,0,sizeof(du));        q[s].x=n;        q[s].y=m;        du[n]++;        du[m]++;        ma=s;                         //街道的最大编号         mi=min(n,m);                          while(scanf("%d%d",&n,&m) && (m||n))        {            scanf("%d",&s);            q[s].x=n;            q[s].y=m;            du[n]++;            du[m]++;            ma=max(s,ma);            mi=min(mi,min(n,m));        }        if(connect())             //是否连通图        {            printf("Round trip does not exist.\n");            continue;        }        k=0;        memset(b,0,sizeof(b));        dfs(1);        for(int i=k-1;i>0;i--)            printf("%d ",ans[i]);        printf("%d\n",ans[0]);    }    return 0;}


0 0