poj 1041 C - John's trip

来源:互联网 发布:js鼠标点击事件 编辑:程序博客网 时间:2024/06/05 04:43

问题描述

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 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 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 12 3 23 1 61 2 52 3 33 1 40 01 2 12 3 21 3 32 4 40 00 0

样例输出

1 2 3 5 4 6 Round trip does not exist.


欧拉回路要满足:1、图是连通的。2、有向图必须每个点的入读和出度相等,无向图的话就是度为偶数;

由于题中已经说了是连通的所以就看是否度数为偶数即可;

这道题关键是最后输出要字典序输出,所以要从小往大搜,dfs的时候得回溯回来之后再保存,这样每次保存下来的都是走不通的路或者是一个环。最后倒着输出出来。

void dfs(int t){    for(int i=1;i<=tal[t];i++)    {        if(!vis[ma[t][i]])        {            vis[ma[t][i]]=1;            dfs(map[t][ma[t][i]]);            a.push(ma[t][i]);        }    }}

其中tai[i]表示第i个点一共与多少边相连,ma[i][j]表示与第i个点相连的第j条边,map[i][j]用来保存图,即第i个点经过第j条边,到达了第map[i][j]点。


代码如下:

#include <iostream>#include <stdio.h>#include <algorithm>#include <string.h>#include <stack>using namespace std;const int maxx=2000;int cc;int du[maxx];int map[maxx][maxx];//map[i][j]表示点i通过j到达z点;int tal[maxx];//tal[x]表示点x有几条邻边int ma[maxx][maxx];//ma[i][j]表示连接i点的第j条边int vis[maxx];stack <int> a;void dfs(int t){    for(int i=1;i<=tal[t];i++)    {        if(!vis[ma[t][i]])        {            vis[ma[t][i]]=1;            dfs(map[t][ma[t][i]]);            a.push(ma[t][i]);        }    }}bool is_ok(){    for(int i=1;i<=cc;i++)    {        if(du[i]%2==1)            return false;    }    return true;}int main(){    int x,y,z;    while(scanf(" %d%d",&x,&y))    {        while(!a.empty())a.pop();        if(x==0&&y==0)break;        scanf("%d",&z);        memset(vis,0,sizeof(vis));        memset(tal,0,sizeof(tal));        memset(du,0,sizeof(du));        cc=0;        du[x]++;du[y]++;cc=max(cc,max(x,y));        map[x][z]=y;map[y][z]=x;        ma[x][++tal[x]]=z;        ma[y][++tal[y]]=z;        while(scanf(" %d%d",&x,&y))        {if(x==0&&y==0)break;              cin>>z;        du[x]++;du[y]++;cc=max(cc,max(x,y));        map[x][z]=y;map[y][z]=x;        ma[x][++tal[x]]=z;        ma[y][++tal[y]]=z;        }        if(!is_ok())            cout<<"Round trip does not exist."<<endl;        else        {            for(int i=1;i<=cc;i++)                sort(ma[i]+1,ma[i]+1+tal[i]);            dfs(1);            while(!a.empty())            {                cout<<a.top()<<" ";                a.pop();            }            cout<<endl;        }    }}




0 0
原创粉丝点击