John's trip
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 10546 | Accepted: 3608 | 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.
题意:起点为输入的第一组x,y 最小值,输入x,y,z 分别为顶点x,y和边z(边号),
注意这里要求输出字典序最小的欧拉回路,所以在euler中从小到大遍历边即可得到字典序最小的欧拉回路
注意:
1、stk[] 与vis[] 前者保存欧拉回路的边,后者记录是否访问过,这里其大小应该为边的大小而不是顶点。在这里WA了半天。
2、采用G[x][z] = y 即 G[u][‘边’] = v 的形式 可以采取 对所有边编号遍历一遍的形式 判断是否有临接顶点
code:
#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<cstring>
#include<set>
#include<cmath>
#include<queue>
#include<algorithm>
#define rep(i,j,k) for(int i=j;i<k;++i)
#define mst(a,b) memset((a),(b),sizeof(a))
#include<cstring>
using namespace std;
typedef long long LL;
typedef vector<int,int> pii;
const int MAXN = 50;
const int MAXM = 2005;
int nV,nE,top;
int stk[MAXM];
int deg[MAXN];
bool vis[MAXM];
int G[MAXN][MAXM];
void init(){
top = nV = nE = 0;
mst(vis,false);
mst(G,0);
mst(deg,0);
}
void euler(int u){
for(int i = 1; i <= nE; ++i){
if(!vis[i] && G[u][i]){
vis[i] = true;
euler(G[u][i]);
stk[++top] = i;
}
}
}
int main()
{
int x,y,z,st;
while(scanf("%d%d",&x,&y) && x!=0 && y != 0){
init();
st = (int)min(x,y);
scanf("%d",&z);
nE = (int)max(z,nE);
nV = (int)max((int)max(x,y),nV);
++deg[x];
++deg[y];
G[x][z] = y;
G[y][z] = x;
while(scanf("%d%d",&x,&y) && x&& y){
scanf("%d",&z);
nE = max(z,nE);
nV = max((int)max(x,y),nV);
++deg[x];
++deg[y];
G[x][z] = y;
G[y][z] = x;
}
//int odd = 0;
//rep(i,1,nV+1) if(deg[i]&1) ++odd;
bool flag = true;
for(int i = 1; i <= nV; i++)
if(deg[i] & 1)
flag = false;
if(!flag){
printf("Round trip does not exist.\n");
}
else{
euler(st);
printf("%d", stk[top]);
for(int i = top - 1; i > 0; i--)
printf(" %d", stk[i]);
// printf("%d",stk[top-1]);
// for(int i = top-2; i >= 0; --i) printf(" %d",stk[i]);
printf("\n");
}
}
return 0;
}