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的更多相关文章

  1. Play on Words HDU - 1116 (并查集 + 欧拉通路)

    Play on Words HDU - 1116 Some of the secret doors contain a very interesting word puzzle. The team o ...

  2. hdu 1116 欧拉回路+并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...

  3. HDU 1116 Play on Words(欧拉回路+并查集)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1116 Play on Words Time Limit: 10000/5000 MS (Java/Ot ...

  4. HDU 1116 Play on Words(欧拉路径(回路))

    http://acm.hdu.edu.cn/showproblem.php?pid=1116 题意:判断n个单词是否可以相连成一条链或一个环,两个单词可以相连的条件是 前一个单词的最后一个字母和后一个 ...

  5. HDU 1116 Play on Words(并查集和欧拉回路)(有向图的欧拉回路)

    Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  6. 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 ...

  7. HDU 1116 Play on Words(有向欧拉判断)

    题目链接 题意:给出一些单词,问全部单词能否首尾相连 直接 将每一个单词第一个和最后一个字母建立一条有向边,保证除了首尾两个出入度不相等,其他的要保证相等.还有一个条件就是 首尾两个出入度差为1 同时 ...

  8. hdu 1116 并查集和欧拉路径

    ---恢复内容开始--- 把它看成是一个图 只是需要欧拉路径就可以了 首尾能连成一条线即可 如果要判断这个图是否连通 得用并查集 在hrbust oj里面看答案学到的方法 不用各种for循环套着判断能 ...

  9. hdu 1116 Play on Words 欧拉路径+并查集

    Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

随机推荐

  1. Katana 还是Owin ? 本地自承载

    使用Owin 将Web项目脱离 IIS确实很特别..... 由此 ,可以衍生出,一个新的通信渠道,本地Server的自承载. 1 Node.js 2 Python 3 Ruby 4 Owin (C#- ...

  2. 关于volatile

    也许读者会注意到,端口寄存器的变量使用了volatile 修饰符,这是因为C语言在编译的时候会进行某种优化来提高效率,比如下面这段代码int i = 1;int a = i;int b = i;对于编 ...

  3. spring注解方式 idea报could not autowire,eclipse却没有问题

    解决办法1: 从根本上解决: File-Project Structure 页面 Facets下删掉 Spring(直接右键Delete) 这个解答是对的.并不会降低安全性!!因为创建项目的时候,都是 ...

  4. QT类的继承结构

    QT类的继承结构 QT的类 core 数据集合 QString 几何类 QPoint QSize QRectangle 系统类 QColor QFont QImage QIcon QCursor QB ...

  5. 免费 Bootstrap 管理后台模块下载

    在这文章中我们将分享17+个最好的免费 Bootstrap 管理模板.你可以免费下载这些Twitter bootstrap 框架来开发网站后台. SB Admin 2 SB Admin is a fr ...

  6. php之手机号码查归属地

    免费手机号码归属地API查询接口 一.淘宝网API API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=1585078144 ...

  7. MVC MVC 路由详解

    在项目中我们引用了System.Web.Routing;   Routing的作用: 确定Controller 确定Action 确定其他参数 根据识别出来的数据, 将请求传递给Controller和 ...

  8. pyqt例子搜索文本

    #!/usr/bin/env python #-*- coding:utf-8 -*- import sip sip.setapi('QString', 2) sip.setapi('QVariant ...

  9. 使用代码自定义UIView注意一二三

    文/CoderAO(简书作者)原文链接:http://www.jianshu.com/p/68b383b129f9著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 当一撮样式一样的视图在 ...

  10. ie8 hack

    1.‘\9’: eg:.test { color/*\**/: blue\9 }.header {width:300px;} /* 所有浏览器*/.header {width/*\**/:330px\ ...