题目链接: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. canal 常用配置

    https://github.com/alibaba/canal/wiki/AdminGuide canal.instance.filter.regex 监控哪些表的正则配置 如下: mysql 数据 ...

  2. SpringMVC post和get乱码解决方式

    如何解决POST请求中文乱码问题,GET的又如何处理呢? 在web.xml中加入: <filter>     <filter-name>CharacterEncodingFil ...

  3. scala -- 柯里化

    柯里化 柯里化是把接受多个参数的函数,变成接受一个单一参数的函数.并且返回接受剩余参数和返回结果的新函数. 就是一个逐次消元的过程. 当把函数的元全消掉,就得到了值. 值就是零元函数. 二元函数 f( ...

  4. Installation failed with message INSTALL_CANCELED_BY_USER.

    Installation failed with message INSTALL_CANCELED_BY_USER. It is possible that this issue is resolve ...

  5. 清理数据库errorlog

    C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\LOGERRORLOGERRORLOG.1ERRORLOG.2ERRORLOG.3ERRORLO ...

  6. mogodb查询

    Find: {$and: [ {"MethodName":"CommLogin"} ,{"CreateTime":{$gte:"2 ...

  7. python+webdriver,选取Select下拉框中的值

    在选择下拉框中的值时遇到了困难,用driver.find_element_by_id("").send_keys("")进行赋值不能成功获取下拉框中的值.   ...

  8. 解决selenium不支持firefox低版本的问题

    解决selenium不支持firefox低版本的问题 在火狐浏览器升级后,突然发现webdriver运行脚本的时候不能调出火狐浏览器了,并报错WebDriverException:Message:'C ...

  9. cdoj841-休生伤杜景死惊开 (逆序数变形)【线段树 树状数组】

    http://acm.uestc.edu.cn/#/problem/show/841 休生伤杜景死惊开 Time Limit: 3000/1000MS (Java/Others)     Memory ...

  10. acceleration

    acceleration - Bing dictionary US[ək.selə'reɪʃ(ə)n]UK[ək.selə'reɪʃ(ə)n] n.加速度:加快:(车辆)加速能力 网络促进:加速力:加 ...