【POJ 1386】Play on Words

【题目】

传送门

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.

【分析】

这道题一开始没思路,后来在老师的讲解下就懂了

我们不把单词当做节点,而是把字母当成节点,每个单词的第一个字母向最后一个字母连边

这样的话,很容易知道,若连出来的图是一条欧拉路,就满足条件,反之不行

对于有向图判断欧拉路,我们用这几点判断:

  1. 基图是连通的
  2. 除起始点外,每个点的初度等于入度
  3. 起点的入度比出度小 1,终点的入度比出度大 1

【代码】

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 30
#define M 100005
#define L 1005
#define wrong {printf("The door cannot be opened.\n");continue;}
using namespace std;
char s[L];
bool vis[N];
int in[N],out[N],father[N];
int find(int x)
{
	if(father[x]!=x)
	  father[x]=find(father[x]);
	return father[x];
}
void merge(int x,int y)
{
	x=find(x);
	y=find(y);
	father[x]=y;
}
int main()
{
	int n,i,j,t;
	scanf("%d",&t);
	while(t--)
	{
		bool f=true;
		memset(in,0,sizeof(in));
		memset(out,0,sizeof(out));
		memset(vis,false,sizeof(vis));
		scanf("%d",&n);
		for(i=1;i<=26;++i)
		  father[i]=i;
		for(i=1;i<=n;++i)
		{
			scanf("%s",s+1);
			int x=s[1]-'a'+1;
			int y=s[strlen(s+1)]-'a'+1;
			out[x]++;in[y]++;merge(x,y);
			vis[x]=vis[y]=true;
		}
		for(i=1;i<=26;++i)
		  for(j=1;j<=26;++j)
		    if(vis[i]&&vis[j]&&find(i)!=find(j))
		      f=false;
		if(!f)  wrong;
		int num1=0,num2=0;
		for(i=1;i<=26;++i)
		{
			if(in[i]==out[i])  continue;
			if(abs(in[i]-out[i])>1)  f=false;
			if(in[i]==out[i]-1)  num1++;
			if(in[i]==out[i]+1)  num2++;
		}
		if(!(num1==0&&num2==0||num1==1&&num2==1))  f=false;
		if(!f)  wrong;
		printf("Ordering is possible.\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/forever_dreams/article/details/82947993