John’s trip(欧拉回路+dfs)
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.
题意: a,b两点由c街道联通,输出字典序最小的欧拉回路
欧拉回路:图联通
无向图 ,所有点的度数都为偶数
有向图, 每个点的入度=出度
思路: 无向图,所以判断度数是否为偶数,联通的话,因为要深搜输出路径,如果不连通,肯定是找不到的,所以不需要再用并查集判断图是否联通,要求字典序最小的话,从小的开始搜,反向输出即可
AC代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
int book[2100],du[2100],n,m;
vector<int>v;
struct pp
{
int l,r;
}p[110000];
int judge()
{
int i;
for(i=1;i<=m;i++)
if(du[p[i].l]%2||du[p[i].r]%2)
return 0;
return 1;
}
void dfs(int x)
{
int i;
for(i=1;i<=m;i++)
{
if((p[i].l==x||p[i].r==x)&&!book[i])
{
book[i]=1;
dfs(p[i].l+p[i].r-x);//搜相邻的点,p[i].l,p[i].r中有一个为x
v.push_back(i);//存路的编号
}
}
}
int main()
{
int i,j,k,a,b,c;
while(~scanf("%d%d",&a,&b)&&(a+b))
{
m=0;
scanf("%d",&c);
memset(book,0,sizeof(book));
memset(du,0,sizeof(du));
v.clear();
p[c].l=a;
p[c].r=b;
du[a]++;
du[b]++;
m=max(m,c);//求最大的路编号
while(~scanf("%d%d",&a,&b)&&(a+b))
{
scanf("%d",&c);
p[c].l=a;
p[c].r=b;
du[a]++;
du[b]++;
m=max(m,c);
}
if(judge())
{
dfs(1);
for(i=v.size()-1;i>0;i--)//反向输出
printf("%d ",v[i]);
printf("%d\n",v[0]);
}
else
printf("Round trip does not exist.\n");
}
return 0;
}