题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1116

思路:将字符串的头元素和尾元素视为图的x,y节点,然后合并x,y。

如果这个图不连通,则门不能打开,如果路径是欧拉回路或者欧拉通路,则门可以打开。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int a[],b[],vis[],in[],out[];
char str[];
int f(int x)
{
if(x==a[x]) return a[x];
else
{
a[x]=f(a[x]);
return a[x];
}
}
void Merge(int x,int y)
{
int t1=f(x),t2=f(y);
if(t1!=t2)
{
a[t2]=t1;
}
}
int main(void)
{
int num,i,j,x,y,t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=;i<;i++)
{
a[i]=i;vis[i]=;
b[i]=;in[i]=;out[i]=;
}
for(j=;j<n;j++)
{
scanf("%s",str);
x=str[]-'a';
y=str[strlen(str)-]-'a';
Merge(x,y);
in[y]++;out[x]++;
vis[x]=;vis[y]=;
}
for(i=;i<;i++) a[i]=f(i);
num=;
for(i=;i<;i++)
if(vis[i]&&a[i]==i) num++;
if(num>)
{
printf("The door cannot be opened.\n");
}
else
{
num=;
for(i=;i<;i++)
if(vis[i]&&in[i]!=out[i]) b[num++]=i;
if(num==)
{
printf("Ordering is possible.\n");
}
else if(num==&&(out[b[]]-in[b[]]==&&in[b[]]-out[b[]]==||
out[b[]]-in[b[]]==&&in[b[]]-out[b[]]==))
printf("Ordering is possible.\n");
else printf("The door cannot be opened.\n");
}
}
return ;
}

hdu-1116(欧拉回路+并查集)的更多相关文章

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

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

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

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

  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 || POJ 1386 || ZOJ 2016 Play on Words (欧拉回路+并查集)

    题目链接 题意 : 有很多门,每个门上有很多磁盘,每个盘上一个单词,必须重新排列磁盘使得每个单词的第一个字母与前一个单词的最后一个字母相同.给你一组单词问能不能排成上述形式. 思路 :把每个单词看成有 ...

  5. hdu 3018 Ant Trip 欧拉回路+并查集

    Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem ...

  6. HDU1878 欧拉回路---(并查集+图论性质)

    http://acm.hdu.edu.cn/showproblem.php?pid=1878 欧拉回路 Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  7. ACM: FZU 2112 Tickets - 欧拉回路 - 并查集

     FZU 2112 Tickets Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u P ...

  8. HDU 2818 (矢量并查集)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2818 题目大意:每次指定一块砖头,移动砖头所在堆到另一堆.查询指定砖头下面有几块砖头. 解题思路: ...

  9. POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)

    Colored Sticks DescriptionYou are given a bunch of wooden sticks. Each endpoint of each stick is col ...

  10. nyist 42 一笔画 (欧拉回路 + 并查集)

    nyoj42 分析: 若图G中存在这样一条路径,使得它恰通过G中每条边一次,则称该路径为欧拉路径. 若该路径是一个圈,则称为欧拉(Euler)回路. 具有欧拉回路的图称为欧拉图(简称E图).具有欧拉路 ...

随机推荐

  1. UI5-文档-4.17-Fragment Callbacks

    现在我们已经集成了对话框,是时候添加一些用户交互了.用户肯定希望在某个时候再次关闭对话框,因此我们添加一个按钮来关闭对话框并分配一个事件处理程序. Preview The dialog now has ...

  2. UI5-文档-2-开发环境

    这一部分将指导您安装.配置和设置SAPUI5开发环境的最常见和推荐用例. 请注意:您可以在不同的平台上使用SAPUI5.各自平台的许可和维护条件也适用于SAPUI5.例如,如果在SAP云平台上使用SA ...

  3. vue基础——计算属性和侦听器

    计算属性——介绍 模板内的表达式非常便利,但是设计他们的初衷是用于简单计算的.在模板中放入太多的逻辑会让模板太过沉重切难以维护.如下: <div id="example"&g ...

  4. WP8.1 在默认浏览器中打开url

    Windows.System.Launcher.LaunchUriAsync(new Uri("http://www.google.com")); PS: This is for ...

  5. 认识serviceLoader

    最近在研究系统设计方面的东西,发现有很多通用的解决方案,包括spring配置扩展以及serviceLoader的应用,这里简单记录下serviceLoader的简单应用,网上例子很多,大同小异,本人觉 ...

  6. python contextmananger装饰器与with

    如果想自定义一个类或者函数使用with语句,除了在类中自己定义__enter__()方法和__exit__()方法外,还可以使用contextmananger装饰器. contextmananger装 ...

  7. Technology, globalisation and the squeeze on good jobs

    Technology, globalisation and the squeeze on good jobs技术与全球化冲击好工作“Highest stock market EVER! Jobs ar ...

  8. javascript 函数对象

    http://hi.baidu.com/gdancer/blog/item/a59e2c12479b4e54f919b814.html jQuery的一些写法就是基于这篇文章的原理的..     函数 ...

  9. Spring WebMVC 4.1返回json时 406(Not Acceptable)

    1.问题现象Tomcat7+Spring4.1.4,返回json字符串时发生406错误 The resource identified by this request is only capable ...

  10. springboot 面向切面

    @Aspect @Configuration public class AspectTest { @Pointcut("execution(public String xxx.xxx.xxx ...