Play on Words

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 5622    Accepted Submission(s): 1850
Problem 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.

题意:给出几个字符串。假设一个字符串的首字符(尾子符)等于另外一个字符串的尾子符(首字符),就让他们连接起来。问最后能不能把全部的字符串都连接起来。

分析:非常明显的是要用到并查集的仅仅是。可是处理首尾字符的时候会有点麻烦,我们最好还是将没一个字符的首尾字符都视为一个点,一个字符串就是一条边,那么该题就转化为了求边能不能形成一条连通图,之后就要用欧拉路来推断改图是否连通就好了。

注:欧拉路分为欧拉回路和欧拉通路。

欧拉通路:满足从一点出发经过每一条边且仅仅经过一次,能把全部的边都经过的路

欧拉回路:欧拉通路而且最后回到原点的路;

假设是欧拉回路那么图中每一个点的入读和处度都相等

假设是通路那么起始点的出度减入度为1, 终点处入度减出度为1。

代码:

/*hdoj 1116 并查集+欧拉通/回路*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define M 1005 int out[26], in[26], fat[26];
bool vis[26];
char s[M]; int f(int x){
if(x != fat[x]) fat[x] = f(fat[x]);
return fat[x];
} void merge(int x, int y){
int a = f(x);
int b = f(y);
if(a != b) fat[a] = b;
} int main(){
int n, t, i;
scanf("%d", &t);
while(t --){
memset(vis, 0, sizeof(vis));
memset(out, 0, sizeof(out));
memset(in, 0, sizeof(in));
scanf("%d", &n);
for(i = 0; i < 26; i ++) fat[i] = i;
for(i = 0; i < n; i ++){
scanf("%s", s);
int x = s[0]-'a';
int y = s[strlen(s)-1]-'a';
merge(x, y);
++out[x]; ++in[y];
vis[x] = vis[y] = 1;
}
int flag1 = 0;
for(i = 0; i < 26; i ++){ //推断是否联连通
if(vis[i]&&fat[i] == i) ++flag1;
}
if(flag1 > 1){
printf("The door cannot be opened.\n"); continue;
}
int flag2, flag3; //flag1是推断是否是所有出入度都相等,flag2是判读起始点有几个,flag3是终点有几个
flag1 = flag2 = flag3 = 0;
for(i = 0; i < 26; i ++){
if(vis[i]&&out[i] != in[i]){
++flag1;
if(out[i]-in[i] == 1) ++flag2;
if(in[i] - out[i] == 1) ++flag3;
}
}
if(flag1 == 0) printf("Ordering is possible.\n");
else if(flag1 == 2&&flag2 == 1&&flag3 == 1) printf("Ordering is possible.\n");
else printf("The door cannot be opened.\n");
}
return 0;
}

hdoj 1116 Play on Words 【并查集】+【欧拉路】的更多相关文章

  1. Colored Sticks (字典树哈希+并查集+欧拉路)

    Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27704   Accepted: 7336 Description You ...

  2. poj2513--并查集+欧拉路+字典树

    经典好题,自己不知道哪里错了交上去是RE,可能是数组开的不好吧,字典树老碰到这种问题.. 先马上别人的代码,有空对拍看看 #include <cstdio> #include <cs ...

  3. poj 2513 Colored Sticks (trie树+并查集+欧拉路)

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 40043   Accepted: 10406 ...

  4. NYOJ--42--dfs水过||并查集+欧拉通路--一笔画问题

    dfs水过: /* Name: NYOJ--42--一笔画问题 Author: shen_渊 Date: 18/04/17 15:22 Description: 这个题用并查集做,更好.在练搜索,试试 ...

  5. BZOJ 1116 [POI2008]CLO(并查集)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1116 [题目大意] Byteotia城市有n个towns,m条双向roads.每条ro ...

  6. hdoj 2473 Junk-Mail Filter【并查集节点的删除】

    Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  7. POJ 2513 Colored Sticks (离散化+并查集+欧拉通路)

    下面两个写得很清楚了,就不在赘述. http://blog.sina.com.cn/s/blog_5cd4cccf0100apd1.htmlhttp://www.cnblogs.com/lyy2890 ...

  8. Colored Sticks POJ - 2513 并查集+欧拉通路+字典树hash

    题意:给出很多很多很多很多个棒子 左右各有颜色(给出的是单词) 相同颜色的可以接在一起,问是否存在一种 方法可以使得所以棒子连在一起 思路:就是一个判欧拉通路的题目,欧拉通路存在:没奇度顶点   或者 ...

  9. Play on Words HDU - 1116 (并查集 + 欧拉通路)

    Play on Words HDU - 1116 Some of the secret doors contain a very interesting word puzzle. The team o ...

  10. hdoj 4786 Fibonacci Tree【并查集+最小生成树(kruskal算法)】

    Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

随机推荐

  1. IDEA阅读源码的技巧

    目录 1. 查看当前类内容 2. 查看当前类的继承体系 3. 查看当前方法的调用链 本教程仅支持 MAC 系统下的 IDEA 开发工具,如果需要 Windows 对应的操作起自行替代相应快捷键即可. ...

  2. AbstractFactory

    定义:为创建一组相关或相互依赖的对象提供一个接口,而且无需指定他们的具体类. (1)定义产品接口 /** * 第一种系列的产品 * @author Administrator * */ interfa ...

  3. [BUG] CS0234: 命名空间“System.Web.Mvc”中不存在类型或命名空间名称“Ajax”(是否缺少程序集引用?)

    写在前面 在浏览view的时候,突然出现这个错误,不过还是很好解决的. bug 解决方案

  4. java查看工具jhat-windows

    Analyzes the Java heap. This command is experimental and unsupported. Synopsis jhat [ options ] heap ...

  5. HDU 1875 畅通project再续 (最小生成树 水)

    Problem Description 相信大家都听说一个"百岛湖"的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其它的小岛时都要通过划小船来实现. 如今政府决定大力发展百岛 ...

  6. [ACM] POJ 1068 Parencodings(模拟)

    Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19352   Accepted: 11675 De ...

  7. OpenCV实现图像颜色特征提取

    https://github.com/ictlyh/ImageFeature 链接:http://pan.baidu.com/s/1mhUoPxI 密码:3cnn

  8. Windows 10 1703创意者更新官方ISO镜像大全

    2017年04月07日 20:00 19867 次阅读 稿源:快科技 12 条评论 Windows 10 Creators Update创意者更新正式版已经发布,目前只能通过易生.MCT工具或者ISO ...

  9. 关于安装oracle 11g client 出现安装先决条件检查全部失败

    本文转自:https://blog.csdn.net/iloli/article/details/45244159 今天我在安装Oracle11gClient时,全部显示成N/A,Oracle无法执行 ...

  10. Python结合NC.exe 实现模拟登录&批量填表

    1.工作需求 有很多事项,每个事项分为:名称.种类.时间等,需要把每个事项逐个输入到网页中并提交. 如果用人肉操作的话,流程就是先登录到网站后台,点击“添加”——>输入各项内容——>点击“ ...