POJ
来源:互联网 发布:苍老师最经典 知乎 编辑:程序博客网 时间:2024/06/05 20:48
点击打开题目链接
John's trip
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8998 Accepted: 3018 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
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 12 3 23 1 61 2 52 3 33 1 40 01 2 12 3 21 3 32 4 40 00 0
Sample Output
1 2 3 5 4 6 Round trip does not exist.
Source
Central Europe 1995
[Submit] [Go Back] [Status] [Discuss]
今天刚开始学习欧拉路径,以为只是并查集加一个判断奇度的问题,现在发现那只是开始(TAT)
题目大意:约翰想去他所有朋友家玩一遍,希望所走路径是一条欧拉回路(路径按照字典序最小,所有路径只走一次,从家出发,最后到家)输出最小字典序路径。
第一次输入x和y中小者为约翰家的点。
首先判断是否符合欧拉回路,即是否存在奇度点。然后用map数组存点边关系(map[i][j]为从点i出发经j路径所能到达的点),dfs求路径。然后反向输出即可。
附上AC代码:
#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>using namespace std;int _map[45][2000];int vis[2000];int street[2000];int degree[45];int str_cnt;int str_top;int s,e,w;int home;int maxd;int flag;void dfs_euler(int n){ for(int i=1;i<=str_cnt;i++) { if(!vis[i]&&_map[n][i]) { vis[i]=1; dfs_euler(_map[n][i]); street[str_top++]=i; } }}int main(){ ios::sync_with_stdio(false);// freopen("in.txt","r",stdin); while(cin>>s>>e,s) { memset(_map,0,sizeof(_map)); memset(vis,0,sizeof(vis)); memset(degree,0,sizeof(degree)); memset(street,0,sizeof(street)); home=min(s,e); maxd=max(s,e); flag=1; str_cnt=str_top=0; do { cin>>w; degree[s]++; degree[e]++; _map[s][w]=e; _map[e][w]=s; str_cnt++; maxd=max(maxd,max(s,e)); }while(cin>>s>>e,s); for(int i=1;i<=maxd;i++) { if(degree[i]%2){flag=0;break;} } if(flag==0)cout<<"Round trip does not exist."<<endl; else { dfs_euler(home); for(int i=str_top-1;i>=0;i--) { if(i!=0) cout<<street[i]<<' '; else cout<<street[i]<<endl; } } } return 0;}
0 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- Android客户端连接服务器端,向服务器端发送请求HttpURLConnection
- Kafka入门理论学习
- laravel框架中所用到的依赖注入
- 电脑小结:怎样一个软件是32位还是64位
- ionic2实现通讯录,联系人搜索功能
- POJ
- 洛谷 P3708 koishi的数学题
- Android 修改拨号音尖锐问题,降级拨号音量修改
- Machine Learning第三讲[Logistic回归] --(四)解决过拟合问题
- 用服务器请求数据,解析数据,和使用Handler
- 常用的Selector
- 使用webpack打包的后,公共请求路径的配置问题
- 最小生成树—prime算法
- 附件上传