欧拉图的路径输出

My little sister had a beautiful necklace made of colorful beads. Two successive beads in the necklace
shared a common color at their meeting point. The figure below shows a segment of the necklace:
But, alas! One day, the necklace was torn and the beads were all scattered over the floor. My sister
did her best to recollect all the beads from the floor, but she is not sure whether she was able to collect
all of them. Now, she has come to me for help. She wants to know whether it is possible to make a
necklace using all the beads she has in the same way her original necklace was made and if so in which
order the bids must be put.
Please help me write a program to solve the problem.
Input
The input contains T test cases. The first line of the input contains the integer T.
The first line of each test case contains an integer N (5 ≤ N ≤ 1000) giving the number of beads
my sister was able to collect. Each of the next N lines contains two integers describing the colors of a
bead. Colors are represented by integers ranging from 1 to 50.
Output
For each test case in the input first output the test case number as shown in the sample output. Then
if you apprehend that some beads may be lost just print the sentence “some beads may be lost”
on a line by itself. Otherwise, print N lines with a single bead description on each line. Each bead
description consists of two integers giving the colors of its two ends. For 1 ≤ i ≤ N1, the second integer
on line i must be the same as the first integer on line i + 1. Additionally, the second integer on line
N must be equal to the first integer on line 1. Since there are many solutions, any one of them is
acceptable.
Print a blank line between two successive test cases.
Sample Input
2
5
1 2
2 3
3 4
4 5
5 6
5
2 1
2 2
3 4
3 1
2 4
Sample Output
Case #1
some beads may be lost
Case #2
2 1
1 3
3 4
4 2
2 2

**
思路:由于是一个单环,所以各点的的度为2,而且在对进行一个点进行深搜时会把所有点搜完,所以用这两个条件来判断是否给的数据是正确的,路径输出是模板

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=55;
int book[N],d[N],q[N][N];
int n;

void dfs(int a)		
{
	book[a]=1;
	for(int i=1;i<=50;i++)
	{
		if(q[a][i]&&!book[i])
			dfs(i);
	}
}

void shuchu(int a)			//路径输出模板
{
	for(int i=1;i<=50;i++)
	{
		if(q[a][i])
		{
			q[a][i]--;
			q[i][a]--;
			shuchu(i);
			printf("%d %d\n",i,a);
		}
	}
}

int main()
{
	int T,c=0;
	cin>>T;
	
	while(T--)
	{
		memset(book,0,sizeof(book));
		memset(d,0,sizeof(d));
		memset(q,0,sizeof(q));	//日常初始化
		
		cin>>n;
		for(int i=0;i<n;i++)
		{
			int a,b;
			cin>>a>>b;
			d[a]++;			//把入度和出度 放在一起了,一个点只有一个入度和一个出度
			d[b]++;
			q[a][b]++;		//由于可能出现相同的颜色数据,所以++;
			q[b][a]++;		//相连的颜色可以调换顺序
		}
		int flag=0;
		for(int i=1;i<=50;i++)
		{
			if(d[i]%2==1){
				flag=1;
				break;
			}
		}				//判断是否有奇数度的,不可能是正确的
		printf("Case #%d\n",++c);
		if(!flag)
		{
			int flag1=0;
			for(int i=1;i<=50;i++)
			{
				if(d[i]){
					dfs(i);
					break;
				}
			}			//对一个点进行搜索,由于只有一个环,所以会搜完所有的点
			for(int i=1;i<=50;i++)
			{
				if(d[i]&&!book[i]){
					flag1=1;
					break;	
				}
			}		//没有搜完,说明不是正确的
			if(!flag)
			{
				for(int i=1;i<=50;i++)
				{
					if(d[i]){
						shuchu(i);
						break;
					}
				}	//进行路径输出
				
			}
			else printf("some beads may be lost\n");
		}
		else printf("some beads may be lost\n");
		if(T)puts("");
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

红和黑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值