uva 10129
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 N that indicates the number of
plates (1 ≤ N ≤ 100000). Then exactly N lines 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.
这道题的题意是给你一些单词,判断一下他们能不能相连接,两个单词连接的条件是单词连接处的字母要相同。仔细分析一会发现这就是一道判断是否有欧拉路的题。
用到的知识有并查集判断图是否全联通(注意本题图是有向图,但我们当作无向图进行并查集)还有怎样找节点的出度和入度,方法是把一个单词看成一个首字母指向尾字母的边,这样把每个字母看成一个节点,最后只需对26个字母进行欧拉路的判定就好了。大家可以自己脑补一下图。 下面贴代码。
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
int find_f(int x);
void uniona(int x,int y);
int f[26];
int main(int argc, char *argv[])
{
int t,i;
scanf("%d",&t);
while(t--)
{
for(i=0; i<26; i++)
f[i] = i;
int chu[26],ru[26],n;
char ch[100000];
memset(chu,0,sizeof(chu));
memset(ru,0,sizeof(ru));
scanf("%d",&n);
for(i=1; i<=n; i++)
{
scanf(" %s",ch);
char f = ch[0];
char e = ch[strlen(ch)-1];
chu[e-'a'] += 1;
ru[f-'a'] += 1;
uniona(e-'a',f-'a');
}
int num = 0;
for(i=0; i<26; i++)
{
if(chu[i]!=0 || ru[i]!=0)
if(f[i] == i)
num += 1;
}
if(num >= 2)
{
printf("The door cannot be opened.\n");
// printf("不联通\n");
continue;
}
bool flag = true;
num = 0;
for(i=0; i<26; i++)
{
if(chu[i] != ru[i])
{
if(abs(chu[i]-ru[i]) == 1) num += 1;
else
{
flag = false; break;
}
}
}
if(flag)
printf("Ordering is possible.\n");
else
printf("The door cannot be opened.\n");
}
return 0;
}
int find_f(int x)
{
if(f[x] == x)
return x;
return f[x] = find_f(f[x]);
}
void uniona(int x,int y)
{
int ax = f[x];
int ay = f[y];
if(ax != ay)
f[ax] = ay;
}