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> ...
随机推荐
- HMLocationEvent
HMLocationEvent *locEvent = [[HMLocationEvent alloc] initWithRegion:region1]; region1.notifyOnEntry ...
- Gradle学习系列
http://www.cnblogs.com/davenkin/p/gradle-learning-1.html
- c# word excel 二进制 存入数据库
在Sql Server中存储.读写Word文件,需要将指定表字段添加为Image类型,示例表结构为:1 CREATE TABLE CONTRACTS ( 2 ID VARCHAR (50), 3 CO ...
- galera安装之编译安装xtrabackup 2.2.11
----1.编译安装percona-xtrabackup yum -y install cmake gcc gcc-c++ libaio libaio-devel automake autoconf ...
- word文档排版技巧
简介 市场部经常要出各种分析报告,一写就是洋洋洒洒几十页.文字功底深厚的小王写东西自然不在话下,然而每每困扰他的却是排版的问题,每次都要花大量的时间修改格式.制作目录和页眉页脚.最头疼的是上司看完报告 ...
- ios sqlite的创建数据库,表,插入查看数据
iOS sqlite数据库操作.步骤是: 先加入sqlite开发库libsqlite3.dylib, 新建或打开数据库, 创建数据表, 插入数据, 查询数据并打印 1.新建项目sqliteDemo,添 ...
- nexus的pom配置
<groupId>com .sms</groupId><artifactId>sms </artifactId><packaging>pom ...
- Spring的泛型依赖注入
Spring 4.x 中可以为子类注入子类对应的泛型类型的成员变量的引用,(这样子类和子类对应的泛型类自动建立关系)具体说明: 泛型注入:就是Bean1和Bean2注入了泛型,并且Bean1和Bean ...
- base64图片解析
大家可能注意到了,网页上有些图片的src或css背景图片的url后面跟了一大串字符,比如:data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAEAAAAk ...
- C# 控件,MenuStrip,statusStrip,contextMenuStrip,ImageList, Listview,MonthCalendar、DataGridView,combobox,textbox,DateTimePicker,treeview,picturebox、toolStrip,radioButton,TableLayoutPanel
一.菜单栏 1)MenuStrip 菜单栏 选择工具栏控件:menuStrip C# Menustrip控件的常用属性用法详解 C#WinForm应用程序——添加菜单栏MenuStrip] 1.通过右 ...