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. transient关键字的含义

    transient java语言的关键字,变量修饰符,如果用transient声明一个实例变量,当对象存储时,它的值不需要维持. Java的serialization提供了一种持久化对象实例的机制.当 ...

  2. fzu 2146 Easy Game

    http://acm.fzu.edu.cn/problem.php?pid=2146  Problem 2146 Easy Game Accept: 661    Submit: 915Time Li ...

  3. [原创]java WEB学习笔记76:Hibernate学习之路---Hibernate介绍,hibernate 环境的搭建

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  4. Java基础(5):试用Array类对数据进行操作(Sort和toString方法)

    Arrays 类是 Java 中提供的一个工具类,在 java.util 包中.该类中包含了一些方法用来直接操作数组,比如可直接实现数组的排序.搜索等 1. 排序 语法:  Arrays.sort(数 ...

  5. C#控件:TabControl

    tabcontrol在切换页面的时候经常用到 这里讲下如何让tabcontrol更好看 ref:http://blog.csdn.net/conmajia/article/details/759671 ...

  6. extjs grid 单元格 多选

    new Ext.grid.CellSelectionModel({ last : false, // 上一次选中的单元格 selections : [], // 选择区缓存 handleMouseDo ...

  7. oracle,sqlserver,mysql 命令行 开启、关闭所需要的服务

    ORACLE需要开启的服务   需要启动的服务:   口令: 启动Oracle 11g服务: (下面的可以作为bat 脚本,直接运行便可以不用去自己去启动和关闭服务了.) @echo off @ EC ...

  8. WM (Constants)

    Create page WM (Constants)   Summary WM_* Constants and their definitions or descriptions and what c ...

  9. sql over开窗函数,

    sql over开窗函数, 1.使用over子句与rows_number()以及聚合函数进行使用,可以进行编号以及各种操作.而且利用over子句的分组效率比group by子句的效率更高. 2.在订单 ...

  10. paper 44 :颜色矩和颜色相关图(color correlogram)