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. 从客户端(?)中检测到有潜在危险的 Request.Path 值 的解决方案

    public ActionResult A(string title) { return Redirect("B"+((String.IsNullOrEmpty(title))?& ...

  2. JavaScript强化教程——JavaScript 总结

    本教程中我们向您讲授了如何向 html 页面添加 JavaScript,使得网站的动态性和交互性更强. 你已经学习了如何创建对事件的响应,验证表单,以及如何根据不同的情况运行不同的脚本. 你也学到了如 ...

  3. iOS加密之MD5加密

    话不多说,上代码! MyMD5.h里面 #import <Foundation/Foundation.h> @interface MyMD5 : NSObject { } +(NSStri ...

  4. atitit.身份认证解决方案attilax总结

    atitit.身份认证解决方案attilax总结 1.1. 身份认证1 1.2.  basic认证1 1.2.1. 编程实现basic客户端2 1.3. digest认证机制3 1.4. SSL认证3 ...

  5. SharePoint 2013 通过审计获取文档下载次数

    1.创建一个文档库,进入库设置,找到”Information management policy settings”,点进去,如下图: 2.分别设置”Document”.”Folder”两个,如下图: ...

  6. Python学习02 列表 List

    Python学习02 列表 List Python列表 List Python中的列表(List)用逗号分隔,方括号包围(comma-separated values (items) between ...

  7. 你真的了解UINavigationController吗?

    一:首先查看一下关于UINavigationController的定义 NS_CLASS_AVAILABLE_IOS(2_0) @interface UINavigationController : ...

  8. iOS 开发之路(AES/DES加密实现) 三

    最近接触的这个项目由于以前服务器上用的是DES/CBC/PKCS5Padding加密方式,为了让在iOS上的加密结果与服务器端保持一致,我做了很多尝试,现在分享给大家.PS:现在不推荐用DES了,只是 ...

  9. c中的指针

    一. 指针前奏 1. 指针的重要性 指针是C语言中非常重要的数据类型,如果你说C语言中除了指针,其他你都学得很好,那你干脆说没学过C语言. 2. 小需求 l void change(int  n)函数 ...

  10. 基于Metaweblog API 接口一键发布到国内外主流博客平台

    之前的生活 之前一直使用evenote写博客和日志,其实还是挺方便的.但是我一直都希望能够同步到国内的博客和国外的blogspot等主流博客平台.而强大everote只提供了facebook.twit ...