码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 10129 Play on Words

时间:2014-07-06 12:12:36      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:uva   acm   graph   euler   

题目如下:

Play on Words

Some of the secret doorscontain a very interesting word puzzle. The team of archaeologists has tosolve 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 largenumber of magnetic plates on every door. Every plate has one word written onit. The plates must be arranged into a sequence in such a way that every wordbegins with the same letter as the previous word ends. For example, the word``acm‘‘ can be followed by the word ``motorola‘‘. Your task is towrite a computer program that will read the list of words and determine whetherit is possible to arrange all of the plates in a sequence (according tothe given rule) and consequently to open the door.

Input Specification

The inputconsists of T test cases. The number of them (T) is givenon the first line of the input file. Each test case begins with a linecontaining a single integer number Nthat indicates the numberof plates (1 <= N <= 100000). Then exactly Nlinesfollow, each containing a single word. Each word contains at least two andat most 1000 lowercase characters, that means only letters ‘a‘through ‘z‘ will appear in the word. The same word mayappear several times in the list.

Output Specification

Yourprogram has to determine whether it is possible to arrange all the plates ina sequence such that the first letter of each word is equal to the lastletter of the previous word. All the plates from the list must be used, eachexactly once. The words mentioned several times must be used that number oftimes.

If there exists such anordering of plates, your program should print the sentence "Ordering is possible.". Otherwise, output the sentence "The door cannot be opened.".

Sample Input

32acmibm3acmmalformmouse2okok

Output for the SampleInput

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


这道题刚开始没发现是欧拉路,直接DFS,果断超时了TAT,然后改成欧拉回路,有没注意到要连通还有欧拉路可以只有一个节点是奇节点,WA了两次TAT,改过来后AC了。

AC的代码如下:


UVA 10129 Play on Words,布布扣,bubuko.com

UVA 10129 Play on Words

标签:uva   acm   graph   euler   

原文地址:http://blog.csdn.net/u013840081/article/details/36896687

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!