Description
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
Output
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<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=4000;
struct edge
{
int v,id;
int next;
};
int p[maxn];
int l;
edge G[maxn];
void init()
{
memset(p,-1,sizeof(p));
l=0;
}
void addedge(int u,int v,int id)//按照id排序
{
G[l].v=v;
G[l].id=id;
if(p[u]==-1)
{
G[l].next=p[u];
p[u]=l++;
return ;
}
int pre=-1;
for(int i=p[u];i!=-1;i=G[i].next)
{
if(G[i].id>id)
{
break;
}
pre=i;
}
if(pre==-1)
{
G[l].next=p[u];
p[u]=l++;
return ;
}
G[l].next=G[pre].next;
G[pre].next=l++;
}
int du[maxn];
int ans[maxn];
int cur;
int used[maxn];
void dfs(int x,int cas)
{
for(int i=p[x];i!=-1;i=G[i].next)
{
if(!used[G[i].id])
{
used[G[i].id]=1;
dfs(G[i].v,G[i].id);
}
}
if(cas!=-1) ans[cur++]=cas;
}
void print()
{
for(int i=cur-1;i>=0;i--)
{
printf("%d ",ans[i]);
}printf("\n");
}
int main()
{
int x,y;
while(scanf("%d%d",&x,&y)==2&&x)
{
init();
memset(du,0,sizeof(du));
int start,z;scanf("%d",&z);
int maxnum=-1;
start=min(x,y);
addedge(x,y,z);
addedge(y,x,z);
du[x]++,du[y]++;
maxnum=max(x,y);
while(scanf("%d%d",&x,&y)==2&&x)
{
scanf("%d",&z);
addedge(x,y,z);
addedge(y,x,z);
du[x]++,du[y]++;
maxnum=max(maxnum,max(x,y));
}
int flag=1;
for(int i=1;i<=maxnum;i++)
{
if(du[i]&1)
{
flag=0;break;
}
}
if(!flag)
{
printf("Round trip does not exist.\n");
continue;
}
memset(used,0,sizeof(used));
cur=0;
dfs(start,-1);
print();
}
return 0;
}