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. AMD 的 CommonJS wrapping

    其实本文的标题应该是「为什么我不推荐使用 AMD 的 Simplified CommonJS wrapping」,但太长了不好看,为了美观我只能砍掉一截. 它是什么? 为了复用已有的 CommonJS ...

  2. Context都没弄明白,还怎么做Android开发?

    Activity mActivity =new Activity() 作为Android开发者,不知道你有没有思考过这个问题,Activity可以new吗?Android的应用程序开发采用JAVA语言 ...

  3. php正则表达式取子字符串及替换

    最近在学习如何用php编写cms,想把文章中的第一个图片提取出来当做缩略图显示到前面,想到的方法就是把文章内容作为一个大字符串,然后用正则表达式找出匹配出第一次出现<img src=" ...

  4. 立体3D方式 【转】

    目前为止,至少有四种普遍使用的立体3D传输格式,分别称为frame sequential(帧连续),frame packing(帧封装),side-by-side(并排),以及checkerboard ...

  5. 优化算法——拟牛顿法之L-BFGS算法

    一.BFGS算法 在"优化算法--拟牛顿法之BFGS算法"中,我们得到了BFGS算法的校正公式: 利用Sherman-Morrison公式可对上式进行变换,得到 令,则得到: 二. ...

  6. TensorFlow笔记六:基于cifar10数据库的AlexNet识别

    准确率只有70%,cpu版本的TF居然跑了两天才跑完,其他方法将继续尝试. 生成数据目录: import numpy as np import os train_label = {} for i in ...

  7. 2016.7.12 eclispe使用mybatis generator生成代码时提示project E is not exist

    运行mybatis-generator之后,出现错误:project E is not exist   错误原因:使用了项目的绝对路径. http://bbs.csdn.net/topics/3914 ...

  8. 我用select做多路复用踩到的坑

    既然说是用select踩到的坑,那么就先直接贴一段使用select的代码上来瞅一下: bool SocketAction(int fd, const char* buf, size_t len, ui ...

  9. apue学习笔记(第八章 进程控制)

    本章介绍UNIX系统的进程控制,包括创建新进程.执行程序和进程终止. 进程标识 每一个进程都有一个非负整数表示的唯一进程ID,除了进程ID,每个进程还有一些其他标识符.下列函数返回这些标识符 #inc ...

  10. 地图之CLLocationManager的使用 定位功能使用

    1.iOS8曾经使用CLLocationManager 1.导入头文件 <CoreLocation/CoreLocation.h> 2.创建位置管理者 CLLocationManager ...