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. unity, 欧拉角(euler angle)

    1,按ZXY顺序. 2,左手螺旋. 3,Z,X,Y旋转均应以未旋转前的坐标系为准.

  2. Oracle 10g如何对用户姓名,按首字母排序、查询

    首先介绍Oracle 9i新增加的一个系统自带的排序函数  1.按首字母排序  在oracle9i中新增了按照拼音.部首.笔画排序功能.设置NLS_SORT值      SCHINESE_RADICA ...

  3. Mysql User表权限字段说明全介绍

    一:mysql权限表user字段详解: Select_priv.确定用户是否可以通过SELECT命令选择数据. Insert_priv.确定用户是否可以通过INSERT命令插入数据. Update_p ...

  4. c语言优先级和结合性

    C语言的运算符众多,具有不同的优先级和结合性,我们将它们全部列了出来,方便大家对比和记忆: 优先级 运算符 名称或含义 使用形式 结合方向 说明 1 [] 数组下标 数组名[常量表达式] 左到右   ...

  5. Linux硬盘速度测试的命令

    测试下硬盘的读写速度如何,在linux下可以使用hdparm 对硬盘进行测试或查看硬盘的相关信息. hdparm 命令进行硬盘速度测试.参数: -a 表示是否关闭磁盘预读取功能.对于大文件读取,这个显 ...

  6. [svc]visio绘制模具

    visio2016狮子XL自定义运维模具下载: https://github.com/lannyMa/scripts/blob/master/%E7%BE%8E%E5%8C%96%E5%AE%9A%E ...

  7. 【Android】14.3 浏览手机中的所有文件夹和文件

    分类:C#.Android.VS2015: 创建日期:2016-02-27 一.简介 前面我们了解了内部存储.外部存储的含义,用一句话说,内部存储实际上是保存在"data"文件夹下 ...

  8. CGameMainScene类

    #ifndef __GAMEMAIN_SCENE_H__ #define __GAMEMAIN_SCENE_H__ #include "cocos2d.h" #include &q ...

  9. Hive使用过程中的坑

    在Hive脚本中如果有 use db; #即使用数据库 最后一定要有一个exit;脚本,退出hive窗口 否则运行到最后,hive无法启动MR任务,只是卡在打印完成hive脚本处. 例子如下: $Hi ...

  10. MultipartEntity 乱码

    MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Ch ...