Play on Words HDU - 1116

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.

InputThe 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. 
OutputYour 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. 题意:给你一些英文字母,不同的英文字母之间可以首位连接,只要字母一样,问可不可以构成一个完整的一条链
题解:一开始想把每一个单词都看作是一个点来跑,但是很难实现(有点像哈密顿图),之后我就把英文的26个字母当作是点,然后按照单词来建边,当作欧拉通路来跑。
   有向的欧拉通路:只有两个节点的出入度不一样(一个出度比入度大一,一个入度比出度大一),其余所有的点的出入度都是一样的。同时还需要判断是不是一个连通图,那就是看是不是在同一个集合(根节点是不是同一个就可以了)
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<map>
#include<cstdlib>
#include<vector>
#include<string>
#include<queue>
using namespace std; #define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
const double PI = acos(-1.0);
const int maxn = 1e3+;
const int mod = 1e9+;
int par[];
void init()
{
for(int i=;i<;i++)
par[i] = i;
}
int find(int x)
{
return par[x]!=x ? find(par[x]) : x;
}
void combine(int a,int b)
{
a = find(a);
b = find(b);
if(a != b)
par[a] = b;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int st[],en[];
memset(st,,sizeof st);
memset(en,,sizeof en);
int n;
scanf("%d",&n);
init();
for(int i=;i<n;i++)
{
char str[];
scanf("%s",str);
int a=str[]-'a';
int b=str[strlen(str)-]-'a';
st[a]++;
en[b]++;
combine(a,b);
}
int start;
for(int i=;i<;i++)
{
if((st[i] || en[i]) && i == find(i))
{
//cout<<i<<endl;
start = i;
break;
}
}
int ans = ;
bool connect = ;
for(int i=;i<;i++)
{
ans += abs(st[i]-en[i]);
if((st[i] || en[i]) && start != find(i))
connect = ;
}
if(ans > || connect == )
puts("The door cannot be opened.");
else
puts("Ordering is possible.");
}
}

Play on Words HDU - 1116 (并查集 + 欧拉通路)的更多相关文章

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

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

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

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

  3. hdu 1116 并查集判断欧拉回路通路

    判断一些字符串能首尾相连连在一起 并查集求欧拉回路和通路 Sample Input 3 2 acm ibm 3 acm malform mouse 2 ok ok Sample Output The ...

  4. hdu 1116(并查集+欧拉路径)

    Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  5. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  6. HDU 3926 并查集 图同构简单判断 STL

    给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...

  7. HDU 4496 并查集 逆向思维

    给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...

  8. POJ 2513 无向欧拉通路+字典树+并查集

    题目大意: 有一堆头尾均有颜色的木条,要让它们拼接在一起,拼接处颜色要保证相同,问是否能够实现 这道题我一开始利用map<string,int>来对颜色进行赋值,好进行后面的并查操作以及欧 ...

  9. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...

随机推荐

  1. 前端专业术语: shim 和 Polyfill,了解下

    在学习和使用 JavaScript 的时候,我们会经常碰到两个术语:shim 和 polyfill.它们有许多定义和解释,意思相近又有差异. Shim Shim 指的是在一个旧的环境中模拟出一个新 A ...

  2. ashx是什么文件

    ashx是什么文件 .ashx 文件用于写web handler的..ashx文件与.aspx文件类似,可以通过它来调用HttpHandler类,它免去了普通.aspx页面的控件解析以及页面处理的过程 ...

  3. SpringMVC10 InitBinder 注册自定义编辑器

    1.配置web.xml文件 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3// ...

  4. A light-weight client-side OAuth library for Java

    这个是一个Github上的开源项目-Signpost,主要封装了一些OAuth认证类的方法,项目地址:电极打开 Signpost 什么是Signpost Signpost是一种非常容易.直观的HTTP ...

  5. nodejs入门学习笔记二——解决阻塞问题

    在最开始,我们要弄清楚node会什么会存在阻塞? node是这么标榜自己的:“在node中除了代码,所有一切都是并行执行的!” 意思是,Node.js可以在不新增额外线程的情况下,依然可以对任务进行并 ...

  6. mysql5.6.31安装及配置

    1,下载安装包解压到安装位置.下载地址https://dev.mysql.com/downloads/mysql/5.6.html#downloads 2,修改默认配置文件 在根目录下面有my-def ...

  7. echarts折柱混合(图表数据与x轴对应显示)

    一天24个小时,每个小时不一定都有对应的数据,所以后台给出的数据,只有每个时间点对应的数据,比如4点,给的是112,5点的242,其他时间没有,则只显示4点,5点时候的数据,那么现在对应的时间点就是后 ...

  8. 使用FusionCharts创建可更新数据的JavaScript图表

    先创建一个简单的图表,然后改变它的数据(请参见下面的代码).图表最初据显示8月份的销售数据,当用户点击按钮时改为显示9月份的销售数据.每个月都有单独的XML文件,代码如下: <html> ...

  9. IIS发布网站出错解决方案

    1.第一类错误(Web服务器被配置为不列出此目录的内容) 问题所在没有为请求的URL设置默认文档,在IIS“默认文档”添加一个你要访问的默认文档名字,如:Default.aspx. 2.第二类错误(请 ...

  10. uLua学习之使用协程(终)

    前言 今天是本系列的第六篇文章,也是最后一篇,我们来看看uLua中如何来实现协程吧.首先,让我们明确协程的概念.在百度百科上的是这样说的,协程更适合于用来实现彼此熟悉的程序组件,如合作式多任务,迭代器 ...