hdu 1116 Play on Words 欧拉路径+并查集
Play on Words
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7791 Accepted Submission(s): 2676
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 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.
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.".
2
acm
ibm
3
acm
malform
mouse
2
ok
ok
Ordering is possible.
The door cannot be opened.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define esp 0.00000000001
const int N=1e5+,M=1e6+,inf=1e9;
const ll INF=1e18+;
int fa[],du[],flag[];
int Find(int x)
{
return x==fa[x]?x:fa[x]=Find(fa[x]);
}
void update(int u,int v)
{
int x=Find(u);
int y=Find(v);
if(x!=y)
{
fa[x]=y;
}
}
void init()
{
for(int i=;i<=;i++)
fa[i]=i;
memset(du,,sizeof(du));
memset(flag,,sizeof(flag));
}
char a[N];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
int n,p=-;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%s",a);
int u=a[]-'a'+;
int v=a[strlen(a)-]-'a'+;
update(u,v);
du[u]++;
du[v]--;
flag[u]++;
flag[v]++;
}
int ans1=,ans2=;
for(int i=;i<=;i++)
{
if(!flag[i])
continue;
if(du[i]==)
{
if(p==-)
p=Find(i);
if(Find(i)!=p)
{
ans1=-;
break;
}
}
if(du[i]==-)
ans1++;
if(du[i]==)
ans2++;
if(du[i]<=-||du[i]>=)
{
ans1=-;
break;
}
}
if((ans1==&&ans2==)||(ans1==&&ans2==))
printf("Ordering is possible.\n");
else
printf("The door cannot be opened.\n");
}
return ;
}
hdu 1116 Play on Words 欧拉路径+并查集的更多相关文章
- Play on Words HDU - 1116(欧拉路判断 + 并查集)
题意: 给出几个单词,求能否用所有的单词成语接龙 解析: 把每个单词的首字母和尾字母分别看作两个点u 和 v,输入每个单词后,u的出度++, v的入度++ 最后判断是否能组成欧拉路径 或 欧拉回路,当 ...
- hdu 5458 Stability(树链剖分+并查集)
Stability Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)Total ...
- [HDU 3712] Fiolki (带边权并查集+启发式合并)
[HDU 3712] Fiolki (带边权并查集+启发式合并) 题面 化学家吉丽想要配置一种神奇的药水来拯救世界. 吉丽有n种不同的液体物质,和n个药瓶(均从1到n编号).初始时,第i个瓶内装着g[ ...
- HDU 1116 Play on Words(并查集和欧拉回路)(有向图的欧拉回路)
Play on Words Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- hdu 5652 India and China Origins 并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题目大意:n*m的矩阵上,0为平原,1为山.q个询问,第i个询问给定坐标xi,yi,表示i年后这 ...
- hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them
http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...
- HDU 5458 Stability (树链剖分+并查集+set)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 给你n个点,m条边,q个操作,操作1是删边,操作2是问u到v之间的割边有多少条. 这题要倒着做才 ...
- hdu 4619 Warm up 2(并查集)
借用题解上的话,就是乱搞题.. 题意理解错了,其实是坐标系画错了,人家个坐标系,我给当矩阵画,真好反了.对于题目描述和数据不符的问题,果断相信数据了(这是有前车之鉴的hdu 4612 Warm up, ...
- hdu 5441 Travel 离线带权并查集
Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...
随机推荐
- 我是如何对网站CSS进行架构的
by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu.com/wordpress/?p=944 一.写在前面的 ...
- jquery选择器中两个class是什么意思?
jquery选择器中两个class是什么意思? $(".class1 .class2") 选择class1元素下class2的元素(中间有空格)$(".class1.cl ...
- ubuntu硬件配置查看命令
主板:sudo dmidecode |grep -A16 "System Information$"
- 电影TS、TC、SCR、R5、BD、HD等版本是什么意思
在很多电影下载网站的影片标题中我们都能看到,比如<刺杀希特勒BD版>.<游龙戏凤TS版>等,这些英文缩写都是什么意思呢?都代表什么画质?以下就是各个版本的具体含义: 1.CAM ...
- 进度条,随机数---demo笔记【原创】
本人学习笔记,参考网上代码完成 makefile TEST_OBJ := rng_app MAKEOPT := ARCH=arm CROSS_COMPILE=arm-none-linux-gnueab ...
- python读入文件
举例说明吧,路径一定要带转义符‘\’,下面的例子中,每一行是一个样本的feature >>> myfile=open("D:\\301\\graduate_thesis\\ ...
- Linux中查找最耗性能的JAVA代码
在这里总结一下查找Linux.Java环境下最耗CPU性能的代码段的方法.基本上原理就是使用top命令查看最耗cpu的进程和线程(子进程).使用jstack把java线程堆栈给dump下来.然后,在堆 ...
- 21、JavaScript加强
1)回顾JS中核心内容 2)了解WEB1.0和WEB2.0时代的技术与特点 3)理解AJAX的产生背景.工作原理与特点 4)掌握AJAX常用API及应用 声明:服务端我们使用Servlet技术 ...
- PostgreSQL连接python,postgresql在python 连接,创建表,创建表内容,插入操作,选择操作,更新操作,删除操作。
安装 PostgreSQL可以用Python psycopg2模块集成. sycopg2是Python编程语言的PostgreSQL数据库的适配器. 其程序代码少,速度快,稳定.不需要单独安装这个模块 ...
- 简单LRU算法实现缓存
最简单的LRU算法实现,就是利用jdk的LinkedHashMap,覆写其中的removeEldestEntry(Map.Entry)方法即可,如下所示: java 代码 import java.ut ...