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. 关于 PHP 程序员技术职业生涯规划

    原文地址:http://rango.swoole.com/archives/570 看到很多 PHP 程序员职业规划的文章,都是直接上来就提 Linux.PHP.MySQL.Nginx.Redis.M ...

  2. PHP关于 []

    在一个表格里,提交时,名字部分加一个[],表示数组,这样,存在多个同样名字的name.前面的value不会替代后面value,如下面 <td><input name="so ...

  3. CentOS7 配置环境

    1.安装CentOS 配置环境 (1)虚拟机中安装CentOS,进入系统使用yum命令不止正常执行…… 原因: 需要设置网卡激活 解决方法: vi /etc/sysconfig/network-scr ...

  4. python——matplotlib图像的基本处理

    1.绘制图像中的点和线 from PIL import Image from pylab import * im = array(Image.open('E:\Python\meinv.jpg')) ...

  5. Android面试收集录9 IntentService详解

    一. 定义 IntentService是Android里面的一个封装类,继承自四大组件之一的Service. 二.作用 处理异步请求,实现多线程 三. 工作流程 注意:若启动IntentService ...

  6. MyBatis---自动创建表

    该项目基于Maven实现 该项目实现了在项目启动时,对数据库表进行操作 源码下载 实现步骤: 1.向pom.xml文件添加maven依赖 <dependency> <groupId& ...

  7. 3670: [Noi2014]动物园

    题目链接 题意:给n个字符串,求出每个字符串的num值,加1后相乘.num[i]表示1~i中,有多少没有重叠的公共前缀后缀. 分析: kmp中p数组表示最大的公共前缀后缀.设一cnt数组,表示1~i中 ...

  8. vim 简单命令

    (1)查找结果全部单独显示 命令: :lvimgrep /pattern/ % | lopen (2)设置文本高亮 命令: :colorscheme evening 把 ":colorsch ...

  9. python-使用unittest和ddt实现数据驱动

    一.数据驱动的概念 相同测测试脚本使用不同的测试数据来执行,测试数据和测试行为完全分离,这样的测试脚本设计模式成为数据驱动.测试框架使用unittest 和ddt模块相结合的方式 二.unittest ...

  10. cloud-init介绍及源码解读

    https://zhuanlan.zhihu.com/p/27664869 知乎大神写的