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. Sql Server Always On主库与附库遇到的问题

    使用Always On的时候永远要记住只有一个主数据库可写,如果写的话要不在监听节点上做写的操作,要不只在主数据库上写的操作不然只读库无法读写

  2. Android APK的安装

    打开packages\apps\PackageInstaller下的清单文件 <?xml version="1.0" encoding="utf-8"?& ...

  3. 【代码笔记】iOS-设置textView或者label的行间距方法

    一,效果图. 二,代码. RootViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional se ...

  4. 【代码笔记】iOS-检测手机翻转

    一,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. ...

  5. OC NSFileManager(文件路径操作)

    OC NSFileManager(文件路径操作) 初始化 NSFileManager * fm = [NSFileManager defaultManager]; 获取当前目录 [fm current ...

  6. python 判断学期与学年

    9,10,11,12,1 第一学期 2,3,4,5,6,7 第二学期 其中8月份放假,暂且放入第一学期.因为大部分学校都选在8月底开学 import datetime def getXNandXQ() ...

  7. 如何开启win7端口的图文教程

    Windows 7/Vista/XP/2003等系统中的远程终端服务是一项功能非常强大的服务,下面来教教大家如何开启端口:打开“控制面板”中的“Windows防火墙”,点击左侧的“高级设置” 右击“入 ...

  8. 集合1--毕向东java基础教程视频学习笔记

    Day14 集合框架01 体系概述02 共性方法03 迭代器04 List集合共性方法05 ListIterator06 List集合具体对象特点07 Vector中的枚举 01 体系概述 集合类为什 ...

  9. SQLServer中ISNULL、NULLIF和CONVERT函数

    create view sss as(select ISNULL(operate_time, CONVERT(VARCHAR(20),create_time,120)) time from s_pro ...

  10. XAML中的特殊符号几空白字符处理

    阅读目录 介绍 详细 处理 Demo下载 介绍 XAML标记语言是基于xml的,所以很多xml中的特殊符号在XAML也是需要处理的. 详细 (取自msdn) 字符 Entity 注释 &(“a ...