Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u

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

欧拉道路问题。这题欧拉回路或者欧拉道路都算成立。

建好图以后先判一下连通性,如果图不连通,肯定不成立。

↑DFS BFS 并查集都可以

然后是判欧拉路径。

  如果是欧拉回路,那么所有点的入度等于出度。

  如果是欧拉路径,那么只有两个点的入度不等于出度,其一是起始点,出度比入度大1,另一个是结束点,入度比出度大1

之前一直WA,不知怎么改着改着就对了。

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int mxn=;
bool vis[mxn];
int n;
int fa[mxn];
int in[mxn],out[mxn];
void init(int n){
for(int i=;i<=n;i++)fa[i]=i;
}
int find(int x){
if(fa[x]==x)return x;
else return fa[x]=find(fa[x]);
}
char s[];
int main(){
int T;
scanf("%d",&T);
while(T--){
memset(vis,,sizeof vis);
memset(in,,sizeof in);
memset(out,,sizeof out);
scanf("%d",&n);
init();
int i,j;
for(i=;i<=n;i++){
scanf("%s",s);
int u=s[]-'a'+;
int v=s[strlen(s)-]-'a'+;
in[v]++;out[u]++;
vis[u]=vis[v]=;
int x=find(u),y=find(v);
if(x!=y)fa[y]=x;
}
int st=,ed=;bool flag=;
int cnt=;
for(i=;i<=;i++){
if(vis[i] && find(i)==i){
cnt++;
}
if(out[i]!=in[i]){//出度不等于入度时判定
if(out[i]==in[i]+){
if(!st)st=i;
else flag=;
}
else if(in[i]==out[i]+){
if(!ed)ed=i;
else flag=;
}
else flag=;
}
}
if(cnt!=){//非连通特判
printf("The door cannot be opened.\n");
continue;
}
if(flag==)printf("The door cannot be opened.\n");
else printf("Ordering is possible.\n");
}
return ;
}

POJ1386 Play on Words的更多相关文章

  1. 【poj1386】 Play on Words

    http://poj.org/problem?id=1386 (题目链接) 题意 给出n个单词,判断它们能否首尾相接的排列在一起. Solution 将每一格单词的首字母向它的尾字母连一条有向边,那么 ...

  2. poj-1386(欧拉回路)

    题意:给你n个单词,每个单词可以和另一个单词连接,前提是(这个单词的尾字母等下一个单词的首字母),问你有没有一种连法能够连接所有的单词: 解题思路:每个单词可以看成是首字母指向尾字母的一条边,那么就变 ...

  3. poj1386 字符串类型的一笔画问题 欧拉回路

    Play on Words Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10685   Accepted: 3625 De ...

  4. poj1386单词连接(欧拉欧拉欧拉)

    ///单词连接,欧拉回路通路都可以(有向图) ///主要构图:比如possibilities就构造p->s的边////题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词 ...

  5. poj1386有向图判断是否存在欧拉回路或者欧拉路

      有向图的图联通是指基图联通,也就是把有向图的边改成无向图然后看是否连通.判断联通可用dfs或者并查集. 题意就是给你n个由小写字母构成的字符串,问你能不能将这n个字符串连接起来,B能接在A后面的条 ...

  6. Play on Words

    poj1386:http://poj.org/problem?id=1386 题意:给你n个单词,问你是否能够通过调整单词的顺序存在这样的一个序列,使得 每个单词的首字母是前一个单词的尾字母. 题解: ...

  7. D2欧拉路,拓扑排序,和差分约束

    第一题:太鼓达人:BZOJ3033 题意:给出k,求一个最长的M位01串,使其从每一个位置向后走k个得到 的M个k位01串互不相同(最后一个和第一个相邻,即是一个环).输出 字典序最小的答案. 2 ≤ ...

  8. ACM学习大纲

    1 推荐题库 •http://ace.delos.com/usaco/ 美国的OI 题库,如果是刚入门的新手,可以尝试先把它刷通,能够学到几乎全部的基础算法极其优化,全部的题解及标程还有题目翻译可以b ...

  9. POJ 1386 Play on Words (有向图欧拉路径判定)

    Play on Words Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8768   Accepted: 3065 Des ...

随机推荐

  1. 【TP5.1】HTML标签自动转义,导致CKEditor保存内容无法正常显示!

    问题:使用Thinkphp5.1 开发的时候显示CKEditor保存的内容不符合预期. 希望的样子,肯定是不显示<p><b>等标签,而是下面的样子. 因为刚开始使用TP5.1和 ...

  2. Java 的单元测试

    有点需要注意,当 JUnit 主线程退出,子线程也会跟着退出,需要使用子线程的 join() 方法使主线程等待 Maven 依赖 <dependency> <groupId>j ...

  3. svn TortoiseSVN 回滚版本

    原文链接: http://keenwon.com/1072.html SVN是一个版本管理工具,在工作中经常使用,尤其是多人合作开发的时候,版本管理显得更加重要.需要使用回退的场景往往都比较" ...

  4. python基础——数字&集合&布尔类型

    Python的核心数据类型 内置对象 对象类型 例子 数字 123,3.1415,3+4j,Decimal(小数),Fraction(分数) 字符串 'dodo',"guido's" ...

  5. DataGridView重查后,返回原来所在行

    首先记录选中行 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 //查询前记录选中行 int _currentRow = 0; //int _cu ...

  6. 《Cracking the Coding Interview》——第18章:难题——题目13

    2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...

  7. 每天一个Linux命令(11):cat命令

    cat命令连接文件并打印到标准输出设备上. 注意:当文件较大时,文本在屏幕上迅速闪过(滚屏),用户往往看不清所显示的内容.因此,一般用more等命令分屏显示.为了控制滚屏,可以按Ctrl+S键,停止滚 ...

  8. CSS简易学习笔记

    学习地址:http://www.w3school.com.cn/css/index.asp cnblog不能把格式复制上来,有格式文字版:https://github.com/songzhenhua/ ...

  9. SQL面试题:之一(难度:中等)

    SQL面试题:之一(难度:中等)

  10. 一个初学者的辛酸路程-依旧Django

    回顾: 1.Django的请求声明周期?   请求过来,先到URL,URL这里写了一大堆路由关系映射,如果匹配成功,执行对应的函数,或者执行类里面对应的方法,FBV和CBV,本质上返回的内容都是字符串 ...