DFS求解欧拉回路

思路:

  • 利用欧拉定理判断出一个图存在欧拉通路或欧拉回路;
  • 选择一个正确的起始顶点,用DFS遍历所有的边(每条边只能遍历一次),走不通就回溯;
  • 在搜索前进的方向上将遍历过的边按顺序记录下来;
  • 这组边的排列就组成了一条欧拉通路或回路。

参考
欧拉回路原理:https://blog.csdn.net/PacosonSWJTU/article/details/50007847
代码:https://blog.csdn.net/qq_41280600/article/details/103928931

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.

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int max_=2005;
int x,y,z,m,map[50][2005],du[50],cnt,rec[2005],s;
bool vis[2005];
void dfs(int u)
{
	for(int i=1;i<=m;i++)
	{
		if(!vis[i]&&map[u][i])
		{
			vis[i]=true;
			dfs(map[u][i]);
			rec[++cnt]=i;
		}
	}
}
int main()
{
	while(scanf("%d%d",&x,&y),x)
	{
		m=0;
		memset(map,0,sizeof(map));
		memset(du,0,sizeof(du));
		s=min(x,y);//记录起点
		scanf("%d",&z);
		map[x][z]=y;
		map[y][z]=x;//y通过街道z到达x
		du[x]++;
		du[y]++;//记录度数
		int n=max(x,y);//记录最大点数
		m++;//记录边的数目
		 while(scanf("%d%d",&x,&y),x)
		 {
		 	scanf("%d",&z);
		 	map[x][z]=y;
		 	map[y][z]=x;
		 	m++;
		 	du[x]++;
		 	du[y]++;
		 	n=max(x,y);
		 }
		 bool ok=true;
		 for(int i=1;i<=n;i++)
		 {
		 	if(du[i]%2==1)//判断欧拉回路,度数为偶数 
			 {
			 	ok=false;
			 	break;
			  } 
		 }
		 if(ok)
		 {
		 	cnt=0;
		 	memset(vis,false,sizeof(vis));
		 	dfs(s);
		 	for(int i=cnt;i>=1;i--)
		 	{
		 		printf("%d ",rec[i]);
			 }
			 cout<<endl;
		 }
		 else
		  printf("Round trip does not exist.\n") ;
	}
	return 0;
}
发布了22 篇原创文章 · 获赞 6 · 访问量 1706

猜你喜欢

转载自blog.csdn.net/qq_44759750/article/details/103943933