HDU 1116 Play on Words(欧拉路径(回路))
http://acm.hdu.edu.cn/showproblem.php?pid=1116
题意:判断n个单词是否可以相连成一条链或一个环,两个单词可以相连的条件是 前一个单词的最后一个字母和后一个单词的第一个字母一样。
#include<cstdio>
#include<cstring>
int fa[],in[],out[];
int findset(int x)
{
if(fa[x]==x) return fa[x];
return fa[x]=findset(fa[x]);
}
bool vis[];
char s[];
int main()
{
int T,n;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
getchar();
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(vis,,sizeof(vis));
for(int i=;i<;i++) fa[i]=i;
while(n--){
gets(s);
int x=s[]-'a';
out[x]++;
int y=s[strlen(s)-]-'a';
in[y]++;
fa[y]=findset(x);
vis[x]=vis[y]=;
}
int scc=;
for(int i=;i<;i++){
if(vis[i]&&fa[i]==i)
scc++;
}
if(scc>){//如果不连通
printf("The door cannot be opened.\n");
continue;
}
bool flag=false;
int x=,y=,z=;
for(int i=;i<;i++){
if(vis[i]&&in[i]!=out[i]){
if(in[i]==out[i]+)
x++;
else if(in[i]+==out[i])
y++;
else z++;
}
}
if(z){
printf("The door cannot be opened.\n");
continue;
}
if((x==&&y==)||(x==&&y==)){
printf("Ordering is possible.\n");
continue;
}
else
printf("The door cannot be opened.\n");
}
return ;
}
HDU 1116 Play on Words(欧拉路径(回路))的更多相关文章
- hdu 1116 并查集和欧拉路径
---恢复内容开始--- 把它看成是一个图 只是需要欧拉路径就可以了 首尾能连成一条线即可 如果要判断这个图是否连通 得用并查集 在hrbust oj里面看答案学到的方法 不用各种for循环套着判断能 ...
- hdu 1116 Play on Words 欧拉路径+并查集
Play on Words Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- hdu 1116(并查集+欧拉路径)
Play on Words Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- 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 欧拉通路和欧拉回路 #include <cstdio> #include <cstrin ...
- HDU 1116 Play on Words(并查集和欧拉回路)(有向图的欧拉回路)
Play on Words Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- hdu 1116 欧拉回路+并查集
http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...
- 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 - 1693 Eat the Trees(多回路插头DP)
题目大意:要求你将全部非障碍格子都走一遍,形成回路(能够多回路),问有多少种方法 解题思路: 參考基于连通性状态压缩的动态规划问题 - 陈丹琦 下面为代码 #include<cstdio> ...
随机推荐
- push an existing repository from the command line
git remote add origin https://github.com/gaoconggit/LandMVC.git git push -u origin master
- 怎么用MathType解决Word公式排版很乱的问题
现在办公室起草文件,期刊论文投稿.学校试着编辑都要先在Word中编辑好后再打印出来.在Word中编辑这些文本内容时,如果遇到公式就要使用专门的MathType公式编辑器.而有很多人在用MathType ...
- [置顶]JB开发之制作系统级Application
1.编译工程,生成xx.app 2.制作引导进程xx替换xx.app里面的xx进程 引导进程代码: int main(int argc, char *argv[]) { @autoreleasepoo ...
- Android 调用系统相机拍照保存以及调用系统相册的方法
系统已经有的东西,如果我们没有新的需求的话,直接调用是最直接的.下面讲讲调用系统相机拍照并保存图片和如何调用系统相册的方法. 首先看看调用系统相机的核心方法: Intent camera = new ...
- LeetCode 笔记系列一 Median of Two Sorted Arrays
题目:There are two sorted arrays A and B of size m and n respectively. Find the median of the two sort ...
- CentOS开启telnet连接
开启telnet连接通道 yum安装telnet yum -y install telnet-server* 关闭防火墙 /etc/init.d/iptables stop 编辑配置文件 vim /e ...
- Cookies, Security, and Privacy Client Identification
w HTTP The Definitive Guide Cookies themselves are not believed to be a tremendous security risk, be ...
- Storm-源码分析- Multimethods使用例子
1. storm通过multimethods来区分local和distributed模式 当调用launch-worker的时候, clojure会自动根据defmulti里面定义的fn来判断是调用哪 ...
- 一篇搞定MongoDB
MongoDB最基础的东西,我这边就不多说了,这提供罗兄三篇给大家热身 MongoDB初始 MongoDB逻辑与物理存储结构 MongoDB的基础操作 最后对上述内容和关系型数据做个对比 非关系型数据 ...
- javafx 表格某一列设置未复选框
1.表格要设置成可编辑.2.对列进行如下设置.列定义: @FXMLprivate TableColumn<Hole, Boolean> id_colCheck; 列设置id_colChec ...