hdu 1116 欧拉回路+并查集
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 欧拉回路+并查集的更多相关文章
- 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 Play on Words(欧拉回路+并查集)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1116 Play on Words Time Limit: 10000/5000 MS (Java/Ot ...
- HDU 1116 || POJ 1386 || ZOJ 2016 Play on Words (欧拉回路+并查集)
题目链接 题意 : 有很多门,每个门上有很多磁盘,每个盘上一个单词,必须重新排列磁盘使得每个单词的第一个字母与前一个单词的最后一个字母相同.给你一组单词问能不能排成上述形式. 思路 :把每个单词看成有 ...
- hdu 3018 Ant Trip 欧拉回路+并查集
Ant Trip Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- HDU1878 欧拉回路---(并查集+图论性质)
http://acm.hdu.edu.cn/showproblem.php?pid=1878 欧拉回路 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- ACM: FZU 2112 Tickets - 欧拉回路 - 并查集
FZU 2112 Tickets Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u P ...
- HDU 2818 (矢量并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2818 题目大意:每次指定一块砖头,移动砖头所在堆到另一堆.查询指定砖头下面有几块砖头. 解题思路: ...
- POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)
Colored Sticks DescriptionYou are given a bunch of wooden sticks. Each endpoint of each stick is col ...
- nyist 42 一笔画 (欧拉回路 + 并查集)
nyoj42 分析: 若图G中存在这样一条路径,使得它恰通过G中每条边一次,则称该路径为欧拉路径. 若该路径是一个圈,则称为欧拉(Euler)回路. 具有欧拉回路的图称为欧拉图(简称E图).具有欧拉路 ...
随机推荐
- 基元线程同步构造之waithandle中 waitone使用
在使用基元线程同步构造中waithandle中waitone方法的讲解: 调用waithandle的waitone方法阻止当前线程(提前是其状态为Nonsignaled,即红灯),直到当前的 Wait ...
- 微擎系统 微信支付 get_brand_wcpay_request:fail
支付授权目录问题,有一个是域名加app的
- Hadoop 初始化系统
hadoop namenode -format 或者 hdfs namenode -format 2.执行hadoop sbin 目录下的 start-dfs.sh start-yarn.sh3.查看 ...
- HTTP Response Code 中文详解
引自:https://blog.csdn.net/lplj717/article/details/70053560 1xx - 信息提示这些状态代码表示临时的响应.客户端在收到常规响应之前,应 ...
- mysql转型
1.将Int 转为varchar经常用 concat函数,比如concat(8,’0′) 得到字符串 ’80′2.将varchar 转为Int 用 cast(a as signed) a为varcha ...
- php去除bom
<?php /*检测并清除BOM*/ if(isset($_GET['dir'])){ $basedir=$_GET['dir']; }else{ $basedir = '.'; } $auto ...
- goim源码分析与二次开发-comet分析一
因为要完成一个聊天的项目,所以借鉴了goim,第一篇分析打算半原版,先摘抄http://www.jianshu.com/p/8bd96a9a473d他的一些理解,写这些还是为了让自己更好的理解这个项目 ...
- Maven项目标准目录结构
-----------------------siwuxie095 Maven 项目标准目录结构 1.Maven 项目分为两种 (1)Java 项目 (2)Web 项目 2.对于 Java 项目 其中 ...
- jsoncpp在Windows和Linux下的安装
Windows下: 参考这个网站,没什么问题,注意MTd这些选对就行了. http://www.cppblog.com/wanghaiguang/archive/2013/12/26/205020.h ...
- iOS codeview
1.环境配置 oclint:http://oclint.org/ xcpretty:https://github.com/supermarin/xcpretty 使用Mac安装xcpretty过程可能 ...