欧拉回路模板题poj1041

19 篇文章 0 订阅

John's trip

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9877 Accepted: 3330 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<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
// 如果判联通的话,可以用并查集
using namespace std;
const int N =2005;
const int inf =0x3f3f3f3f;
int maxn;

struct node
{
    int v;
    int val;
};

vector< node >ve[N];
int inde[N];
stack< int >ss;
vector< int >ans;
int vis[N];

void init()
{
    memset(inde,0,sizeof(inde));
    for(int i=0;i<N;i++) ve[i].clear();
    ans.clear();
    memset(vis,0,sizeof(vis));
}

bool cmp(node a,node b)
{
    return a.val<b.val;
}


void fleury(int u)
{
    int v,i;
    for(i=0;i<ve[u].size();i++){
        if(!vis[ve[u][i].val]){
            vis[ve[u][i].val ]=1;
            fleury(ve[u][i].v);
            ans.push_back(ve[u][i].val);
        }
    }
}


/*
void dfs(int u)
{
    ss.push(u);
    for(int i=0;i<ve[u].size();i++){
        if(vis[ve[u][i].val]) continue;
        vis[ ve[u][i].val ]=1;
        dfs(ve[u][i].v);
        break;
    }
}

void fleury(int st)
{
    ss.push(st);
    while(!ss.empty()){
        int f=0;
 //       cout<<"u "<<ss.top()<<endl;
        int u=ss.top(); ss.pop();
        for(int i=0;i<ve[u].size();i++){
            if(vis[ve[u][i].val ]) continue;
            f=1; break;
        }
        if(!f) cout<<u<<" ";
        else dfs(u);
    }
}
*/

int main()
{
    int u,v;
    node tmp;
    int st=1;
    int val;
    while(scanf("%d %d",&u,&v)!=EOF)
    {
        if(u==0&&v==0) break;
        init();
        maxn=-1;
        scanf("%d",&val);
        maxn=max(u,v);
        st=min(u,v);

        inde[v]++;
        inde[u]++;

        tmp.v=v; tmp.val=val;
        ve[u].push_back(tmp);
        tmp.v=u; tmp.val=val;
        ve[v].push_back(tmp);
        while(scanf("%d %d",&u,&v)!=EOF){
            if(u==0&&v==0) break;

            maxn=max(u,v);
            inde[v]++; inde[u]++;
            scanf("%d",&val);
            tmp.v=v; tmp.val=val;
            ve[u].push_back(tmp);
            tmp.v=u;
            ve[v].push_back(tmp);
        }

        int f=0;
        for(int i=1;i<=maxn;i++){
            if(inde[i]&1){  //奇度定点不能有
                f=1; break;
            }
        }

        if(f){
            printf("Round trip does not exist.\n");
            continue;
        }
        while(!ss.empty()) ss.pop();
        for(int i=1;i<=maxn;i++) sort(ve[i].begin(),ve[i].end(),cmp);
        fleury(st);
        for(int i=ans.size()-1;i>=0;i--){
            if(i==0) printf("%d\n",ans[i]);
            else printf("%d ",ans[i]);
        }

    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值