HDOJ 1116-Play on Words【欧拉路径+欧拉回路+并查集】

Play on Words

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6898    Accepted Submission(s): 2335


Problem Description
Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzzle is very important for us.

There is a large number of magnetic plates on every door. Every plate has one word written on it. The plates must be arranged into a sequence in such a way that every word begins with the same letter as the previous word ends. For example, the word ``acm'' can be followed by the word ``motorola''. Your task is to write a computer program that will read the list of words and determine whether it is possible to arrange all of the plates in a sequence (according to the given rule) and consequently to open the door.
 

Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing a single integer number Nthat indicates the number of plates (1 <= N <= 100000). Then exactly Nlines follow, each containing a single word. Each word contains at least two and at most 1000 lowercase characters, that means only letters 'a' through 'z' will appear in the word. The same word may appear several times in the list.
 

Output
Your program has to determine whether it is possible to arrange all the plates in a sequence such that the first letter of each word is equal to the last letter of the previous word. All the plates from the list must be used, each exactly once. The words mentioned several times must be used that number of times.
If there exists such an ordering of plates, your program should print the sentence "Ordering is possible.". Otherwise, output the sentence "The door cannot be opened.".
 

Sample Input
  
  
3 2 acm ibm 3 acm malform mouse 2 ok ok
 

Sample Output
  
  
The door cannot be opened. Ordering is possible. The door cannot be opened.
 

Source
 解题思路:
就是将每个单词的首字母和尾字母,看成起点和终点的有向线段,我们要找出欧拉回路,或欧拉通路。欧拉回路就是每个点的出度和入度都是相等的,欧拉通路就是

只有两个端点出度入度不等且其中一个出度减去入度为1,另一个入度减出度为1。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define max 30
using namespace std;
char str[1000];
int in[max],out[max],p[max];
int set[max],vis[max];
int find(int x)
{
	int cc=x;
	int t;
	while(x!=set[x])
	{
		x=set[x];
	}
	while(cc!=x)
	{
		t=set[cc];
		set[cc]=x;
		cc=t; 
	}
	return x; 
}
void join(int x,int y)
{
	int xx=find(x);
	int yy=find(y);
	if(xx!=yy)
	{
		set[xx]=yy;
	} 
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		scanf("%d",&n);
		int i,j;
		for(i=1;i<=26;i++)
		{
			set[i]=i;
			vis[i]=0;
			in[i]=out[i]=p[i]=0;
		}
		for(i=0;i<n;i++)
		{
			int x,y;
			scanf("%s",str);
			int ll=strlen(str);
			x=str[0]-'a'+1;
			y=str[ll-1]-'a'+1;
			in[y]++;
			out[x]++;
			vis[x]=vis[y]=1;
			join(x,y);
		}
		int ex=0;
		for(i=1;i<=26;i++)
		{
			if(vis[i]&&set[i]==i)
			{
				ex++;
				if(ex>1)
				{
					break;
				}
			}
		}
		if(ex>1)
		{
			printf("The door cannot be opened.\n");
            continue;
		}
		int sum=0;
		for(i=1;i<=26;i++)
		{
			if(vis[i]&&in[i]!=out[i])
			{
				p[sum++]=i;
			}
		}
		if(sum==0)
		{
			printf("Ordering is possible.\n");
		}
		else if(sum==2&&(out[p[0]]-in[p[0]]==1&&in[p[1]]-out[p[1]]==1||in[p[0]]-out[p[0]]==1&&out[p[1]]-in[p[1]]==1))
		{
			printf("Ordering is possible.\n");
		}
		else
		{
			printf("The door cannot be opened.\n");
		}
	}
	return 0;
} 


Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值