http://acm.hdu.edu.cn/showproblem.php?pid=1116

给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思。但是如果有多个重复的单词时,也必须满足这样的条件才能算YES。否则都是不可能的情况。

欧拉回路和欧拉通路的判定可以总结为如下:

1)所有的点联通

2)欧拉回路中所有点的入度和出度一样。

3)欧拉通路中起点的入度 - 出度 = 1,终点的 初度 - 入度 = 1, 其他的所有点入度 = 出度;

所以用并查集搞就好了

#pragma comment(linker, "/STACK:36777216")
#pragma GCC optimize ("O2")
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define clr0(x) memset(x,0,sizeof(x))
typedef long long LL;
#define maxn 30
int fa[maxn],vis[maxn]; int f(int x){return x == fa[x] ? x:(fa[x]=f(fa[x]));}
void merge(int x,int y)
{
x = f(x),y = f(y);
if(x != y)
fa[x] = y;
}
int out[maxn],in[maxn],ans[maxn],i,j;
char s[1000];
void solve()
{
int n;RD(n);
clr0(out),clr0(in),clr0(vis);
for(i = 0;i < 26;++i)
fa[i] = i;
while(n--){
scanf("%s",s);
int x = s[0] - 'a',y = s[strlen(s) - 1] - 'a';
merge(x,y);
out[x]++,in[y]++;
vis[x] = vis[y] = 1;
}
int cnt = 0;
for(i = 0;i < 26;++i)
if(vis[i] && f(i) == i){
cnt++;
if(cnt > 1){ //图不连通
puts("The door cannot be opened.");
return ;
}
}
for(i = 0;i < 26;++i)
if(vis[i] && out[i] != in[i]){ //统计入度和出度不相等的点的信息
ans[cnt++] = i;
}
if(cnt == 1 || cnt == 3 && (out[ans[1]] - in[ans[1]] == in[ans[2]] - out[ans[2]]) && abs(in[ans[2]] - out[ans[2]]) == 1)
puts("Ordering is possible.");
else
puts("The door cannot be opened.");
return;
}
int main()
{
int _;RD(_);while(_--)
solve();
return 0;
}

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

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

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

  3. HDU 1116 || POJ 1386 || ZOJ 2016 Play on Words (欧拉回路+并查集)

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

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

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

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

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

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

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

  7. HDU 2818 (矢量并查集)

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

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

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

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

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

随机推荐

  1. "\\s+"的使用

    详解 "\\s+" 正则表达式中\s匹配任何空白字符,包括空格.制表符.换页符等等, 等价于[ \f\n\r\t\v] \f -> 匹配一个换页 \n -> 匹配一个换 ...

  2. 微擎系统 微信支付 get_brand_wcpay_request:fail

    支付授权目录问题,有一个是域名加app的

  3. avg(xxxxxx)什么时候能独自出现?

    avg(xxxxxx)是作为求一组数据的平均数,需要有这组数据的总数和个数,所以通常配合着group by来使用, 比如: SELECT ID, AVG(GRADE) AS 平均数 FROM TEST ...

  4. 用Git发布版本笔记

    1.首先,如果是发布的Develop分支,先从master建立HotFix分支,提交到git并指定关联关系 (git branch --set-upstream-to=D..) 2.对H分支进行功能完 ...

  5. python OSError: [Errno 22] Invalid argument: 'D:\\crawle\x01.html1'

    import urllib.request file = urllib.request.open("http://www.baidu.com") data = file.read( ...

  6. visual code golang配置

    前言 其实环境搭建没什么难的,但是遇到一些问题,主要是有些网站资源访问不了(如:golang.org), 导致一些包无法安装,最终会导致环境搭建失败,跟据这个教程几步,我们将可以快速的构建golang ...

  7. SSH(Struts,Spring,Hibernate)和SSM(SpringMVC,Spring,MyBatis)的区别

    SSH 通常指的是 Struts2 做前端控制器,Spring 管理各层的组件,Hibernate 负责持久化层. SSM 则指的是 SpringMVC 做前端控制器,Spring 管理各层的组件,M ...

  8. Hamburgers

    Hamburgers http://codeforces.com/problemset/problem/371/C time limit per test 1 second memory limit ...

  9. VC++ 定时器使用总结

    VC++    WM_TIMER   定时器使用方法       在编程时,会经常使用到定时器.使用定时器的方法比较简单,通常告诉Windows一个时间间隔,然后WINDOWS以此时间间隔周期性触发程 ...

  10. 【SQL模板】四.插入/更新 列模板TSQL

    ---Name: 插入/更新 列模板.sql ---Purpose: 用于更新 数据库中 列 的脚本模板 ---Author: xx ---Time: 2015-12-18 10:26:06 ---R ...