[题目链接]

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

[算法]

将每个单词的首字母向尾字母连一条有向边,判断欧拉路径是否存在,即可

[代码]

  1. #include <algorithm>
  2. #include <bitset>
  3. #include <cctype>
  4. #include <cerrno>
  5. #include <clocale>
  6. #include <cmath>
  7. #include <complex>
  8. #include <cstdio>
  9. #include <cstdlib>
  10. #include <cstring>
  11. #include <ctime>
  12. #include <deque>
  13. #include <exception>
  14. #include <fstream>
  15. #include <functional>
  16. #include <limits>
  17. #include <list>
  18. #include <map>
  19. #include <iomanip>
  20. #include <ios>
  21. #include <iosfwd>
  22. #include <iostream>
  23. #include <istream>
  24. #include <ostream>
  25. #include <queue>
  26. #include <set>
  27. #include <sstream>
  28. #include <stdexcept>
  29. #include <streambuf>
  30. #include <string>
  31. #include <utility>
  32. #include <vector>
  33. #include <cwchar>
  34. #include <cwctype>
  35. #include <stack>
  36. #include <limits.h>
  37. using namespace std;
  38. #define MAXN 100010
  39. #define MAXLEN 1010
  40. #define MAXC 30
  41. const int M = ;
  42.  
  43. struct edge
  44. {
  45. int to,nxt;
  46. } e[MAXN];
  47.  
  48. int tot;
  49. int fa[MAXC],head[MAXC],size[MAXC],in[MAXC],out[MAXC];
  50. char str[MAXLEN];
  51. set< int > s;
  52.  
  53. template <typename T> inline void read(T &x)
  54. {
  55. int f = ; x = ;
  56. char c = getchar();
  57. for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
  58. for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
  59. x *= f;
  60. }
  61. inline void addedge(int u,int v)
  62. {
  63. tot++;
  64. e[tot] = (edge){v,head[u]};
  65. head[u] = tot;
  66. }
  67. inline int get_root(int x)
  68. {
  69. if (fa[x] == x) return x;
  70. return fa[x] = get_root(fa[x]);
  71. }
  72. inline void merge(int u,int v)
  73. {
  74. int x = get_root(u) , y = get_root(v);
  75. if (x == y) return;
  76. if (size[x] < size[y]) swap(x,y); // Union By Rank
  77. size[x] += size[y];
  78. fa[y] = x;
  79. }
  80.  
  81. int main()
  82. {
  83.  
  84. int T;
  85. read(T);
  86. while (T--)
  87. {
  88. int n;
  89. read(n);
  90. for (int i = ; i <= ; i++)
  91. {
  92. head[i] = ;
  93. fa[i] = i;
  94. size[i] = ;
  95. in[i] = out[i] = ;
  96. }
  97. s.clear();
  98. for (int i = ; i <= n; i++)
  99. {
  100. scanf("%s",str + );
  101. int len = strlen(str + );
  102. int fir = str[] - 'a' + , lst = str[len] - 'a' + ;
  103. merge(fir,lst);
  104. addedge(fir,lst);
  105. in[lst]++; out[fir]++;
  106. s.insert(fir); s.insert(lst);
  107. }
  108. bool connect = false;
  109. int sz = (int)s.size();
  110. for (int i = ; i <= ; i++) connect |= (size[i] == sz);
  111. if (!connect)
  112. {
  113. printf("The door cannot be opened.\n");
  114. continue;
  115. }
  116. bool flag = true;
  117. for (set<int> :: iterator it = s.begin(); it != s.end(); it++) flag &= (in[*it] == out[*it]);
  118. if (flag)
  119. {
  120. printf("Ordering is possible.\n");
  121. continue;
  122. }
  123. int s1 = , s2 = ;
  124. for (set<int> :: iterator it = s.begin(); it != s.end(); it++)
  125. {
  126. s1 += ((in[*it] - out[*it]) == );
  127. s2 += ((out[*it] - in[*it]) == );
  128. if (abs(in[*it] - out[*it]) >= ) s1 = M;
  129. }
  130. if (s1 == s2 == ) printf("Ordering is possible.\n");
  131. else printf("The door cannot be opened.\n");
  132. }
  133.  
  134. return ;
  135.  
  136. }

[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 思路分析:该问题要求判断单词是否能连接成一条直线,转换为图论问题:将单词的首字母和尾字母看做一个点,每个单词描述了一条从首字母指 ...

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

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

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

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

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

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

  7. [欧拉回路] poj 1386 Play on Words

    题目链接: http://poj.org/problem?id=1386 Play on Words Time Limit: 1000MS   Memory Limit: 10000K Total S ...

  8. POJ 1386&&HDU 1116 Play on Words(我以后再也不用cin啦!!!)

    Play on Words Some of the secret doors contain a very interesting word puzzle. The team of archaeolo ...

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

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

随机推荐

  1. 微服务网关从零搭建——(八)Ocelot网关中加入skywalking APM

    准备工作 一.下载skywalking 本例使用的是 注: 1.解压后执行完2,3步骤后运行\bin\startup.bat 2.默认后台端口为8080 如需修改则修改\webapp\webapp.y ...

  2. libevent reference Mannual II--library

    FYI: http://www.wangafu.net/~nickm/libevent-book/TOC.html The Libevent Reference Manual: Preliminari ...

  3. 洛谷——P3919 【模板】可持久化数组(可持久化线段树/平衡树)

    P3919 [模板]可持久化数组(可持久化线段树/平衡树) 题目背景 UPDATE : 最后一个点时间空间已经放大 标题即题意 有了可持久化数组,便可以实现很多衍生的可持久化功能(例如:可持久化并查集 ...

  4. 日本語 IME输入法(Microsoft 输入法)切换问题

    平假名 与 片假名之间的切换 按住 Ctrl + Caps Lock(平假名) 按住 Alt + Caps Lock(片假名) 另外:语言之间的切换 Alt + Shift 键 / Windows + ...

  5. Linux 安装 MySQL 详解(rpm 包)

    说明:Linux 系统中软件的安装在 root 用户下进行,此安装方式为 rpm 包方式,安装的版本为:MySQL-5.6.25-1.linux_glibc2.5.x86_64.rpm-bundle. ...

  6. textbook references

    * math 1. Teubner-Taschenbuch der Mathematik * CFD

  7. list数组排序---stream

    import java.util.*;import java.util.stream.Collector;import java.util.stream.Collectors; public clas ...

  8. [bzoj4567][Scoi2016][背单词] (贪心+trie树)

    Description Lweb 面对如山的英语单词,陷入了深深的沉思,“我怎么样才能快点学完,然后去玩三国杀呢?”.这时候睿智 的凤老师从远处飘来,他送给了 Lweb 一本计划册和一大缸泡椒,他的计 ...

  9. [bzoj4247][挂饰] (动规+排序)

    Description JOI君有N个装在手机上的挂饰,编号为1...N. JOI君可以将其中的一些装在手机上. JOI君的挂饰有一些与众不同——其中的一些挂饰附有可以挂其他挂件的挂钩.每个挂件要么直 ...

  10. java多线程synchronized volatile解析

    先简单说说原子性:具有原子性的操作被称为原子操作.原子操作在操作完毕之前不会线程调度器中断.即一个操作或者多个操作 要么全部执行并且执行的过程不会被任何因素打断,要么就都不执行.在Java中,对除了l ...