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> ...
随机推荐
- python XlsxWriter Example: Hello World
http://xlsxwriter.readthedocs.io/example_hello_world.html The simplest possible spreadsheet. This is ...
- 审批流_state_selection 相关用法
审批流 _state_selection 前部分参数代表:新加的状态 后部分参数代表:原有系统的状态
- sdut 2153:Clockwise(第一届山东省省赛原题,计算几何+DP)
Clockwise Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Saya have a long necklace with ...
- hdu 2025:查找最大元素(水题,顺序查找)
查找最大元素 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- sqlalchemy 判断字段是否存在
1.low方法: (1)person_obj=session.query(Person).filter(Person.name=='jack') print (person_obj.count()) ...
- JavaScript 事件循环 — event loop
引言 相信所有学过 JavaScript 都知道它是一门单线程的语言,这也就意味着 JS 无法进行多线程编程,但是 JS 当中却有着无处不在的异步概念 .在初期许多人会把异步理解成类似多线程的编程模式 ...
- AWS系列-创建AMI
AMI创建 在XEN中pv是半虚拟化,hvm是全虚拟化,pv只能用于linux内核的系统,效率更高,hvm可以虚拟所有常见操作系统(可以使用 windows),理论效率比pv略低,另外,hvm需要cp ...
- 剑指 offer set 17 判断一棵树是否是平衡树
总结 1. 书上给出一个简洁代码, 说是会重复遍历. 2. 框架相同, 可以写出下面的代码 class Solution { public: bool ans; int getHeight(TreeN ...
- python裁剪base64编码的图片
简介 今天遇到需要裁剪base64字符串的PNG图片,并返回base64格式字符串的任务,捣鼓半天. 裁剪代码如下: def deal_inspect_img(base64_str): "& ...
- 160520、MyBatis的几种批量操作
MyBatis中批量插入 方法一: <insert id="insertbatch" parameterType="Java.util.List"> ...