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 ...
随机推荐
- k:特殊的线性表—队列
队列的概念: 队列是另一种特殊的线性表,它的特殊性体现在其只允许在线性表的一端插入数据元素,在线性表的另一端删除数据元素(一般会采用在线性表的表尾那端(没被head指针所指的那端)插入数据元素,在线 ...
- .NET Core是什么
对于开发人员,把C#语言和.NET描述为最重要的新技术一点都不夸张.NET提供了一种环境.在这种环境中,可以开发在Windows上运行的几乎所有应用程序.如:编写Web页面.WPF应用程序.REST ...
- 实习小结(二)--- SSM框架搭建
SSM项目框架搭建 前几天做了一个学生信息管理的项目,使用纯控制台输入,查询数据库,将信息在控制台中打印,功能完善得差不多之后,老师让将这个项目移植到Web中,使用Spring+SpringMVC+M ...
- WC前的颓废——带花树
QAQ现在很不想写题解博客那就来写个算法吧QAQ... 带花树 题目 来看个题... UOJ79. 某机房里有\(n\)个OIer,其中有\(n\)个男生,\(0\)个女生.现在他们要两两配对. 有\ ...
- js判断是手机还是PC端
有时接触一些手机上的适应,需要知道是pc 还是移动端 function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = [ ...
- BaseActivity合集
1.出自“高仿京东商城”: package com.itau.jingdong.ui.base; import com.itau.jingdong.AppManager; import com.ita ...
- QQ群文件下载速度慢-解决办法
QQ群文件下载速度慢-解决办法 本方法是本人亲测测试出来的,特此和大家分享 没有效果让你打我 解决方法 1.打开[手机版 QQ] 2.进入群文件,找到需要下载文件 3.[分享],先点击[发给好友],选 ...
- Linux下php-fpm进程过多导致内存耗尽问题
这篇文章主要介绍了解决Linux下php-fpm进程过多导致内存耗尽问题,需要的朋友可以参考下 最近,发现个人博客的Linux服务器,数据库服务经常挂掉,导致需要重启,才能正常访问,极其恶心,于是 ...
- C#网络编程(一)基础篇
简介: C#网络编程API包含在System.Net和System.Net.Sockets命名空间下,大部分网络操作都可以在其中找到相应的类来实现:包括Socket的创建和连接,网络流收发方法的封装, ...
- LNMP-day3-php扩展缓存插件
perl的编译问题 [root@localhost php5.6.33]# echo 'export LC_ALL=C' >> /etc/profile #设置环境变量,解决后面perl ...