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

题意:判断n个单词是否可以相连成一条链或一个环,两个单词可以相连的条件是 前一个单词的最后一个字母和后一个单词的第一个字母一样。

分析前提:有(无)向图的欧拉路径判断均是基于连通图
欧拉路径判断:
1、一个无向图存在欧拉路径的充要条件:头节点和尾节点度数为奇数 ;中间节点度数为偶数。
2、一个有向图存在欧拉路径的充要条件:头 入度==出度-1 ;中间 入度==出度 ; 尾 入度==出度+1。
欧拉回路判断:
1、一个无向图存在欧拉回路的充要条件:当且仅当该图所有顶点度数都是偶数。
2、一个有向图存在欧拉回路的充要条件:所有顶点的入度等于出度。

#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(欧拉路径(回路))的更多相关文章

  1. hdu 1116 并查集和欧拉路径

    ---恢复内容开始--- 把它看成是一个图 只是需要欧拉路径就可以了 首尾能连成一条线即可 如果要判断这个图是否连通 得用并查集 在hrbust oj里面看答案学到的方法 不用各种for循环套着判断能 ...

  2. hdu 1116 Play on Words 欧拉路径+并查集

    Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  3. hdu 1116(并查集+欧拉路径)

    Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  4. Play on Words HDU - 1116 (并查集 + 欧拉通路)

    Play on Words HDU - 1116 Some of the secret doors contain a very interesting word puzzle. The team o ...

  5. hdu 1116 Play on Words

    http://acm.hdu.edu.cn/showproblem.php?pid=1116 欧拉通路和欧拉回路 #include <cstdio> #include <cstrin ...

  6. HDU 1116 Play on Words(并查集和欧拉回路)(有向图的欧拉回路)

    Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  7. hdu 1116 欧拉回路+并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...

  8. HDU 1116 Play on Words(欧拉回路+并查集)

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

  9. HDU - 1693 Eat the Trees(多回路插头DP)

    题目大意:要求你将全部非障碍格子都走一遍,形成回路(能够多回路),问有多少种方法 解题思路: 參考基于连通性状态压缩的动态规划问题 - 陈丹琦 下面为代码 #include<cstdio> ...

随机推荐

  1. push an existing repository from the command line

    git remote add origin https://github.com/gaoconggit/LandMVC.git git push -u origin master

  2. SQLServer------数据类型在C#中的转换

    SQL C#tinyint bytesmallint Int16char stringint int

  3. shrink-to-fit(自适应宽度)

    自适应宽度是指当未明白设定容器的宽度(或外边距设为auto)时,在特定的情况下容器的宽度会依据情况自行设定.而设定的结果往往并非我们想要的. W3C规范中描写叙述了几种shrink-to-fit的情况 ...

  4. 重写equals()方法也要重写hashcode()方法

    如果我们对equals方法进行了重写,建议一定要对hashCode方法重写,以保证相同的对象返回相同的hash值,不同的对象返回不同的hash值.

  5. oracle order by 字段不能为空 为空速度慢 不走索引

    oracle order by 字段不能为空 为空速度慢 不走索引

  6. eclipse 设置 @author @version等注释模板

    eclipse->window->preference->java->code styple->code template->Code->New Java f ...

  7. c#获取QQ音乐当前播放的歌曲名

    在网上找了很久,没找到方法,自己尝试着做,还是做出来了,很简单,就几句代码. Process[] ps = Process.GetProcessesByName("QQmusic" ...

  8. Arcengine 二次开发得到点shapefile的坐标

    做二次开发的时候,想要得到Point shapfile的坐标和相应的属性,也就是Point 的(x,y)和某个属性,在网上查了一些资料,做总结如下 首先,你要确定自己要操作的图层,一般来说,得到当前操 ...

  9. INFORMATION_SCHEMA.STATISTICS 统计 表 库 大小

    INFORMATION_SCHEMA MySQL :: MySQL 5.5 Reference Manual :: 21 INFORMATION_SCHEMA Tables https://dev.m ...

  10. HBase简介及集群安装

    一.Hbase概述 Apache HBase™是Hadoop数据库,是一个分布式,可扩展的大数据存储. 当您需要对大数据进行随机,实时读/写访问时,请使用Apache HBase™.该项目的目标是托 ...