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. NOI P1896 互不侵犯 状压DP

    题目描述 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. 注:数据有加强(2018/4/25) ...

  2. POJ:2431-Expedition

    Expedition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20089 Accepted: 5786 Descripti ...

  3. Java中数据类型转换&基本类型变量和对象型变量

    1.Java的数据类型分为三大类 布尔型,字符型和数值型 其中数值型又分为整型和浮点型 2.Java的变量类型 布尔型 boolean 字符型 char 整型    byte,short,int,lo ...

  4. Java课程目录

    Java课程目录 1 年前 大家收藏一下这篇文章,这是所有课程的目录. 第一周: 第一课:进制及计算机表示 第二课:进制习题课,进一步介绍进制的知识 第三节课:Java语言的品味(一),通过stati ...

  5. Lua语言中文手册 转载自网络

    Programming in LuaCopyright ® 2005, Translation Team, www.luachina.net Programming in LuaProgramming ...

  6. MQ消息中间件

    MQ是什么? MQ是Message Queue消息队列的缩写.消息队列是一种应用程序对应用程序的通信方法.应用程序通过写和检索入列队的针对应用程序的数据(消息)来进行通信,而不需要专用连接来链接它们. ...

  7. Monkey、Monkeyrunner之间的区别

    Monkey.Monkeyrunner之间的区别 一.Monkey Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中.它向系统发送伪随机的用户事件流(如按键输入.触摸屏输 ...

  8. win10下乌龟git安装和使用

    一.安装git for windows 首先下载git for windows客户端http://msysgit.github.io/ 安装过程没什么特别的,不停next就ok了     图太多就不继 ...

  9. JavaScript里面的基本函数

    1.主要有三类基本函数 <script type="text/javascript"> // 普通函数 function func1(arg){ return true ...

  10. intellij idea 2017 工具使用问题

    1.打开idea 打开maven项目报错:Unable to import maven project 2.在idea中Help->Show Log in Explorer->idea.l ...