hdu 1116 Play on Words
http://acm.hdu.edu.cn/showproblem.php?pid=1116
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 1000
using namespace std; int f[maxn],in[maxn],ou[maxn],a[maxn];
bool vis[maxn];
int n;
char str[maxn]; int find(int x)
{
if(x==f[x]) return x;
return f[x]=find(f[x]);
} void union1(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
f[fx]=fy;
} void inti()
{
for(int i=; i<; i++)
f[i]=i;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
inti();
memset(in,,sizeof(in));
memset(ou,,sizeof(ou));
memset(vis,false,sizeof(vis));
for(int i=; i<n; i++)
{
scanf("%s",str);
int k=strlen(str);
ou[str[]-'a']++;
in[str[k-]-'a']++;
vis[str[]-'a']=true;
vis[str[k-]-'a']=true;
union1(str[]-'a',str[k-]-'a');
}
int ans=;
for(int i=; i<; i++)
{
if(vis[i]&&f[i]==i)
ans++;
}
if(ans>)
{
printf("The door cannot be opened.\n");
continue;
}
int m1=,m2=;
bool flag=true;
for(int j=; j<; j++)
{
if(vis[j]&&in[j]!=ou[j])
{
if(in[j]-ou[j]==)
{
m1++;
}
else if(ou[j]-in[j]==)
{
m2++;
}
else flag=false;
}
}
if(flag&&((m1==&&m2==)||(m1==&&m2==)))
printf("Ordering is possible.\n");
else
printf("The door cannot be opened.\n");
}
return ;
}
hdu 1116 Play on Words的更多相关文章
- Play on Words HDU - 1116 (并查集 + 欧拉通路)
Play on Words HDU - 1116 Some of the secret doors contain a very interesting word puzzle. The team o ...
- hdu 1116 欧拉回路+并查集
http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...
- HDU 1116 Play on Words(欧拉回路+并查集)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1116 Play on Words Time Limit: 10000/5000 MS (Java/Ot ...
- HDU 1116 Play on Words(欧拉路径(回路))
http://acm.hdu.edu.cn/showproblem.php?pid=1116 题意:判断n个单词是否可以相连成一条链或一个环,两个单词可以相连的条件是 前一个单词的最后一个字母和后一个 ...
- HDU 1116 Play on Words(并查集和欧拉回路)(有向图的欧拉回路)
Play on Words Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- POJ 1386&&HDU 1116 Play on Words(我以后再也不用cin啦!!!)
Play on Words Some of the secret doors contain a very interesting word puzzle. The team of archaeolo ...
- HDU 1116 Play on Words(有向欧拉判断)
题目链接 题意:给出一些单词,问全部单词能否首尾相连 直接 将每一个单词第一个和最后一个字母建立一条有向边,保证除了首尾两个出入度不相等,其他的要保证相等.还有一个条件就是 首尾两个出入度差为1 同时 ...
- hdu 1116 并查集和欧拉路径
---恢复内容开始--- 把它看成是一个图 只是需要欧拉路径就可以了 首尾能连成一条线即可 如果要判断这个图是否连通 得用并查集 在hrbust oj里面看答案学到的方法 不用各种for循环套着判断能 ...
- hdu 1116 Play on Words 欧拉路径+并查集
Play on Words Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
随机推荐
- Qt C++中的关键字explicit——防止隐式转换(也就是Java里的装箱),必须写清楚
最近在复习QT,准备做项目了,QT Creator 默认生成的代码 explicit Dialog(QWidget *parent = 0)中,有这么一个关键字explicit,用来修饰构造函数.以前 ...
- mysql sql优化<1>
<pre name="code" class="html">explain SELECT t.* FROM ( SELECT t1.sn AS cl ...
- HDU--3487 Play with Chain (Splay伸展树)
Play with Chain Problem Description YaoYao is fond of playing his chains. He has a chain containing ...
- spoj1812-Longest Common Substring II(后缀自动机)
Description A string is finite sequence of characters over a non-empty finite set Σ. In this problem ...
- Odometer使用JavaScript和CSS制作数字滑动效果
Odometer是一个使用JavaScript和CSS技术,制作出数字上下滑动的动画效果插件,有点类似与我们的天然气的读数的动画效果,这个插件是轻量级的,压缩版本只有3kg,使用CSS3动画技术,所以 ...
- matlab遗传算法
function [xv,fv] = myGA(fitness, a, b, NP, NG, Pc, Pm, eps) % 用遗传算法求解一维无约束优化问题 % % 待优化的目标函数 fitness ...
- 《学习opencv》笔记——矩阵和图像操作——cvCalcCovarMatrix,cvCmp and cvCmpS
矩阵和图像的操作 (1)cvCalcCovarMatrix函数 其结构 void cvCalcCovarMatrix(计算给定点的均值和协方差矩阵 const CvArr** vects,//给定向量 ...
- poj 1011 Sticks (DFS+剪枝)
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 127771 Accepted: 29926 Descrip ...
- HDU1285——确定比赛名次
Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委 ...
- with admin option 与with grant option
在赋予user 权限或者role 时,常常会用到with admin option 和with grant option,而在使用中,可能会很容易出现混淆的情况,现把他们的相同点和不同点总结如下:相同 ...