Play on Words

Time Limit: 1000MS Memory Limit: 10000K

Total Submissions: 10620 Accepted: 3602

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 wordmotorola”. 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

Central Europe 1999

题意:给你n个字符串,判断这些字符串能连接在一起使的每个单词的第一个字母与前面一个的字符串的最后一个字母相同.

方法:并查集+欧拉通路,并查集判断图是不是连通的.用有向图的欧拉路的性质判断.能不能形成欧拉通路.

#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define CRR fclose(stdin)
#define CWW fclose(stdout)
#define RR freopen("input.txt","r",stdin)
//#pragma comment(linker, "/STACK:102400000")
#define WW freopen("output.txt","w",stdout) const int MAX = 1100;
char s[MAX];
bool vis[26];
int pre[26];
int in[26],out[26];
int Find(int x)//并查集
{
return pre[x]==-1?x:pre[x]=Find(pre[x]);
} void Link(int x,int y)
{
int Fx=Find(x);
int Fy=Find(y);
if(Fx!=Fy)
{
pre[Fx]=Fy;
}
}
bool Judge()
{
int ood=0,ooc=0;
int ans=-1;
for(int i=0;i<26;i++)
{
if(!vis[i])
{
continue;
}
if(in[i]-out[i]>=2)//如果入度与出度的差值大于1,则形不成欧拉通路.
{
return false;
}
if(out[i]-in[i]>=2)
{
return false;
}
if(in[i]-out[i]==1)
{
ood++;
if(ood>1)//只有一个顶点的入度与出度的差值为一,另一个的出度与入度的差值为一,其余的差值为零.
{
return false;
}
}
if(out[i]-in[i]==1)
{
ooc++;
if(ooc>1)
{
return false;
}
}
if(out[i]==0&&in[i]==0)
{
return false;
}
if(ans==-1)
{
ans=i;
}
else
{
if(Find(i)!=Find(ans))//判断图是不是连通的.
{
return false;
}
}
}
if(ood!=ooc)
{
return false;
}
return true;
} int main()
{
int T; int n; scanf("%d",&T); while(T--)
{
scanf("%d",&n); memset(vis,false,sizeof(vis)); memset(pre,-1,sizeof(pre)); memset(out,0,sizeof(out)); memset(in,0,sizeof(in)); for(int i=0;i<n;i++)
{
scanf("%s",s); int len=strlen(s); in[s[len-1]-'a']++;
out[s[0]-'a']++;
Link(s[0]-'a',s[len-1]-'a');
vis[s[0]-'a']=true;
vis[s[len-1]-'a']=true;
}
if(Judge())
{
printf("Ordering is possible.\n");
}
else
{
printf("The door cannot be opened.\n");
}
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

欧拉通路-Play on Words 分类: POJ 图论 2015-08-06 19:13 4人阅读 评论(0) 收藏的更多相关文章

  1. OC基础:类的扩展.协议 分类: ios学习 OC 2015-06-22 19:22 34人阅读 评论(0) 收藏

    //再设计一个类的时候,有些方法需要对外公开(接口),有些仅供内部使用. 类的扩展:为类添加新的特征(属性)或者方法 对已知类: 1.直接添加 2.继承(在其子类中添加实例变量和方法) 3.使用ext ...

  2. K - Work 分类: 比赛 2015-07-29 19:13 3人阅读 评论(0) 收藏

    K - Work Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  3. refresh的停车场 分类: 栈和队列 2015-06-18 17:13 26人阅读 评论(0) 收藏

    refresh的停车场 TimeLimit: 1000ms Memory limit: 65536K 题目描述 refresh最近发了一笔横财,开了一家停车场.由于土地有限,停车场内停车数量有限,但是 ...

  4. 摄像头参数查看与调节 分类: C/C++ OpenCV 2014-11-08 18:13 138人阅读 评论(0) 收藏

    cvGetCaptureProperty 获得视频获取结构的属性 double cvGetCaptureProperty( CvCapture* capture, int property_id ); ...

  5. android开发之this.finish()的使用 分类: android 学习笔记 2015-07-18 19:05 30人阅读 评论(0) 收藏

    在一个Activity用完之后应该将之finish掉,但是,之前在学校里自己摸索着开发时并没有太注意这个问题,因为activity无论是否finish掉对功能的影响貌似都不是那么明显(这是读书时候的观 ...

  6. UI基础:事件.响应链 分类: iOS学习-UI 2015-07-03 19:51 1人阅读 评论(0) 收藏

    UIEvent:事件,是由硬件捕捉的一个代表用户操作操作设备的对象. 事件分三类:触摸事件.晃动事件.远程控制事件. 触摸事件:用户通过触摸设备屏幕操作对象,.输入数据.支持多点触摸,包含1个到多个触 ...

  7. UI基础:UILabel.UIFont 分类: iOS学习-UI 2015-07-01 19:38 107人阅读 评论(0) 收藏

    UILabel:标签 继承自UIView ,在UIView基础上扩充了显示文本的功能.(文本框) UILabel的使用步骤 1.创建控件 UILabel *aLabel=[[UILabel alloc ...

  8. OC基础:Date 分类: ios学习 OC 2015-06-22 19:16 158人阅读 评论(0) 收藏

    NSDate  日期类,继承自NSObject,代表一个时间点 NSDate *date=[NSDate date]; NSLog(@"%@",date);   //格林尼治时间, ...

  9. OC基础:继承.初始化方法,便利构造器 分类: ios学习 OC 2015-06-16 19:27 84人阅读 评论(0) 收藏

    继承: 1.单向继承,一个类只能有一个父类,一个父类可以有多个子类. 2.单向继承,基类(根类)是OSObject 3.子类可以继承父类的属性和方法 当父类的方法不满足子类的需求时,子类可以重写父类的 ...

随机推荐

  1. Nodejs解决2分钟限制

    摘要:解决:在nodejs中调用服务,若超过2分钟服务没有返回数据,node会再次请求服务.  加班的日子总算暂时结束了,才发现下午6点钟的天还没有黑!开始我的总结吧... 去年的某个项目用nodej ...

  2. IntelliJ IDEA 项目相关的几个重要概念介绍

    必备材料介绍 IntelliJ IDEA 对其他 IDE 转过来的用户有特别优待,对其专门整理了非常棒的资料,还请其他 IDE 过来的用户抽时间查看,会有很大帮助:Eclipse 用户可以看:http ...

  3. 判断UpLoader是否安装了Flash

     var flashVersion = (function() {             var version;             try {                 version ...

  4. Groupon面经:Find paths in a binary tree summing to a target value

    You are given a binary tree (not necessarily BST) in which each node contains a value. Design an alg ...

  5. navicat内的主键和外键

    数据库内的一个重点是主键另一个是外键 实体完整性{ 主键的全称:主关键字    它能够进行唯一标示某一列的 主键的三大特点是:唯一  非空  排序 一个没有主键的表不是一个完整的表,只要表设置了主键那 ...

  6. Linux Ubuntu常用终端命令

    查看cpu温度: 安装命令如下:sudo apt-get install acpi 然后acpi -t 即可 输入法配置窗口命令: fcitx-config-gtk3 im-config 任务管理器命 ...

  7. JSP里比对单选框或复选框的数值而自动打勾

    <table> <tr> <td class="tableleft">状态</td> <td><input typ ...

  8. linux扩大swap交换空间

    有两种解决方法:一是创建新的swap分区;另一则是创建swap文件 创建swap文件如果你的硬盘空间已经全部分配给其他分区,也没有多余的预算新添购硬盘,我们可以利用swap文件的方式增加虚拟的swap ...

  9. mysql 导入大数据的秘籍

    在使用这种方法前,你必须先建立一个数据库,这个数据库是你希望将sql文件导入的数据库.假如你创建的数据库为demo_data,数据库文件为demo.sql 并且该数据库文件位于你的D盘下,即该文件在 ...

  10. 反射认识_06_ArrayList_HashSet区别

    包01: package ReflectionCollection; public class ReflectionConstructorPoint { private int x; public i ...