POJ1041 John's trip(欧拉回路+打印路径)

John's trip
Time Limit: 1000MS      Memory Limit: 65536K
Total Submissions: 8960     Accepted: 3002      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.
题意讲的是:
首先呢,约翰的家是在输入的第一条街比较小的路口那里,然后输入的每条边都对应一个z的编号,问你最后能够
走一直走通过所有z编号的街道回到1的字典序最小的路径,而且每条边都是无向边。
其实,他就是输出一个欧拉回路的字典序最小的路径。
无向图的欧拉回路应该会判断吧。
通过并查集判断图是否联通,还有判断每个点的度是否为偶数。
然后dfs输出最小路径。
怎么找最小字典序列。
其实每次dfs从最小的z标号开始找呢,由于是欧拉回路,所以最后一定路是能够返回到起点的。
至于为什么是倒着输出是为了解决(1,1,1),(2,2,16)这种对于自身就是一个z标记的点。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#include<limits.h>
#define MOD 1000000007
#define fir first
#define sec second
#define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)
#define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)
#define mes(x, m) memset(x, m, sizeof(x))
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define INF 1e9+7
#define inf 0x3f3f3f3f
#define Pi 4.0*atan(1.0)

#define lowbit(x) (x&(-x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define max(a,b) a>b?a:b

typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-9;
const int maxn = 10000+5;
const int maxm = 10000005;
using namespace std;

inline int read(){
    int x(0),f(1);
    char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int deg[maxn];
int p[maxn];
bool vis[maxn];
Pii vec[maxn];
int t;
void init(){
    for(int i=1;i<=maxn;++i){
        p[i]=i;
    }
    mes(deg,0);
    t=0;
}
bool check(int n){
    for(int i=1;i<=n;++i){
        if(deg[i]&1){
            return false;
        }
    }
    return true;
}
inline int find(int x){
    return x==p[x]?x:p[x]=find(p[x]);
}
inline void merge(int x,int y){
    x=find(x);
    y=find(y);
    if(x!=y){
        p[y]=x;
    }
}
int ans[maxn];
void dfs(int s,int n){
    for(int i=1;i<=n;++i){
        if(!vis[i]&&(vec[i].fir==s||vec[i].sec==s)){
            vis[i]=true;
            dfs(vec[i].second+vec[i].first-s,n);
            ans[t++]=i;
        }
    }
    return;
}
int main(){
    fin;
    int x,y,z,s,maxZ,n;
    while(~scanf("%d%d",&x,&y)){
        init();
        n=-1;
        maxZ=0;
        if(!x&&!y){
            break;
        }
        scanf("%d",&z);
        n=max(n,(max(x,y)));
        s=min(x,y);
        ++maxZ;
        deg[x]++;
        deg[y]++;
        merge(x,y);
        vec[z]={x,y};
         while(~scanf("%d%d",&x,&y)){
            if(!x&&!y){
                break;
            }
            scanf("%d",&z);
            n=max(n,(max(x,y)));
            ++maxZ;
            deg[x]++;
            deg[y]++;
            merge(x,y);
            vec[z]={x,y};
        }
        if(!check(n)){
            puts("Round trip does not exist.");
        }else{
            int k=0;
            for(int i=1;i<=n;++i){
                if(p[i]==i){
                    ++k;
                }
            }
            if(k==1){
                fill(vis,vis+maxZ+1,false);
                dfs(s,maxZ);
                printf("%d",ans[maxZ-1]);
                for(int i=maxZ-2;i>=0;i--){
                    printf(" %d",ans[i]);
                }
                printf("\n");
            }else{
                puts("Round trip does not exist.");
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值