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 ;
}
												

Play on Words(欧拉回路)的更多相关文章

  1. ACM/ICPC 之 混合图的欧拉回路判定-网络流(POJ1637)

    //网络流判定混合图欧拉回路 //通过网络流使得各点的出入度相同则possible,否则impossible //残留网络的权值为可改变方向的次数,即n个双向边则有n次 //Time:157Ms Me ...

  2. [poj2337]求字典序最小欧拉回路

    注意:找出一条欧拉回路,与判定这个图能不能一笔联通...是不同的概念 c++奇怪的编译规则...生不如死啊... string怎么用啊...cincout来救? 可以直接.length()我也是长见识 ...

  3. ACM: FZU 2112 Tickets - 欧拉回路 - 并查集

     FZU 2112 Tickets Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u P ...

  4. UVA 10054 the necklace 欧拉回路

    有n个珠子,每颗珠子有左右两边两种颜色,颜色有1~50种,问你能不能把这些珠子按照相接的地方颜色相同串成一个环. 可以认为有50个点,用n条边它们相连,问你能不能找出包含所有边的欧拉回路 首先判断是否 ...

  5. POJ 1637 混合图的欧拉回路判定

    题意:一张混合图,判断是否存在欧拉回路. 分析参考: 混合图(既有有向边又有无向边的图)中欧拉环.欧拉路径的判定需要借助网络流! (1)欧拉环的判定:一开始当然是判断原图的基图是否连通,若不连通则一定 ...

  6. codeforces 723E (欧拉回路)

    Problem One-Way Reform 题目大意 给一张n个点,m条边的无向图,要求给每条边定一个方向,使得最多的点入度等于出度,要求输出方案. 解题分析 最多点的数量就是入度为偶数的点. 将入 ...

  7. UVa 12118 检查员的难题(dfs+欧拉回路)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. UVA 10054 (欧拉回路) The Necklace

    题目:这里 题意:有一种由彩色珠子连接而成的项链,每个珠子两半由不同颜色(由1到50的数字表示颜色)组成,相邻的两个珠子在接触的地方颜色相同,现在有一些零碎的珠子,确认它是否能 复原成完整的项链. 把 ...

  9. poj2513Colored Sticks(无向图的欧拉回路)

    /* 题意:将两端涂有颜色的木棒连在一起,并且连接处的颜色相同! 思路:将每一个单词看成一个节点,建立节点之间的无向图!判断是否是欧拉回路或者是欧拉路 并查集判通 + 奇度节点个数等于2或者0 */ ...

  10. poj 1386 Play on Words(有向图欧拉回路)

    /* 题意:单词拼接,前一个单词的末尾字母和后一个单词的开头字母相同 思路:将一个单词的开头和末尾单词分别做两个点并建一条有向边!然后判断是否存在欧拉回路或者欧拉路 再次强调有向图欧拉路或欧拉回路的判 ...

随机推荐

  1. java spring-WebSocket json参数传递与接收

    Websocket原理(摘抄) 一.websocket与http WebSocket是HTML5出的东西(协议),也就是说HTTP协议没有变化,或者说没关系,但HTTP是不支持持久连接的(长连接,循环 ...

  2. 详解Map集合体系及方法entrySet、keySet、values

    简单回顾Map集合: Map表示映射关系,以键值对的方式来保存数据.key和value一一对应.key是唯一的,不可重复,而value是可重复的,可以被多个key关联.虽然Map是放入两个数据,但是却 ...

  3. Java中的代码块:局部代码块、构造代码块和静态代码块

    代码块 java代码中用{ }括起来的代码段叫做代码块 1.局部代码块 在局部位置,用于限定变量的生命周期 例如,下面代码中的a仅在代码块中起作用,因此会编译报错 class Demo{ public ...

  4. js 里常用的字符串操作方法

    /*var str='啦啦啦'; var str1='哈哈哈' //charAt() 返回指定索引处的字符串 console.log(str.charAt(3)) //charCodeAt() 返回指 ...

  5. laravel when 的用法

    当你在使用where语句有前提条件时,比如某值为1的时候才执行where子句,否则不执行,这个时候,laravel5.5新出了一个简便方法when($arg,fun1[,fun2]). 具体用法如下: ...

  6. [示例] 用代码设置 ListView 颜色 (只适用 Win 平台,无需修改官方源码)

    如果可以使用代码随意设置 ListView 的颜色,而不用加载额外的 Style 及修改官方的源码,那该有多好?! 其实 Style 提供了很强了扩充性及可塑性,可以很容易的去操作它. 下面以 Lis ...

  7. 在全志V3/V3s和索智S3/S3L上调试32MB NorFlash

    选取MX25L25635F作为调试对象,其他型号的NorFlash开发调试原理基本一致.为了使V3/V3s/S3/S3L识别32MB NorFlash并正常工作,主要针对以下三个部分进行开发和调试.下 ...

  8. python2.7入门---XML解析

        首先我们先来考虑,什么是XML?XML 指可扩展标记语言(eXtensible Markup Language).XML 被设计用来传输和存储数据.XML是一套定义语义标记的规则,这些标记将文 ...

  9. 20155215宣言 实验四 Andoid开发基础实验报告

    20155215宣言 实验四 Andoid开发基础实验报告 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> 课程: 2.完成实验 ...

  10. 20155230 2016-2017-2 《Java程序设计》第十周学习总结

    20155230 2016-2017-2 <Java程序设计>第十周学习总结 教材学习内容总结 网络编程:就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发 ...