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. bootstrap datetimepicker时间日期控件

    github地址:https://github.com/smalot/bootstrap-datetimepicker Both Date and Time picker widget based o ...

  2. NSDateFormatter

    NSDate *now = [NSDate date]; NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = ...

  3. sublime text常用插件

    这个比较重要,不会装插件的时候找了好久 sublime text常用插件 1.插件的安装方法 第一种:用package control 这个是用来管理插件的,必备啊,安装package control ...

  4. something about css locating.

    CSS position:static:默认属性,静态定位relative:相对定位,相对于父元素的定位,需要配合top,left,right,bottom,z-index等属性absolute:绝对 ...

  5. struts局部、全局类型转换器

    第01步:编写bean package com.self.bean; import java.util.Date; public class User { private Date birthday ...

  6. ios blog

    转得一个朋友的博客,大家可以看哈(主要时国外的) 主要分开发教程.示例项目.UI设计.问题解决几块. 开发教程: 即便过了入门阶段,还是要经常看看一些不错的实例教程. .http://mobile.t ...

  7. Android中使用SurfaceView+MediaPlayer+自定义的MediaController实现自定义的视屏播放器

    效果图如下: (PS本来是要给大家穿gif动态图的,无奈太大了,没法上传) 功能实现:暂停,播放,快进,快退,全屏,退出全屏,等基本功能 实现的思路: 在主布局中放置一个SurfaceView,在Su ...

  8. (转)VS2008连接TFS 2010

    偶尔还是会用到,老是忘记安装的顺序,在这儿mark一下. 用VS2008连接TFS 2010, 需要按照以下顺序安装一下组件: .VS2008 Team Explorer 2008 3.Install ...

  9. paper 74:MATLAB图像处理_HSV与RGB颜色空间互转

    HSV空间:分别是H(色调)——S(饱和度)——V(亮度) 与HSI颜色空间类似:分别是H(色调)——S(饱和度)——I(强度) 注意: 强度和亮度差不多是一个概念. 饱和度代表的是渗入白光的数量级, ...

  10. YbRapidSolution.MVC项目首页分页没有起作用

    @model YbRapidSolution.Mvc.Models.CmsPagerDataModel <nav> <ul class="pager"> & ...