Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 8571   Accepted: 2997

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. 题意:给出n个单词,问所有的单词能否首尾相连(能相连的单词的首和尾必须是相同的); 思路:一道判断有向图欧拉路的题目;
   可以将每个单词的首和尾看作节点,判断图的连通性可以用并查集,每输入一个单词将其首和尾加入集合中,最后任取一个节点判断其他所有节点和该节点是否有共同的祖先,若是,则是连通的,否则不连通;
     在连通性的前提下,若所有节点的入读等于出度 或者 一个节点的入度比出度大1 一个节点的入度比出度小1,说明有欧拉路,否则没有欧拉路;
因为手误,贡献一次WA;
 #include<stdio.h>
#include<string.h> int set[],indegree[],outdegree[],vis[];
int count; void init()
{
for(int i = ; i < ; i++)
set[i] = i;
} int find(int x)
{
if(set[x] != x)
set[x] = find(set[x]);
return set[x];
} int check()
{
int x,i;
int flag = ;
for(i = ; i < ; i++)
{
if(vis[i])
{
if(flag == )
{
x = find(i);
flag = ;
}
else
{
if(find(i) != x)
break;
}
}
}
if(i < )
return ;//图是不连通的,直接返回; int c1 = , c2 = , c3 = ;
for(int i = ; i < ; i++)
{
if(vis[i])
{
if(indegree[i] == outdegree[i])
c1++;
else if(indegree[i]-outdegree[i] == )
c2++;
else if(outdegree[i]-indegree[i] == )
c3++;
}
}
if((c2 == && c3 == && c1 == count-) ||(c1 == count))
return ;
else return ;
} int main()
{
int test,n;
char s[];
scanf("%d",&test);
while(test--)
{
memset(indegree,,sizeof(indegree));
memset(outdegree,,sizeof(outdegree));
memset(vis,,sizeof(vis));
init();
count = ; scanf("%d",&n);
for(int i = ; i <= n; i++)
{
scanf("%s",s);
int len = strlen(s);
int u = s[]-'a';
if(!vis[u])
{
vis[u] = ;
count++;
}
int v = s[len-]-'a';
if(!vis[v])
{
vis[v] = ;
count++;
} indegree[u]++;
outdegree[v]++;
int tu = find(u);
int tv = find(v);
if(tu != tv)
set[tu] = tv;
} if(check())
printf("Ordering is possible.\n");
else printf("The door cannot be opened.\n");
}
return ;
}

欧拉路:图G,若存在一条路,经过G中每条边有且仅有一次,称这条路为欧拉路,如果存在一条回路经过G每条边有且仅有一次,

称这条回路为欧拉回路。具有欧拉回路的图成为欧拉图。

判断欧拉路是否存在的方法

有向图:图连通,有一个顶点出度大入度1,有一个顶点入度大出度1,其余都是出度=入度。

无向图:图连通,只有两个顶点是奇数度,其余都是偶数度的。

判断欧拉回路是否存在的方法

有向图:图连通,所有的顶点出度=入度。

无向图:图连通,所有顶点都是偶数度。

其中判断图的连通性用并查集。

Play on Words(有向图欧拉路)的更多相关文章

  1. POJ1386Play on Words[有向图欧拉路]

    Play on Words Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11846   Accepted: 4050 De ...

  2. poj 1386 Play on Words(有向图欧拉路+并查集)

    题目链接:http://poj.org/problem?id=1386 思路分析:该问题要求判断单词是否能连接成一条直线,转换为图论问题:将单词的首字母和尾字母看做一个点,每个单词描述了一条从首字母指 ...

  3. 欧拉路&&欧拉回路 概念及其练习

    欧拉路: 如果给定无孤立结点图G,若存在一条路,经过图中每边一次且仅一次,这条路称为欧拉路: 如果给定无孤立结点图G,若存在一条回路,经过图中每边一次且仅一次,那么该回路称为欧拉回路. 存在欧拉回路的 ...

  4. HihoCoder1644 : 完美命名的烦恼([Offer收割]编程练习赛37)(有向图的一笔画问题||欧拉路)

    描述 程序员常常需要给变量命名.给函数命名.给项目命名.给团队命名…… 好的名字可以大大提高程序员的主观能动性,所以很多程序员在起名时都会陷入纠结和烦恼. 小Hi希望给新的项目起个完美的名字.首先小H ...

  5. hdu1161 欧拉路

    欧拉路径是指能从一个点出发能够“一笔画”完整张图的路径:(每条边只经过一次而不是点) 在无向图中:如果每个点的度都为偶数 那么这个图是欧拉回路:如果最多有2个奇数点,那么出发点和到达点必定为该2点,那 ...

  6. POJ 1637 Sightseeing tour (混合图欧拉路判定)

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6986   Accepted: 2901 ...

  7. hihoCoder #1182 欧拉路·三 (变形)

    题意: 写出一个环,环上有2^n个格子,每个格子中的数字是0或1,相连着的n个格子可以组成一个数的二进制,要求给出这2^n个数字的序列,使得组成的2^n个数字全是不同的.(即从0到2^n-1) 思路: ...

  8. hiho 1182 : 欧拉路·三

    1182 : 欧拉路·三 这时题目中给的提示: 小Ho:是这样的,每次转动一个区域不是相当于原来数字去掉最左边一位,并在最后加上1或者0么. 于是我考虑对于"XYYY",它转动之后 ...

  9. Play on Words(欧拉路)

    http://poj.org/problem?id=1386 题意:给定若干个单词,若前一个的尾字母和后一个单词的首字母相同,则这两个单词可以连接,问是否所有的单词都能连接起来. 思路:欧拉路的判断, ...

随机推荐

  1. Android 中 SQLite 性能优化

    数据库是应用开发中常用的技术,在Android应用中也不例外.Android默认使用了SQLite数据库,在应用程序开发中,我们使用最多的无外乎增删改查.纵使操作简单,也有可能出现查找数据缓慢,插入数 ...

  2. Android(java)学习笔记203:网页源码查看器(Handler消息机制)

    1.项目框架图: 2.首先是布局文件activity_main.xml: <LinearLayout xmlns:android="http://schemas.android.com ...

  3. Css实现透明效果,兼容IE8

    Css实现透明效果,兼容IE8 >>>>>>>>>>>>>>>>>>>>> ...

  4. php生成不重复随机字符串

    使用时间戳作为原始字符串,再随机生成五个字符随机插入任意位置,生成新的字符串,保证不重复 function rand($len) { $chars='ABCDEFGHIJKLMNOPQRSTUVWXY ...

  5. recursive - simple screenshot but detail principle.

    the code below demonstates the principle of the'recursive-call' that the programing beginner may be ...

  6. C语言字符串库函数的实现

    1.strlen(字符串的长度) size_t Strlen(const char* str) { assert(str); ;; ++i) { if (str[i] == '\0') return ...

  7. functools学习有感

    functools的内容不多,包含四个函数(partial,reduce,update_wrapper,wraps)和一个python对象(partial Objects). functools的四个 ...

  8. spring Mvc json返回json的日期格式问题

    (一)输出json数据 springmvc中使用jackson-mapper-asl即可进行json输出,在配置上有几点: 1.使用mvc:annotation-driven 2.在依赖管理中添加ja ...

  9. 设置session的生命周期(php)

    PHP中,Session变量保存在服务器端(默认以文件格式保存),而Session ID以cookie形式保存在客户端. 销毁session的方法有2种 第一种是通过程序 session_destor ...

  10. VIM中文乱码

    下面是关于VIM中文乱码问题的解决方法: 打开VIM的配制文件在里面加上一段这样的代码就可以了: set encoding=prc