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.
 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int par[];
int id[];
int od[]; ///par为并查集,id为入度,od为出度
int vis[]; ///vis为该顶点出现过的标记
int finds(int x)///并查集的查找函数
{
int r = x;
while(r != par[r])
{
r = par[r];
}
int i = x, j;
while(i != r)///路径压缩
{
j = par[i];
par[i] = r;
i = j;
}
return r;
}
void join(int x, int y)///并查集的合并函数
{
int fx = finds(x);
int fy = finds(y);
if(fx != fy)///如果x,y在两个不同的连通分量上,那么合并
{
par[fy] = fx;
}
}
int main()
{
int t, n, i;
int u,v;
char str[];
scanf("%d", &t);
while(t--)
{
memset(id, , sizeof(id));///初始化各个数组
memset(od, , sizeof(od));
memset(vis, false, sizeof(vis));
for(i = ; i <; i++)///初始化并查集,根节点为自身
{
par[i] = i;
}
scanf("%d", &n);
while(n--)
{
scanf("%s", str);
int len = strlen(str);
u=str[]-'a';
v=str[len-]-'a';
join(u,v);///合并出现的顶点
id[v]++;///相应的入度+1
od[u]++;///相应的出度+1
vis[u]=vis[v]=;///标记该顶点出现过
}
int flag = , tag = , a = , b = ; ///flag来判图是否连通,tag为图中顶点入度不等于出度的个数
///a为入度大出度1的点的个数,b为出度大入度1的点的个数
for(i = ; i <; i++)///判连通
{
if(vis[i] && par[i] == i)///如果连通,那么所有的点将被划分到一个集合之中
{
flag++;
}
}
if(flag > )///不连通,直接输出
{
printf("The door cannot be opened.\n");
}
else///flag必须为1
{
for(i = ; i <; i++)///查找tag,a, b 的个数
{
if(vis[i] && id[i] != od[i])
{
tag++;
}
if(vis[i] && id[i]-od[i] == )
{
a++;
}
if(vis[i] && od[i]-id[i] == )
{
b++;
}
}
if(tag == )///tag = 0,对于所有的点入度等于初度,存在欧拉回路
{
printf("Ordering is possible.\n");
}
else if(a == b && a == && tag == )///a = 1 && b = 1 && tag = 2.存在欧拉通路
{
printf("Ordering is possible.\n");
}
else
{
printf("The door cannot be opened.\n");
}
}
}
return ;
}
												

最新文章

  1. 使用OUYA第一次启动OUYA
  2. 为什么说Parcelable 比Serializable更高效
  3. VS软件对应编号
  4. Java并发编程-关卡
  5. Hadoop开发环境搭建
  6. Boost Build
  7. myNote
  8. HDU-5532(LIS-nlogn)
  9. 重读LPTHW-Lesson18-21 函数
  10. Java设计模式(三)-修饰模式
  11. IE不能上网、有道云笔记不能联网、各种软件主页不能联网解决办法一
  12. 《JavaScript高级程序设计》读书笔记 ---创建对象
  13. phabricator 搭建
  14. (转载)Java多线程的监控分析工具(VisualVM)
  15. RocketMQ源码 — 九、 RocketMQ延时消息
  16. 【MAC】常用方法-持续更新
  17. Android:一个高效的UI才是一个拉风的UI(二)
  18. 在UTF-8中,一个汉字为什么需要三个字节?(转)
  19. android 开发 ANR
  20. 2018/03/27 每日一个Linux命令 之 cron

热门文章

  1. UML架构设计师必备神器
  2. ThinkPHP微信扫码支付接口
  3. Python中级 —— 02函数式编程
  4. Delphi的FIFO实现
  5. 和IDEA一样好用的go语言IDE:Goland
  6. 搜索引擎的选择与在chrome上的设置
  7. jade
  8. SSM-CRUD入门项目——查询
  9. 20155235 《Java程序设计》 实验二 Java面向对象程序设计
  10. 20155339 2016-2017-2 《Java程序设计》第4周学习总结