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.
题意:给了很多个单词,判断能否像成语接龙那般把所有单词都用上,每个单词只能用一次,且无先后顺序。
思路:我们可以把单词的两端da根据当成图的两个端点,那么本题就是判断是否存在欧拉通路。用并查集结合欧拉图的特性,可知只有一个祖先,且最多只有两个点的入度,出度不一样,且差值为1.
代码如下:
#include<cstdio>
#include<cstring>
int f[110],in[33],out[33],book[33];
int getf(int x)
{
if(x==f[x])
return x;
return f[x]=getf(f[x]);
}
void init()
{
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
memset(book,0,sizeof(book));
for(int i=0; i<30; i++)
f[i]=i;
}
int main()
{
int t,n;
scanf("%d",&t);
int as=t;
while(t--)
{
init();
char str[1100];
scanf("%d",&n);
for(int i=0; i<n; i++)
{
scanf("%s",str);
int l=strlen(str);
int t1=getf(str[0]-'a');
int t2=getf(str[l-1]-'a');
if(t1!=t2) //并查集
f[t2]=t1;
in[str[0]-'a']++;//入度+1
out[str[l-1]-'a']++; //出度+1
book[str[0]-'a']=book[str[l-1]-'a']=1; //这个字母出现过
}
int a=0,b=0,ans=0,flag=1,mark=1;
for(int i=0; i<26; i++)
{
if(book[i]) //出现过的字母
{
if(f[i]==i)//找到祖先
ans++;
if(in[i]!=out[i])//入度和出度不等
{
if(in[i]-out[i]==1) //入度和出度之差为1
a++;
else if(out[i]-in[i]==1) //出度和入度之差为1
b++;
else //不满足条件
mark=0;
}
if(ans>1||!mark)
{
flag=0;
break;
}
}
}
if(flag&&mark)
{
if((!a&&!b)||(a==1&&b==1))
printf("Ordering is possible.\n");
else
printf("The door cannot be opened.\n");
}
else
printf("The door cannot be opened.\n");
}
}