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

欧拉回路定义:图G的一个回路,若它恰通过G中每条边一次,则称该回路为欧拉(Euler)回路

欧拉回路存在的充要条件

无向图:是连通图且所有顶点的度为偶数。

有向图:是连通图且所有顶点的入度等于出度。

这里的无向图度为偶数我的理解是,有一条边就为一个度,即一出一入为一度,必须所有点的度是偶数才能形成欧拉回路

那么我们来说说题目,首先这道题不和我们平时做的,直接一个点到另一个点就是一条路径了,这道题目所谓的路口就是顶点点,而在顶点之间,我们可能会有很多条路径,根据无向图对度的定义,那么与m条边相连,那么度就为m条,对于这m条边,如果m为奇数那么一定不能构成欧拉回路。

对于输入数据我们要进行处理,当我们输入

x,y,z

那么就有 map[x][z]=y,map[y][z]=x 这两个元素的意思就是:点x经过路径z能够到达点y,点y经过路径z能够到达点x

在dfs遍历路径的时候 , vis[i]标记的是第i条路径,当通过一条路径的时候,我们就到达点 map[x][z]了,即y点,因此我们进入下一层dfs应该是dfs(map[x][z]),也就是下一层我们从j点开始遍历路径。

我们要把路径用一个栈存储,理解一下递归的过程就可以了。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <stack>
//无向图:一出一入为一度,度为偶数,即欧拉回路
//有向图:出度和入度相等
using namespace std;
int map[50][2000];
int vis[2000];
int degree[2000];
int n;
stack<int >s;
void dfs(int x)
{
    int i;
    for(i=1;i<=n;i++)
    {
        if(!vis[i]&&map[x][i])
        {
            vis[i]=1;
            dfs(map[x][i]);
            s.push(i);
        }
    }
}
int main()
{
    int x,y,flag,z,i,j;
    while(scanf("%d%d",&x,&y))
    {
        while(!s.empty()) s.pop();
        memset(vis,0,sizeof(vis));
        memset(map,0,sizeof(map));
        memset(degree,0,sizeof(degree));
        flag=0;
        n=1;
        if(x==0&&y==0) break;
        scanf("%d",&z);
        map[x][z]=y;
        map[y][z]=x;
        degree[x]++;
        degree[y]++;
        n++;
        int home=min(x,y);
        while(scanf("%d%d",&x,&y))
        {
            if(x==0&&y==0) break;
            scanf("%d",&z);
            map[x][z]=y;
            map[y][z]=x;
            degree[x]++;
            degree[y]++;
            n++;
        }
        for(i=1;i<=n;i++)
        {
            if(degree[i]%2!=0)
            {
                flag=1;
                break;
            }
        }
        if(flag)
        {
            printf("Round trip does not exist.\n");
            continue;
        }
        dfs(home);
        while(!s.empty())
        {
            printf("%d ",s.top());
            s.pop();
        }
        printf("\n");
    }


}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值