poj1041

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边。现在要问你该图中是否存在欧拉回路,如果存在,则输出字典序最小的那条欧拉回路(输入按序走过的所有边标号)。且题目中保证了该无向图是连通的。

分析:

        首先欧拉路径的输出方式可以参考刘汝佳入门经典P112中的代码。不过那里的图中的边是用(i,j)来表示的,而这里的图中的边是用一个数字编号表示的.所以源代码中还需要做点小修改.

        然后要保证从John的家作为起始点输出欧拉回路且保证字典序最小,因为euler这个函数输出的欧拉路径是从起点逆序的(即起点被放到了最后才输出),所以我们需要把最后结果保存在ans数组中,最后逆序输出,且每次都是从小到大选择与当前节点相连的可行边的(这样可以保证输出结果的字典序最小).

        当然还需要在用euler之前判断无向连通图的所有节点是不是都是偶数度,如果不是则不存在欧拉回路
代码:
 

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxe=1995+10;
const int maxv=44+10;
struct edge
{
    int u,v;
}edges[maxe];
int n,m;//n表示节点数,m表示边数,边与点都从1开始编号
int degree[maxv];//每个点的度数
int vis[maxe];//标记边是否被走过
int ans[maxe];//最终结果存在这里
int cnt;//当前存在ans中的边下标
bool ok()
{
    for(int i=1;i<=n;i++)if(degree[i]%2!=0) return false;
    return true;
}
void euler(int s)
{
    for(int i=1;i<=m;i++)if(!vis[i] && (edges[i].u==s||edges[i].v==s) )
    {
        vis[i]=1;
        euler(edges[i].u+edges[i].v-s);//dfs搜索最后其实是逆序,从这不一致往后递归。
        ans[cnt++]=i;
    }
}
int main()
{
    int x,y,z;
    while(scanf("%d%d",&x,&y)==2&&x)
    {
        cnt=n=m=0;
        int John_home = min(x,y);
        memset(degree,0,sizeof(degree));
        memset(vis,0,sizeof(vis));
        do
        {
            scanf("%d",&z);
            edges[z].u=x;
            edges[z].v=y;
            degree[x]++;
            degree[y]++;
            m++;
            n=max(n,max(x,y));
        }while(scanf("%d%d",&x,&y)==2&&x);
        if(!ok())
        {
            printf("Round trip does not exist.\n");
            continue;
        }
        euler(John_home);
        printf("%d",ans[m-1]);
        for(int i=m-2;i>=0;i--)
            printf(" %d",ans[i]);
        printf("\n");
    }
    return 0;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值