Play on Words(POJ 1386)

题目链接

题目描述

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.

输入格式

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.

输出格式

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.”.

输入样例

3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok

输出样例

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

提示/说明

分析

题目大意是一共有t组测试,每组测试都包含一个数字n和n个碟子,其中每个碟子上面都有一个单词。现在要求进行单词接龙,两个单词连接的条件是前一个单词的尾字母和后一个单词的尾字母相同,问是否可用将所有单词都接起来。如果可以就输出"Ordering is possible.",否则就输出“The door cannot be opened.”。
显然这是一个有向图欧拉路是否存在的判断,每一个单词都是一条由首字母指向尾字母的边,因此以小写英文字母为顶点,统计其出入度判断即可。

欧拉通路/回路的判定

有向图

  • 欧拉通路:图是连通的,图中只有两个奇度点,分别是欧拉通路的两个端点。对于欧拉通路,除起点、终点外,每个点如果进入,显然一定要出去,因此都是偶点。
  • 欧拉回路:图是连通的,点均为偶度点。对于欧拉回路,每个点进入、出去的次数相等,因此没有奇点。

无向图

  • 欧拉通路:图是连通的,除两顶点外其余点的入度等于出度,且这两个顶点中,一个顶点入度比出度大1(起点),另一个入度比出度小1(终点)
  • 欧拉回路:图是连通的,图中所有点的入度等于出度。

源程序

//#include <bits/stdc++.h>	//poj不能用万能头
#include <cstdio>
#include <cstring>
#include <cstdlib>
#define MAXN 1005
using namespace std;
char str[MAXN];
int t,n,in[30],out[30],father[30];
int find(int x)
{
	if(x==father[x])return x;
	return father[x]=find(father[x]);
}
int Union(int x,int y)
{
	x=find(x);
	y=find(y);
	if(x!=y)father[x]=y;
}
int main()
{
	scanf("%d",&t);	//测试组数 
	while(t--){
		for(int i=0;i<26;i++){	//初始化 
			in[i]=out[i]=0;
			father[i]=i;
		}
		scanf("%d",&n);
		for(int i=1;i<=n;i++){	//统计度数并更新并查集 
			scanf("%s",str);
			int len=strlen(str);
			int x=str[0]-'a',y=str[len-1]-'a';
			out[x]++;	 
			in[y]++;
			Union(x,y);
		}
		int cnt=0;	//统计连通分量 
		for(int i=0;i<26;i++)
			if((in[i]||out[i])&&i==father[i])cnt++;
		if(cnt>1)
			printf("The door cannot be opened.\n");
		else{
			bool flag=false;
			int s=0,t=0; //统计起点和终点个数 
			for(int i=0;i<26;i++){
				if(in[i]==out[i])continue;
				else if(out[i]==in[i]+1) s++;	//起点数增加 
				else if(in[i]==out[i]+1) t++;	//终点数增加 
				else {		//不存在欧拉路 
					flag=1;
					break; 
				}
			}
			if(!flag&&(s==0&&t==0||s==1&&t==1))printf("Ordering is possible.\n");
			else printf("The door cannot be opened.\n");
			
		} 
	}
}
发布了19 篇原创文章 · 获赞 0 · 访问量 136

猜你喜欢

转载自blog.csdn.net/weixin_43960284/article/details/105188623