HDU 1116 Play on Words(并查集+欧拉通路)

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



acm 
ibm 

acm 
malform 
mouse 

ok 
ok

Sample Output

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

题意:给你几个单词,如果所有的单词收尾能够连成一串(不考虑输入的顺序),门就能打开。

我一开始看也并没有思路,可能还是做的题不够多了,被指出用并查集后才能慢吞吞的一步步的想出是提出单词的首和尾,因为中间的部分根本没有用。并查集的父数组表示26个字母,每次输入把收尾并起来,最后通过判断树的个数、以及收尾顶点的度数进一步判断能否连成欧拉通路。

附上AC代码

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int f[30],in[30],out[30];
int book[30],p[30];
void init()
{
	for(int i=1;i<=26;i++)
	 f[i]=i;
}
int getf(int x)
{
	if(f[x]==x) return x;
	else
	{
		f[x]=getf(f[x]);
		return f[x];
	}
}
void merge(int a,int b)
{
	int t1,t2;
	t1=getf(a);t2=getf(b);
	if(t1!=t2)
	f[t2]=t1;
}
int main()
{
	int t,n;
	int i,a,b,k;
	char s[1005];
	scanf("%d",&t);
	while(t--)
	{
		init();
		memset(in,0,sizeof(in));
		memset(out,0,sizeof(out));
		memset(p,0,sizeof(p));
		memset(book,0,sizeof(book));	
		scanf("%d",&n);
		for(i=1;i<=n;i++)
		{
			scanf("%s",&s);
			a=s[0]-'a'+1;
			b=s[strlen(s)-1]-'a'+1;
			merge(a,b);
			in[b]++;   //出度入度都分别加一 
			out[a]++;
			book[a]=book[b]=1;  //book记录这个点已经在路径中 
		}
	int count=0;   //count记录分支数量 
	for(i=1;i<=26;i++)
	{
		if(book[i]==1&&getf(i)==i) count++;
	}
	if(count>1)         //存在多个分支,肯定不符合题意 
	{
		printf("The door cannot be opened.\n");
		continue;
	}
	k=0;
	for(i=1;i<=26;i++)
	{
		if(book[i]&&in[i]!=out[i])
		p[k++]=i;                 //p数组表示首节点或者尾节点的字母是第几个 ,后面会用到 
	}
	if(k==0)                             //每个首尾节点的度数都是偶数,说明这是一个欧拉回路,肯定满足题意 
	{
		printf("Ordering is possible.\n");
		continue;
	}
	
	if(k==2&&((out[p[0]]-in[p[0]]==1 && in[p[1]]-out[p[1]]==1)      //入度与出度不相等的节点个数为2
        || (out[p[1]]-in[p[1]]==1 && in[p[0]]-out[p[0]]==1)))      //这说明不是回路只是一个通路,那么首尾节点的出度和入度相差1 
        {
        	printf("Ordering is possible.\n");
        	continue;
		}
	printf("The door cannot be opened.\n");
	}
	return 0;
 } 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值