POJ1041 John's trip 无向图的欧拉回路路径输出

此题要求我们判断欧拉回路是否存在,如果存在还要用最大字典序输出。这样我们就直接吧每一个出边表连出去的边根据从小到大排序就好。这样越小的节点号越早遍历到。最后从栈顶输出的时候自然就是最大的字典序了。

至于求欧拉回路的路径这里我用了DFS,其实还有专门的一种算法叫Fleury。

用DFS的方法是,之前的是否存在的判断仍与之前相同,当判定存在的时候,在图上选择一个正确的起点(如果是回路那个点都可以当起点,但如果是通路这里就要找出七点了),然后用dfs遍历每一条边(注意是边),遇到走不通的就回溯,将路径上的边压入栈,然后从栈顶输出买这个序列就是欧拉回路。

注:欧拉回路有一个这样的性质,我们从图G中去掉一个圈得到新图G‘有欧拉回路,那么G也有欧拉回路,基于这个性质,一旦找到一个圈就消去,从图中拿出,知道图为空。

无向图与有向图的求法相似。(注欧拉通路最后去圈后会剩下一条路径。因为是通路不是回路,最后当然没有一个完整的圈了。。。。。)


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


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<utility>


using namespace std;

#define MAXN 2000
#define MAXM 1000000

int path[MAXM],top;
int deg[MAXN];
int set[MAXN];
bool vis[MAXM];
vector< pair<int,int> > g[MAXN];

void add(int u,int v,int e)
{
    g[u].push_back(make_pair(e,v));
    g[v].push_back(make_pair(e,u));
}

void init(int n)
{
	for(int i=0;i<=n;i++)
		set[i]=i;
}


int find(int a)
{

	int root=a;
	int temp;
	while(set[root]!=root)
		root=set[root];
	while(set[a]!=root)
	{
		temp=a;
		a=set[a];
		set[temp]=root;
	}
	return root;
}


bool merge(int a,int b)
{
	int x=find(a);
	int y=find(b);
	if(x==y)
		return false;
	set[x]=y;
	return true;
}

void dfs(int u)
{
    for(int i=0;i<g[u].size();i++)
        if(!vis[g[u][i].first])
        {
            vis[g[u][i].first]=1;
            dfs(g[u][i].second);
            path[top++]=g[u][i].first;
        }
}

bool eluer()
{
    init(MAXN-1);
    for(int i=0;i<MAXN;i++)
        for(int j=0;j<g[i].size();j++)
            merge(find(g[i][j].second),find(i));
    int st=-1;
    for(int i=0;i<MAXN;i++)
        if(g[i].size())
        {
            if(deg[i]%2==1) return false;
            if(st==-1) st=i;
            if(find(st)!=find(i)) return false;
            sort(g[i].begin(),g[i].end());
        }
    top=0;
    memset(vis,0,sizeof(vis));
    if(st!=-1) dfs(st);
    return true;
}

int x,y,z;

int main()
{
    while(~scanf("%d%d",&x,&y))
    {
        if(x==0 && y==0) break;
        scanf("%d",&z);
        for(int i=0;i<MAXN;i++)
            g[i].clear();
        memset(deg,0,sizeof(deg));
        add(x,y,z);
        deg[x]++,deg[y]++;
        while(~scanf("%d%d",&x,&y))
        {
            if(x==0 && y==0) break;
            scanf("%d",&z);
            add(x,y,z);
            deg[x]++,deg[y]++;
        }
        if(!eluer())
            printf("Round trip does not exist.\n");
        else
        {
            for(int i=top-1;i>=0;i--)
                printf("%d%s",path[i],i!=0?" ":"\n");
        }
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值