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. partial局部类

    局部类型允许我们将一个类.接口或结构分成好几个部分,分别实现在几个不同的.cs文件中. 局部类型适用于以下情况: (1)类型特别大,不宜放在一个文件中实现. (2)一个类型中的一部分代码为自动化工具生 ...

  2. JAVA导出Excel封装

    1.数据bean public class ExcelBean { private String name; private String sheetName; private ExcelTitle[ ...

  3. java web(jsp)-The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

    在静态项目上新建 jsp文件的时候,报错:The superclass "javax.servlet.http.HttpServlet" was not found on the ...

  4. sql server 2005 大数据量插入性能对比

    sql server 2005大数据量的插入操作 第一,写个存储过程,传入参数,存储过程里面是insert操作, 第二,用System.Data.SqlClient.SqlBulkCopy实例方法, ...

  5. ubuntu桌面变空白,或者只有壁纸,任务栏消失的解决办法

    原因:因为打开了桌面特效的原因,但设置不合导致的. 解决方法:方法一:1.按住Ctrl+Alt+F1切换到字符终端下,输入用户名和密码登录2.输入以下命令删除出错的Compiz配置文件相关目录:rm ...

  6. Linux 自动更新时间

    1. 从NTP上把时间同步到本地 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 2. 更新本地时间 ntpdate us.pool.ntp.o ...

  7. 常用的dos命令之简略总结

    Dos常用命令  一.基础命令  1 dir  无参数:查看当前所在目录的文件和文件夹.  /s:查看当前目录已经其所有子目录的文件和文件夹.  /a:查看包括隐含文件的所有文件.  /ah:只显示出 ...

  8. hdoj 2602(背包)

    Problem D Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

  9. OC文件操作(2)

    NSFileManager 文件管理器完成文件的创建.移动.拷贝等管理操作 1.查询文件和目录  OC中查询路径下的目录主要分为浅度遍历和深度遍历.  浅度遍历  NSFileManager * ma ...

  10. extend简单用法

    eg:var obj1=[{a:1,b:2},{a:2,b:3}] var obj2=[{c:3,d:2},{c:4,d:3}] var resultArray=[]; for (var i = 0; ...