Play on Words
Time Limit: 10000/5000 MS(Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3754 Accepted Submission(s): 1204
Problem Description
Some of the secret doors contain a very interesting wordpuzzle. The team of archaeologists has to solve it to open that doors. Becausethere 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 oneword written on it. The plates must be arranged into a sequence in such a waythat every word begins with the same letter as the previous word ends. Forexample, the word ``acm'' can be followed by the word ``motorola''. Your taskis to write a computer program that will read the list of words and determinewhether it is possible to arrange all of the plates in a sequence (according tothe 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 aline containing a single integer number Nthat indicates the number of plates (1<= N <= 100000). Then exactly Nlines follow, each containing a singleword. 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 wordmay appear several times in the list.
Output
Your program has to determine whether it is possible toarrange all the plates in a sequence such that the first letter of each word isequal to the last letter of the previous word. All the plates from the listmust be used, each exactly once. The words mentioned several times must be usedthat number of times.
If there exists such an ordering of plates, your program should print thesentence "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
CentralEurope 1999
题目解析:
输入一些英文单词,根据该单词的首尾字母,判断所有单词能不能连成一串,类似于成语接龙的意思。同样如果有多个重复的单词时,也必须满足这样的条件才能通过,否则都是不可能的情况。输入包括若干个案例,每个案例中最多有100000个单词。
思路解析:
该题所涉及的知识点主要是并查集和欧拉回路,那么什么是并查集呢?所谓的并查集就是指“并”和“查”的集合,具体大家可以结合HDU畅通工程来理解,当然,在那里会有相关的解析。(http://blog.csdn.net/caizi1991/article/details/8995793)现在我们来着重看一下欧拉回路的知识及其应用背景,那么,欧拉回路又是什么呢?它跟我们今天所做的“成语接龙”有什么联系呢?学过离散的同学都知道,欧拉回路是指,在图中,经过所有的边一次且仅一次的回路就是欧拉回路。而经过所有的顶点一次且仅一次的回路叫做哈密顿回路。现在我们考虑每个单词有用的成分只有首尾字母,那么我们把单词的首尾字母提取出来,抽象成两个顶点,而恰恰这两个顶点又是有了联系的,我们把它们放入一个集合。实际上,无论输入多少个单词,首尾字母中所出现的字母无外乎a,b,c……y,z等26个英文字母。所以反客为主,我们不用被动的根据输入的单词来变换,首先建立26个集合,将有联系的集合合并(merge)则矣。当做完这个工作后,我们再来看看欧拉回路存在性的判定定理:
一、无向图
每个顶点的度数都是偶数,则存在欧拉回路。
二、有向图(所有边都是单向的)
每个节顶点的入度都等于出度,则存在欧拉回路。
三.混合图欧拉回路
混合图欧拉回路用的是网络流。
把该图的无向边随便定向,计算每个点的入度和出度。如果有某个点出入度之差为奇数,那么肯定不存在欧拉回路。因为欧拉回路要求每点入度 = 出度,也就是总度数为偶数,存在奇数度点必不能有欧拉回路。
好了,到这里,这道题也就解决了,相信大家对并查集及欧拉回路又有了更深刻的理解。
#include
#include
#define MAX 26
using namespace std;
int out[MAX],in[MAX],flag[MAX],p[MAX],father[MAX];
int Find(int x) //查找当前节点的祖先(根)
{
if(father[x]!=x)
father[x]=Find(father[x]);
return father[x];
}
void Merge(int a,int b) //如果这两个节点不同根,则令a的祖先指向b,从而建立同源树
{
int p=Find(a);
int q=Find(b);
if(p!=q)
father[q]=p;
}
int main()
{
int i,k,count,a,b,ncase,n;
char str[1002];
cin>>ncase; //输入案例个数
while(ncase--)
{
for(i=0;i<26;i++)
father[i]=i; //对26个字母进行初始化
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
memset(flag,0,sizeof(flag));
cin>>n;
for(i=1;i<=n;i++)
{
cin>>str;
a=str[0]-'a'; //用0,1,2...26分别表示26个英文字母,a,b分别表示各个单词首尾字母
b=str[strlen(str)-1]-'a';
Merge(a,b); //将a,b合并
in[b]++; //入度和出度分别加1
out[a]++;
flag[a]=flag[b]=1; //标记该字母已存在
}
count=0;
for(i=0;i<26;i++)
{
father[i]=Find(i);
if(flag[i] && father[i]==i) //如果不同源,则非同根
count++; //计算连通分支个数
}
if(count>1) //非连通图,直接输出
{
cout<<"The door cannot be opened."<