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. JSP介绍与语法-java之JSP学习第一天(非原创)

    文章大纲 一.JSP 简介二.JSP 生命周期三.JSP 语法四.学习资料下载五.参考文章   一.JSP 简介 1. 什么是Java Server Pages? JSP全称Java Server P ...

  2. JSOI 2009 BZOJ 1444 有趣的游戏

    题面 题目描述 小阳阳发明了一个有趣的游戏:有n个玩家,每一个玩家均有一个长度为 l 的字母序列,任何两个玩家的字母序列不同.共有m种不同的字母,所有的字母序列都由这m种字母构成,为了方便,我们取大写 ...

  3. 解决 java.sql.SQLException: Before start of result set

    java中使用如下代码做数据库连接,用以查询数据 *******************我是分割线************************************* try { Class.f ...

  4. 最长递增子序列问题—LIS

    问题:给定一组数 a0,a0,....,an-1. 求该序列的最长递增(递减)序列的长度. 最长递增子序列长度的求法有O(n^2)和O(nlogn)两种算法. 1.复杂度为O(n^2)的算法. 设L[ ...

  5. tts和字符集的关系--要求源和目的端的数据库字符集必须一样,国家字符集必须一样。

    tts和字符集的关系--要求源和目的端的数据库字符集必须一样,国家字符集必须一样. imp sys/as TRANSPORT_TABLESPACE=Y datafiles= C:\oracle\pro ...

  6. 如何查看在Heroku上部署了那些站点

        使用以下命令查看 Heroku 站点地址:   $ heroku domains   例如: http://peaceful-springs-94972.herokuapp.com/signu ...

  7. Webpack DLL

    参考自:https://www.jianshu.com/p/a5b3c2284bb6 在用 Webpack 打包的时候,对于一些不经常更新的第三方库,比如 react,lodash,我们希望能和自己的 ...

  8. 使用jquey的css()方法改变样式,

    $("#tip").css("display","none"); $("#tip").css("display ...

  9. css:选择器

    http://blog.csdn.net/xyz121323693/article/details/8516297 交集选择器 并集选择器 后代选择器 子代选择器 http://www.cnblogs ...

  10. shell脚本实现定时重启任务并输出日志信息

    #!/bin/bash #当前日期 time=`date` pidno=`ps aux|grep adserver-beta|grep -v "grep"|awk '{print ...