题目链接:

http://poj.org/problem?id=1386

Play on Words
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9685   Accepted: 3344

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

  1. 3
  2. 2
  3. acm
  4. ibm
  5. 3
  6. acm
  7. malform
  8. mouse
  9. 2
  10. ok
  11. ok

Sample Output

  1. The door cannot be opened.
  2. Ordering is possible.
  3. The door cannot be opened.

Source

[Submit]   [Go Back]   [Status]  
[Discuss]

题目意思:

给n个单词。假设A单词的最后一个字母和B单词的第一个字母是一样的。则说明A和B可以连在一起。

问全部单词是否连成一串。

解题思路:

推断连通性+欧拉通路

仅仅考虑单词的首字母和末尾字母。把出现的字母当成节点,一个单词的首字母和尾字母连成一条边。

先用并查集推断是否连通。然后推断是否有欧拉通路。

代码:

  1. //#include<CSpreadSheet.h>
  2.  
  3. #include<iostream>
  4. #include<cmath>
  5. #include<cstdio>
  6. #include<sstream>
  7. #include<cstdlib>
  8. #include<string>
  9. #include<string.h>
  10. #include<cstring>
  11. #include<algorithm>
  12. #include<vector>
  13. #include<map>
  14. #include<set>
  15. #include<stack>
  16. #include<list>
  17. #include<queue>
  18. #include<ctime>
  19. #include<bitset>
  20. #include<cmath>
  21. #define eps 1e-6
  22. #define INF 0x3f3f3f3f
  23. #define PI acos(-1.0)
  24. #define ll __int64
  25. #define LL long long
  26. #define lson l,m,(rt<<1)
  27. #define rson m+1,r,(rt<<1)|1
  28. #define M 1000000007
  29. //#pragma comment(linker, "/STACK:1024000000,1024000000")
  30. using namespace std;
  31.  
  32. #define Maxn 30
  33.  
  34. bool vis[Maxn];
  35. int dei[Maxn],deo[Maxn],fa[Maxn],n;
  36. char sa[1100];
  37.  
  38. int Find(int x)
  39. {
  40. int temp=x;
  41. while(fa[x]!=x)
  42. x=fa[x];
  43. while(fa[temp]!=x)
  44. {
  45. int cur=fa[temp];
  46. fa[temp]=x;
  47. temp=cur;
  48. }
  49. return x;
  50. }
  51. void Unio(int x,int y)
  52. {
  53. x=Find(x),y=Find(y);
  54. if(x!=y)
  55. fa[x]=y;
  56. }
  57.  
  58. int main()
  59. {
  60. //freopen("in.txt","r",stdin);
  61. //freopen("out.txt","w",stdout);
  62. int t;
  63.  
  64. scanf("%d",&t);
  65. while(t--)
  66. {
  67. scanf("%d",&n);
  68. memset(vis,false,sizeof(vis));
  69. memset(dei,0,sizeof(dei));
  70. memset(deo,0,sizeof(deo));
  71.  
  72. for(int i=1;i<=26;i++)
  73. fa[i]=i;
  74.  
  75. while(n--)
  76. {
  77. scanf("%s",sa+1);
  78. int len=strlen(sa+1);
  79. deo[sa[1]-'a'+1]++;
  80. dei[sa[len]-'a'+1]++;
  81. vis[sa[1]-'a'+1]=true;
  82. vis[sa[len]-'a'+1]=true;
  83. Unio(sa[1]-'a'+1,sa[len]-'a'+1);
  84. }
  85.  
  86. int la=-1,nui=0,nuo=0;
  87. bool ans=true;
  88.  
  89. for(int i=1;i<=26;i++)
  90. {
  91. if(vis[i])
  92. {
  93. if(la==-1)
  94. la=Find(i);
  95. else if(la!=Find(i))
  96. {
  97. //printf("->i:%d la:%d\n",i,la);
  98. //system("pause");
  99. ans=false;
  100. break;
  101. }
  102. if(dei[i]-deo[i]==1)
  103. nui++;
  104. else if(deo[i]-dei[i]==1)
  105. nuo++;
  106. else if(deo[i]!=dei[i])
  107. {
  108. ans=false;
  109. break;
  110. }
  111. //printf("i:%d la:%d\n",i,la);
  112. //system("pause");
  113.  
  114. }
  115. }
  116. if(!ans||nui>=2||nuo>=2||(nui+nuo)==1)
  117. printf("The door cannot be opened.\n");
  118. else
  119. printf("Ordering is possible.\n");
  120.  
  121. }
  122. return 0;
  123. }

[欧拉回路] poj 1386 Play on Words的更多相关文章

  1. POJ 1386 Play on Words(欧拉图的判断)

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

  2. poj 1386 Play on Words 有向欧拉回路

    题目链接:http://poj.org/problem?id=1386 Some of the secret doors contain a very interesting word puzzle. ...

  3. poj 1386 Play on Words门上的单词【欧拉回路&&并查集】

    题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...

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

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

  5. HDU 1116 || POJ 1386 || ZOJ 2016 Play on Words (欧拉回路+并查集)

    题目链接 题意 : 有很多门,每个门上有很多磁盘,每个盘上一个单词,必须重新排列磁盘使得每个单词的第一个字母与前一个单词的最后一个字母相同.给你一组单词问能不能排成上述形式. 思路 :把每个单词看成有 ...

  6. POJ 1386 判断欧拉回路

    题意:要开启一扇门,n个单词是密码,n个单词中,如果一个单词的首字母和前一个单词的尾字母相同,并且每个单词都能这么连起来且只用一次,则门可以开启,否则不能开启,现给出单词,判断门是否可以开. 有向图欧 ...

  7. POJ 1386 Play on Words(单词建图+欧拉通(回)路路判断)

    题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...

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

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

  9. POJ 1386 Play on Words(欧拉路)

    http://poj.org/problem?id=1386 题意: 给出多个单词,只有单词首字母与上一个单子的末尾字母相同时可以连接,判断所有字母是否可以全部连接在一起. 思路: 判断是否存在欧拉道 ...

随机推荐

  1. hdoj--1301--Jungle Roads(克鲁斯卡尔)

    Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  2. python中对单例模式的理解

    class Foo(object): instance = None def __init__(self): pass def process(self): ' @classmethod #版本1单例 ...

  3. The python programing language

    Python is an example of high-level language. As you might infer from the name “high-level language”, ...

  4. Nginx下部署Laravel项目

    Nginx下部署Laravel项目 标签(空格分隔): php Nginx配置文件 listen 80 default_server; #listen [::]:80 default_server i ...

  5. html局部页面打印

    1.局部打印函数. function preview(oper) { if (oper < 10) { bdhtml=window.document.body.innerHTML;//获取当前页 ...

  6. C#自定义事件监视变量变化

    首先监视定义类 class Event { public delegate void tempChange(object sender, EventArgs e); public event temp ...

  7. mysql去掉密码规则的两种方式

    环境介绍:centeros 7 + mysqld5.7 当我们装完数据库以后,使用临时密码登录到数据库去更改一个简单的密码,如 set password='; 结果出现以下提示: ERROR (HY0 ...

  8. NodeJS代码调试

    1.在Chrome打开chrome://flags/#enable-devtools-experiments 2.激活Developer Tools experiments 3.重启Chrome 4. ...

  9. 【Henu ACM Round#20 B】Contest

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 根据时间和原分数. 算出对应的分数就可以了. [代码] #include <bits/stdc++.h> using n ...

  10. Spring Boot学习总结(2)——Spring Boot整合Jsp

    怎么使用jsp上面起了疑问,查阅了多方资料,找到过其他人的博客的描述,也找到了spring在github上的给出的例子,看完后稍微改动后成功 整合jsp,于是决定将整合过程记载下来. 无论使用的是那种 ...