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. Maven插件的简介,安装及在eclipse中配置

    1.Maven简介 1.Maven介绍 Maven是一个基于项目对象模型(POM:project object model)的概念的纯java开发的开源的项目管理工具.Maven把我们的工程抽像一个对 ...

  2. 浅淡 RxJS WebSocket

    const open$ = new Subject(); const ws = webSocket({ url: 'wss://echo.websocket.org', openObserver: o ...

  3. JavaScript入门学习(2)--进度条

    <html> <style type="text/css"> #bar{width:0px; height:20px; background:#ee00ff ...

  4. Altium Designer (DXP) 复制粘贴,放器件 出错报异常的原因

    安装好Altium Designer (DXP) 09板和2013后,运行后原理图复制粘贴元件或者放心的器件后出现下面异常, 原因是: 1.未启动 Print Spooler服务 2.没有安装任何打印 ...

  5. python列表的通用操作

    #'+'和'*'#+可以将两个列表拼接为一个列表my_list = [1,2,3]+[4,5,6]#*可以将列表重复指定的次数my_list = [1,2,3]*5 print(my_list) #创 ...

  6. 利用Docker设置Node.js

      docker是一个开源的应用容器引擎,可以为我们提供安全.可移植.可重复的自动化部署的方式.docker采用虚拟化的技术来虚拟化出应用程序的运行环境.如上图一样.docker就像一艘轮船.而轮船上 ...

  7. 155. Minimum Depth of Binary Tree【LintCode by java】

    Description Given a binary tree, find its minimum depth. The minimum depth is the number of nodes al ...

  8. NoSQL入门第一天——NoSQL入门与基本概述

    一.课程大纲 二.入门概述 1.为什么用NoSQL 单机MySQL的年代: 一个网站的访问量一般都不大,用单个数据库完全可以轻松应付. 我们来看看数据存储的瓶颈是什么? 1.数据量的总大小 一个机器放 ...

  9. 20155316 2016-2017-2《Java程序设计》课程总结

    每周作业 链接汇总 预备作业1:学习调查(专业期望 师生关系 代码行数) 预备作业2:"做中学"调查(日常技能 C语言 Java 公文写作) 预备作业3:Linux系统与虚拟机学习 ...

  10. 优步uber司机申请了为什么一直没有通过审核,帐号也显示未激活

    优步uber现在是越来越火,申请注册成为优步uber司机的人数也日剧增多,申请了的车主都知道,申请后要等待审核,审核通过才可以激活帐号,快的运气好的,三五天不到一个星期就激活了,慢点的得大半个月,还有 ...