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. 最近学习了Node,利用Express搭建了个人博客,总结下吧

    node+express+jade+mongodb搭建了一套个人博客,我来总结下这几个家伙的使用感受吧! [node] 成熟插件库众多,真的是只有你想不到,没有它做不到的.而且对于有前端JS基础的童鞋 ...

  2. [ javascript canvas 插件演示 ] canvas 插件演示

    <!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...

  3. Android中利用ViewHolder优化自定义Adapter的典型写法

    利用ViewHolder优化自定义Adapter的典型写法 最近写Adapter写得多了,慢慢就熟悉了. 用ViewHolder,主要是进行一些性能优化,减少一些不必要的重复操作.(WXD同学教我的. ...

  4. 微信小程序如何设置开发者和体验者

    微信小程序需要在后台添加开发者和体验者 开发者:增加开发人员的,开发人员添加后,可上传代码,最多10个人,可以删除 体验者:添加为体验者,管理员发布体验版本后,通过扫码二维码可以下载体验版小程序,最多 ...

  5. yii create url (二)

    在Yii中经常要生成URL,不管是为了自动跳转还是仅仅是一个链接.下面对Yii中的URL生成做了一个总结.提示:以下controllerX代表控制器X,actionX代表方法X.在Controller ...

  6. Xcode编译相关

    Xcode多工程联编及工程依赖 iOS release,debug版设置不同的AppIcon Xcode创建子工程以及工程依赖 Xcode 依赖管理带来的静态库动态库思考

  7. 初次使用 git 的“核弹级选项”:filter-branch 从仓库中删除文件

    当初看 Pro Git 时就被作者这个“核弹级选项”的称呼吓到了,因此一直没敢好奇地去尝试.核弹啊,用对了威力无穷,用错了破坏力无穷! 但是,今天,我不得不用了,因为我想把我的原来写一些代码放到 gi ...

  8. C#语言基础——数组

    数组 一.一位数组 数组初始化,创建数组,数组长度为5 int[] array = new int[5]; array[0] = 1; array[1] = 2; array[2] = 3; arra ...

  9. WCF绑定和行为在普通应用和SilverLight应用一些对比

    本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 阅读目录 介绍 绑定 普通应用和SilverLight应用区别 本文版权归mephisto和博客园共有, ...

  10. Bootstrap入门(二)栅格

    Bootstrap入门(二)栅格 Bootstrap入门(二)栅格 全局CSS样式--栅格 先引入本地的CSS文件(根据自己的文件夹,有不同的引入地址,我是放在一个新建的名为css的文件夹中) con ...