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. AOP注解方式

    Aop,  aspect object programming  面向切面编程 功能: 让关注点代码与业务代码分离! 关注点, 重复代码就叫做关注点: 切面, 关注点形成的类,就叫切面(类)! 面向切 ...

  2. 框架使用-Sql拼接

     Sql语句拼写: 查询 DQueryDom DmoQuery(返回的整个对象) 更新 DQUpdateDom 删除 DQDeleteDom 条件 dom.Where.Conditions.Add(D ...

  3. pycharm 更改字体and背景颜色

    File-settings-Appearance&Behavior-Appearance-Theme File-settings-Editor-font

  4. 洛谷P4133 [BJOI2012]最多的方案(记忆化搜索)

    题意 题目链接 求出把$n$分解为斐波那契数的方案数,方案两两不同的定义是分解出来的数不完全相同 Sol 这种题,直接爆搜啊... 打表后不难发现$<=1e18$的fib数只有88个 最先想到的 ...

  5. 彻底解决Android 应用方法数不能超过65K的问题

    作为一名Android开发者,相信你对Android方法数不能超过65K的限制应该有所耳闻,随着应用程序功能不断的丰富,总有一天你会遇到一个异常: Conversion to Dalvik forma ...

  6. python 之Requests库学习笔记

    1.    Requests库安装 Windows平台安装说明: 直接以管理员身份打开cmd运行界面,使用pip管理工具进行requests库的安装. 具体安装命令如下: >pip instal ...

  7. LeetCode Remove Duplicates from Sorted Array删除整型数组中的重复元素并返回剩下元素个数

    class Solution { public: int removeDuplicates(int A[], int n) { ],*e=&A[]; //s指向开头第一个,e往后遍历相同的 i ...

  8. mac 下删除非空文件夹

    Linux中rmdir命令是用来删除空的目录.使用方式: rmdir [-p] dirName 参数: -p 是当子目录被删除后使它也成为空目录的话,则顺便一并删除. 举例说明:rmdir folde ...

  9. java 如何使的float保留2位或者多位小数

    方法1: float   f   =  34.232323;     BigDecimal   b  =   new  BigDecimal(f);     float   f1   =  b.set ...

  10. Windows下配置Jmeter环境变量

    一.安装SDK 1.下载并安装sdk,安装目录为D:\Program Files (x86)\Java\jdk1.7.0_01 2.配置环境变量 1)新建系统变量:JAVA_HOME = D:\Pro ...