Play on Words
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 11312   Accepted: 3862

Description

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.

Source

 
分析:

这一题是典型的欧拉道路题目。  欧拉道路的定义是: 除了起点和终点外, 其他点的“进出” 次数应该相等。 换句话说,除了起点和终点外, 其他点的度数应该是偶数。

对于有向图, 则必须其中一个点的出度恰好比入度大1, 另一个的入度比出度大。

如果奇点数不存在的话, 则可以从任意点出发,最终一定会回到该点(成为欧拉回路)。

题目给的单词量比较大,但是有用的只有首和尾的字母,所以只需要存首尾字母就可以了。

欧拉道路还有关键的一部是判断这一个图是连通的, 并且只有一个一个连通分支。

代码:

#include<cstdio>
#include<cstring>
using namespace std;
#define N 27
struct node{
int in,out;
}degree[N];
int fa[N],rank[N],mem[N],vis[N],top,t,n;
char str[];
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
int main(){
scanf("%d",&t);
while(t--){
for(int i=;i<=;i++){
fa[i]=i,rank[i]=;
}
memset(degree,,sizeof degree);
memset(vis,,sizeof vis);
top=;
scanf("%d",&n);
for(int i=;i<=n;i++){
memset(str,,sizeof str);
scanf("%s",str);
int a=str[]-'a'+,b=str[strlen(str)-]-'a'+;
if(!vis[a]){
vis[a]=;
mem[++top]=a;
}
if(!vis[b]){
vis[b]=;
mem[++top]=b;
}
degree[a].out++;degree[b].in++;
a=find(a);b=find(b);
if(a!=b){
if(rank[a]<rank[b]) fa[a]=b;
else{
fa[b]=a;
if(rank[a]==rank[b]) rank[a]++;
}
}
}
int tmp=find(mem[]),flag=;
for(int i=;i<=top;i++){
if(find(mem[i])!=tmp){
flag=;break;
}
}
if(flag){
printf("The door cannot be opened.\n");
continue;
}
int sum=,flag1=,flag2=,ok=;
for(int i=;i<=top&&sum<=&&ok;i++){
if(degree[mem[i]].in!=degree[mem[i]].out){
sum++;
if(degree[mem[i]].in==degree[mem[i]].out+) flag1++;
else if(degree[mem[i]].in==degree[mem[i]].out-) flag2++;
else ok=;
}
}
if(ok){
if(flag1==&&flag2== || flag1==&&flag2==)
printf("Ordering is possible.\n");
else
printf("The door cannot be opened.\n");
}
else
printf("The door cannot be opened.\n");
}
return ;
}

poj 1386的更多相关文章

  1. POJ 1386 Play on Words(欧拉图的判断)

    Play on Words Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11838   Accepted: 4048 De ...

  2. poj 1386 Play on Words 有向欧拉回路

    题目链接:http://poj.org/problem?id=1386 Some of the secret doors contain a very interesting word puzzle. ...

  3. poj 1386 Play on Words(有向图欧拉路+并查集)

    题目链接:http://poj.org/problem?id=1386 思路分析:该问题要求判断单词是否能连接成一条直线,转换为图论问题:将单词的首字母和尾字母看做一个点,每个单词描述了一条从首字母指 ...

  4. poj 1386 Play on Words门上的单词【欧拉回路&&并查集】

    题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...

  5. POJ 1386 Play on Words(欧拉路)

    http://poj.org/problem?id=1386 题意: 给出多个单词,只有单词首字母与上一个单子的末尾字母相同时可以连接,判断所有字母是否可以全部连接在一起. 思路: 判断是否存在欧拉道 ...

  6. POJ 1386 Play on Words(单词建图+欧拉通(回)路路判断)

    题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...

  7. [POJ 1386] Play on Words

    [题目链接] http://poj.org/problem?id=1386 [算法] 将每个单词的首字母向尾字母连一条有向边,判断欧拉路径是否存在,即可 [代码] #include <algor ...

  8. [欧拉回路] poj 1386 Play on Words

    题目链接: http://poj.org/problem?id=1386 Play on Words Time Limit: 1000MS   Memory Limit: 10000K Total S ...

  9. POJ 1386&&HDU 1116 Play on Words(我以后再也不用cin啦!!!)

    Play on Words Some of the secret doors contain a very interesting word puzzle. The team of archaeolo ...

  10. poj 1386 Play on Words(有向图欧拉回路)

    /* 题意:单词拼接,前一个单词的末尾字母和后一个单词的开头字母相同 思路:将一个单词的开头和末尾单词分别做两个点并建一条有向边!然后判断是否存在欧拉回路或者欧拉路 再次强调有向图欧拉路或欧拉回路的判 ...

随机推荐

  1. 求子数组的最大和要求O(n)

    //求子数组的最大和 //输入一个整形数组.有整数也有负数,数组中连续一个或多个子数组,每一个子数组都有一个和,求全部子数组的和的最大值,要求时间复杂度O(n) #include<iostrea ...

  2. Mockito 相关资料

    https://monkeyisland.pl/2008/04/26/asking-and-telling/ http://qiuguo0205.iteye.com/blog/1456528 http ...

  3. mysql root 用户被删

    [root@M ~]# vi /etc/my.cnf [mysqld] skip-grant-tables [root@M ~]# service mysqld restart Shutting do ...

  4. Apple ID地区怎么改为美国?(转载)

    Apple ID地区怎么改为美国?有时候我们想要去App Store中下载国外APP的话,就可以尝试将Apple ID地区改为美国,然后再打开App Store,就可以切换到美国应用市场,下载国外AP ...

  5. FreeRTOS 二值信号量,互斥信号量,递归互斥信号量

    以下转载自安富莱电子: http://forum.armfly.com/forum.php 本章节讲解 FreeRTOS 任务间的同步和资源共享机制,二值信号量. 二值信号量是计数信号量的一种特殊形式 ...

  6. Spring的AOP简单理解

    最近在研究spring的AOP,翻译出来的意思是面向切面. 总结如下: 所谓AOP就是将分散在各个方法处的公共代码提取到一处, 并通过类似拦截器的机制实现代码的动态整合.可以简单地想象成, 在某个方法 ...

  7. Excel TargetRange.Validation为空的

    做Excel的时候遇到过TargetRange.Validation为空,赋值类似空指针一样的情况. 这样的情况,不懂Excel调试了好久,最后还知道,这个对象需要自己去定义才能够进行赋值, 这样定义 ...

  8. 74HC164dD驱动LED

    驱动要点: 1.上升沿写入串行数据: CLK=0; DAT=num&0x01; CLK=1; 2.写入数据的数码管编码(指代码中的 tab[]) 串行数据是FIFO先进先出,也就是先写高位,移 ...

  9. 从文件/文件流的头字节中得到mime信息

    在写网络爬虫的时候,需要根据链接来获取文件类型,将内容正确存储.之前我都是根据链接的后缀来判断的,比如: http://img12.360buyimg.com/da/20120330/88_31_Zy ...

  10. Btrace的使用方法

    本文基于<深入理解Java虚拟机:JVM高级特性与最佳实践 第2版> 写在前面: Btrace有很多用法,比如说性能监视,连接泄露,内存泄漏,多线程竞争,而本文说的只是最基本的应用打印调用 ...