HDU 1116 Play on Words(欧拉回路+并查集)
传送门:
http://acm.hdu.edu.cn/showproblem.php?pid=1116
Play on Words
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9939 Accepted Submission(s): 3399
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.
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<iostream>
#include<stdio.h>
#include<memory.h>
using namespace std;
#define max_v 30
int in[max_v],out[max_v];
int vis[max_v];
int pa[max_v];
int rk[max_v];
int n;
void make_set(int x)
{
pa[x]=x;
rk[x]=;
}
int find_set(int x)
{
if(x!=pa[x])
pa[x]=find_set(pa[x]);
return pa[x];
}
void union_set(int x,int y)
{
x=find_set(x);
y=find_set(y);
if(x==y)
return ;
if(rk[x]>rk[y])//按秩合并
pa[y]=x;
else
{
pa[x]=y;
if(rk[x]==rk[y])
rk[y]++;
}
}
void init()//初始化
{
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(vis,,sizeof(vis));
for(int i=; i<max_v; i++)//并查集初始化
make_set(i);
}
int main()
{
int t;
scanf("%d",&t);
string str;
while(t--)
{
init();
scanf("%d",&n);
for(int i=; i<n; i++)
{
cin>>str;
int x=str[]-'a';
int y=str[str.length()-]-'a';
in[x]++;//入度
out[y]++;//出度
vis[x]=;//出现过的标记
vis[y]=;
union_set(x,y);//合并
}
int root=;
for(int i=; i<max_v; i++)
{
if(vis[i]==&&pa[i]==i)//判断连通性
{
root++;
if(root>=)
break;
}
}
if(root>=)//不连通
{
printf("The door cannot be opened.\n");
continue;
}
int s1=,s2=,s3=;
for(int i=; i<max_v; i++)
{
if(vis[i]&&in[i]!=out[i])
{
if(in[i]==out[i]-)//链头
s1++;
else if(in[i]==out[i]+)//链尾
s2++;
else//不是链
s3++;
}
}
if(s3)
printf("The door cannot be opened.\n");
else if((s1==&&s2==)||(s1==&&s2==))//链或者环
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 1116 Play on Words 欧拉路径+并查集
Play on Words Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- 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 欧拉回路+并查集
http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...
- HDU 1116 Play on Words(并查集和欧拉回路)(有向图的欧拉回路)
Play on Words Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU 1116 || POJ 1386 || ZOJ 2016 Play on Words (欧拉回路+并查集)
题目链接 题意 : 有很多门,每个门上有很多磁盘,每个盘上一个单词,必须重新排列磁盘使得每个单词的第一个字母与前一个单词的最后一个字母相同.给你一组单词问能不能排成上述形式. 思路 :把每个单词看成有 ...
- hdu 3018 Ant Trip 欧拉回路+并查集
Ant Trip Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- HDU1878 欧拉回路---(并查集+图论性质)
http://acm.hdu.edu.cn/showproblem.php?pid=1878 欧拉回路 Time Limit: 2000/1000 MS (Java/Others) Memory ...
随机推荐
- git 远程代码回退
git reflog git reset --hard commitId git push -f
- Java中的继承:父类和子类的关系
一.父类引用指向子类对象时 1.若子类覆盖了某方法,则父类引用调用子类重新定义的新方法 2.若子类未覆盖某方法,则父类引用调用父类本身的旧方法 3.若子类覆盖了某属性,但父类引用仍调用父类本身的旧属性 ...
- PDO drivers no value in Windows 或 ndefined class constant 'MYSQL_ATTR_USE_BUFFERED_QUERY'
把办公室的drupal7.54版本放到自己的笔记本(OS:Windows10 Pro,php:7.0.9,mysql 5.7.11,apache:2.4)上运行不了,查看了各项配置应该没问题啊.之前还 ...
- asp.net修改上传文件大小
我们大家都知道ASP.NET为我们提供了文件上传服务器控件FileUpload,默认情况下可上传的最大文件为4M,如果要改变可上传文件大小限制,那么我们可以在web.config中的httpRunti ...
- CentOS 7 Apache 多端口部署 Web Apps 指南
转载自简书,原作者xuyan0,链接https://www.jianshu.com/p/b34c78bf9bf0,如有侵权,请联系删除 导语 Apache web 服务器运行着互联网上超过半数的活跃的 ...
- (Stanford CS224d) Deep Learning and NLP课程笔记(一):Deep NLP
Stanford大学在2015年开设了一门Deep Learning for Natural Language Processing的课程,广受好评.并在2016年春季再次开课.我将开始这门课程的学习 ...
- *.vue文件的template标签内使用form标签
由于form表单有重复提交的问题,所以在vue文件内直接使用form标签时需要注意这个问题,否则会导致页面重复刷新跳转不成功的问题 解决方案: <form @submit.prevent> ...
- linux_day3
1.grep与find的区别? grep是查找文件内的字符而find则是查找文件 2.显示/etc/passwd中以nologin结尾的行 3. 输入ip addr命令后,过滤出包含ip的行 ip a ...
- spring boot(4)-html和templates
静态页面 spring boot项目只有src目录,没有webapp目录,会将静态访问(html/图片等)映射到其自动配置的静态目录,如下 /static /public /resources ...
- maven(15),快照与发布,RELEASE与SNAPSHOT
发布RELEASE 用户A将代码打包发布到RELEASE仓库,具体操作参考上篇文章.用户B使用时,需要在pom.xml添加JAR包的依赖坐标.如果用户A将版本从1.0升级为2.0,用户B使用时也 ...