John's trip
Time Limit: 1000MS | | Memory Limit: 65536K | ||
Total Submissions: 10564 | | Accepted: 3617 | | 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.
Source
Central Europe 1995
算法分析:
题意:
给你一个无向图,数据格式如点x 点y 边Z(编号),表示由x点和y点构成了Z边。现在要问你该图中是否存在欧拉回路,如果存在,输出那条欧拉回路(输入按序走过的所有边标号)。且题目中保证了该无向图是连通的。
实现:
1.判断是否为欧拉回路:无向连通图的所有节点是不是都是偶数度,如果不是则不存在欧拉回路.
2.如何输出:直接建图,把边的编号当作权值就ok,可以实现字典序。
代码实现:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<set>
#include<vector>
#include<sstream>
#include<queue>
#define ll long long
#define PI 3.1415926535897932384626
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=6000;
struct Edge
{
int to,next,id;
bool flag;
}edge[maxn];
int head[maxn];
int in[maxn];
int tot;
void addedge(int u,int v,int w)
{
edge[tot].to=v;
edge[tot].next=head[u];
edge[tot].id=w;
edge[tot].flag=false;
head[u]=tot++;
}
int start;
vector<int>ans;
void init()
{
tot=0;
memset(head,-1,sizeof(head));
memset(in,0,sizeof(in));
}
void dfs(int x)
{
for(int i=head[x];i!=-1;i=edge[i].next)
{
if(!edge[i].flag)
{
edge[i].flag=true;
edge[i^1].flag=true;
dfs(edge[i].to);
ans.push_back(i);//记录边的号码
}
}
}
int main()
{
int x,y,c;
while(scanf("%d%d",&x,&y))
{
if(x==0&&y==0)break;
init();
scanf("%d",&c);
addedge(x,y,c);
addedge(y,x,c);
in[x]++;
in[y]++;
start=min(x,y);
while(scanf("%d%d",&x,&y))
{
if(x==0&&y==0)break;
scanf("%d",&c);
addedge(x,y,c);
addedge(y,x,c);
in[x]++;
in[y]++;
}
int flag=1;
for(int i=1;i<=maxn;i++) //判断是否为欧拉回路
{
if(in[i]%2!=0){
flag=0;
break;
}
}
if(flag==0)
{
puts("Round trip does not exist.");
continue;
}
ans.clear();
dfs(start);
for(int i=0;i<ans.size();i++)
{
if(i==0)
printf("%d",edge[ans[i]].id);
else
printf(" %d",edge[ans[i]].id);
}
cout<<endl;
}
return 0;
}