Play on Words
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 11846   Accepted: 4050

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、无向图存在欧拉回路条件:每个点度都为偶数

2、无向图存在欧拉路条件:只有两个点度为奇数或每个点度都为偶数

3、有向图存在欧拉回路条件:每个点入度都等于出度

4、有向图存在欧拉路条件:只存在这样两个点:一个点入度等于出度+1,另一个点入度=出度-1,其他每个点入度都等于出度,或者每个点入度都等于出度。

判连通,判条件就好了

奇怪的是,dfs就WA,并查集就可以

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
const int N=1e5+,L=1e3+;
int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int T,n,ind[],outd[],has[];
char s[L];
//int vis[30],g[30][30];
//void dfs(int u){//printf("dfs %d\n",u);
// vis[u]=1;
// for(int v=0;v<=25;v++)
// if(!vis[v]) dfs(v);
//}
int fa[],root=;
inline int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
int main(){
T=read();
while(T--){
int flag=;
for(int i=;i<=;i++) ind[i]=outd[i]=has[i]=,fa[i]=i;
//memset(g,0,sizeof(g));
n=read();
for(int i=;i<=n;i++){
scanf("%s",s+);
int len=strlen(s+),u=s[]-'a',v=s[len]-'a';
//g[u][v]=g[v][u]=1;
fa[find(u)]=find(v);root=find(v);
outd[u]++;ind[v]++;
has[u]=has[v]=;
}
//for(int i=0;i<=25;i++) if(has[i]) {dfs(i);break;}
//for(int i=0;i<=25;i++) if(has[i]&&!vis[i]) {flag=0;break;}
for(int i=;i<=;i++) if(has[i]&&find(i)!=root){flag=;break;}
if(flag){
int more=,less=;
for(int i=;i<=;i++){
if(abs(outd[i]-ind[i])>=) {flag=;break;}
if(outd[i]==ind[i]+) more++;
if(outd[i]==ind[i]-) less++;
}
if(flag){
if((more==&&less==)||(more==&&less==)) printf("Ordering is possible.\n");
else flag=;
}
}
if(flag==) printf("The door cannot be opened.\n");
}
}

POJ1386Play on Words[有向图欧拉路]的更多相关文章

  1. Play on Words(有向图欧拉路)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8571   Accepted: 2997 Description Some ...

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

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

  3. 欧拉路&&欧拉回路 概念及其练习

    欧拉路: 如果给定无孤立结点图G,若存在一条路,经过图中每边一次且仅一次,这条路称为欧拉路: 如果给定无孤立结点图G,若存在一条回路,经过图中每边一次且仅一次,那么该回路称为欧拉回路. 存在欧拉回路的 ...

  4. HihoCoder1644 : 完美命名的烦恼([Offer收割]编程练习赛37)(有向图的一笔画问题||欧拉路)

    描述 程序员常常需要给变量命名.给函数命名.给项目命名.给团队命名…… 好的名字可以大大提高程序员的主观能动性,所以很多程序员在起名时都会陷入纠结和烦恼. 小Hi希望给新的项目起个完美的名字.首先小H ...

  5. hdu1161 欧拉路

    欧拉路径是指能从一个点出发能够“一笔画”完整张图的路径:(每条边只经过一次而不是点) 在无向图中:如果每个点的度都为偶数 那么这个图是欧拉回路:如果最多有2个奇数点,那么出发点和到达点必定为该2点,那 ...

  6. POJ 1637 Sightseeing tour (混合图欧拉路判定)

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6986   Accepted: 2901 ...

  7. hihoCoder #1182 欧拉路·三 (变形)

    题意: 写出一个环,环上有2^n个格子,每个格子中的数字是0或1,相连着的n个格子可以组成一个数的二进制,要求给出这2^n个数字的序列,使得组成的2^n个数字全是不同的.(即从0到2^n-1) 思路: ...

  8. hiho 1182 : 欧拉路·三

    1182 : 欧拉路·三 这时题目中给的提示: 小Ho:是这样的,每次转动一个区域不是相当于原来数字去掉最左边一位,并在最后加上1或者0么. 于是我考虑对于"XYYY",它转动之后 ...

  9. Play on Words(欧拉路)

    http://poj.org/problem?id=1386 题意:给定若干个单词,若前一个的尾字母和后一个单词的首字母相同,则这两个单词可以连接,问是否所有的单词都能连接起来. 思路:欧拉路的判断, ...

随机推荐

  1. html4基础知识梳理

    基础的html知识,只放Xmind的截图. 第一部分: 第二部分: 某些标签的使用示例及注意事项,在印象笔记里.

  2. [deviceone开发]-拼图小游戏

    一.简介 九宫格小游戏,可从本地图库载入一张图片,填充到9个ImageView,另涉及Timer计时.图库控件. 每个格子都是相同的控件,动态添加到首页中的,在初始化后,响应touch事件,之后通过多 ...

  3. (转)高性能JavaScript:加载和运行(动态加载JS代码)

    浏览器是如何加载JS的 当浏览器遇到一个<script>标签时,浏览器首先根据标签src属性下载JavaScript代码,然后运行JavaScript代码,继而继续解析和翻译页面.如果需要 ...

  4. JavaScript学习笔记-基础语法、类型、变量

    基础语法.类型.变量   非数字值的判断方法:(因为Infinity和NaN他们不等于任何值,包括自身) 1.用x != x ,当x为NaN时才返回true; 2.用isNaN(x) ,当x为NaN或 ...

  5. 如何在Infraworks中创建多树种组成的森林

    在Infraworks 2014中,你可以有shp文件导入生成树木和森林,也可以直接在模型中规划一片区域作为森林.美中不足的就是,这些充其量叫树林不能叫森林,因为他们的样式都是一个树种,而真正的森林肯 ...

  6. Websphere 系列的https证书的配置说明

    术语解释 v IHS IBM HTTP Server v WP Websphere portal v WAS Websphere Application Server 系统安装 WAS6.1 安装了例 ...

  7. Java输入/输出流体系

    在用java的io流读写文件时,总是被它的各种流能得很混乱,有40多个类,理清啦,过一段时间又混乱啦,决定整理一下!以防再忘 Java输入/输出流体系 1.字节流和字符流 字节流:按字节读取.字符流: ...

  8. Ida动态修改android程序的内存数据和寄存器数值,绕过so文件的判断语句

    我们继续分析自毁程序密码这个app,我们发现该程序会用fopen ()打开/proc/[pid]/status这个文件,随后会用fgets()和strstr()来获取,于是我们在strstr()处下个 ...

  9. APP 游戏审核改动

    广电总局封杀游戏 移动游戏将进入洗牌期 封杀了电影.电视剧.网络剧 现在轮到游戏了 新法速递 2016年7月1日,国家新闻出版广电总局办公厅<关于移动游戏出版服务管理的通知>(新广出办发[ ...

  10. 【Android】不使用WebView来执行Javascript脚本(Rhino)

    前言 动态执行脚本能有效的降低重要功能硬编码带来的问题,尤其是依赖于第三方的应用,可以通过动态脚本+在线参数(例如友盟在线参数)再不更新应用的情况下升级功能. 声明 欢迎转载,但请保留文章原始出处:) ...