用深搜超时。
后来用欧拉回路。
union set 原来是并查集 ,是种数据结构,我看英文知道有这个union set 刚开始还以为是种算法。

union set code:

int height[MAX];
int root[MAX];for(int i=0;i<26;i++){root[i]=i;height[i]=1;}int Find(int a){if(root[a]!=a){root[a]=Find(root[a]);}return root[a];}void un(int a ,int b){int x,y;x=Find(a);y=Find(b);if(x!=y){if(height[x]<height[y]){root[y]=x;height[x]++;}else{root[x]=y;height[y]++;}}
}

AC 代码,用高度进行了修正,比较x,y两个根。把数量多的根,附在数量少的根上。就是说哪个根的数量比较少,哪个就是最后的根。
加上这个前是1230ms ,加上后957ms。

AC代码

#include <iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<queue>
using namespace std;const int MAX=30;
int n;int root[MAX];
int visited[MAX];
int in[MAX];
int out[MAX];
int p[MAX];int height[MAX];
int Find(int a){if(root[a]!=a){root[a]=Find(root[a]);}return root[a];}void un(int a ,int b){int x,y;x=Find(a);y=Find(b);if(x!=y){if(height[x]<height[y]){root[y]=x;height[x]++;}else{root[x]=y;height[y]++;}}}int main()
{int t;scanf("%d",&t);while(t--){scanf("%d",&n);memset(visited,false,sizeof(visited));memset(in,0,sizeof(in));memset(out,0,sizeof(out));// init rootfor(int i=0;i<26;i++){root[i]=i;height[i]=1;}string tmp;int a,b;for(int i=0;i<n;i++){cin>>tmp;int len=tmp.length();a=tmp[0]-'a';b=tmp[len-1]-'a';//un(a,b);visited[a]=true;visited[b]=true;out[a]++;in[b]++;}//for(int i=0;i<26;i++){root[i]=Find(i);}int cnt=0;for(int i=0;i<26;i++){if(visited[i]&&root[i]==i)cnt++;}if(cnt>1){printf("The door cannot be opened.\n");continue;}int j=0;for(int i=0;i<26;i++){if(visited[i]&&out[i]!=in[i]){p[j++]=i;}}if(j==0){printf("Ordering is possible.\n");continue;}if(j==2&&((out[p[0]]-in[p[0]]==1&&in[p[1]]-out[p[1]]==1)||(out[p[1]]-in[p[1]]==1&&in[p[0]]-out[p[0]]==1))){printf("Ordering is possible.\n");continue;}printf("The door cannot be opened.\n");}return 0;
}

hdu 1116 Play on Words相关推荐

  1. hdu 1116 Play on Words

    我的思路: 用俩个数组分别记录出现过的节点的入度和出度,这样只有一个字母的入度比出度大1,一个字母的出度比入度大1,或者是所有有字母的出度和入度都一样,他就是欧拉路 但这样还差了一个联通性的判断 于是 ...

  2. hdu 1116 Play on Words(欧拉通路)

    题意:给出给出n个单词,要求判断这些单词能否接龙,规则像成语接龙一样,就是当前单词的尾字母为下一个单词的首字母. 思路:看着像是欧拉通路相关的题目,但是重点就在于怎么抽象出来. 刚开始想了半天没想明白 ...

  3. hdu 1116 欧拉回路 并查集 一组字符串能否首尾相连成一个字符串

    主要是欧拉回路的基础知识,用并查集加工处理 注意欧拉回路和并查集的细节判断 不能粘贴复制,一定要理解之后再敲一遍代码,否则浪费更多的时间 #include <stdio.h> #inclu ...

  4. hdu 1116 欧拉路

    一个有向图存在欧拉路: 在有向图中,如果图是弱连通的,并且图中除开两个顶点,其他所有顶点的入度等于出度,并且这两个点中,一个点入度比出度多1,另一个点出度比入度少1,那么该图存在欧拉路,这是个充要条件 ...

  5. 【HDU - 1116】【POJ - 1386】Play on Words(判断半欧拉图,欧拉通路)

    题干: Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has ...

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

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

  7. 杭电OJ分类题目(4)-Graph

    原题出处:HDOJ Problem Index by Type,http://acm.hdu.edu.cn/typeclass.php 杭电OJ分类题目(4) HDU Graph Theory - U ...

  8. 【转载】图论 500题——主要为hdu/poj/zoj

    转自--http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  9. [转] HDU 题目分类

    转载来自:http://www.cppblog.com/acronix/archive/2010/09/24/127536.aspx 分类一: 基础题:1000.1001.1004.1005.1008 ...

最新文章

  1. Java设计模式:工厂模式
  2. linux中sh+$0,浅谈linux中shell变量$#,$@,$0,$1,$2的含义解释
  3. 你的元器件为什么会无缘无故地失效了?
  4. 98. Validate Binary Search Tree
  5. SAP License:启动物料帐后不可更改物料价格的处理方法
  6. [ Coding七十二绝技 ] 如何利用Java异常快速分析源码
  7. CTF__(1)web之Cookie
  8. 供应链库存管理-供应链库存案例分析
  9. 问题:为什么我们计算的GDP增长率跟统计局公布的不一样
  10. excel 插入计算机用户名,excel中获取计算机登陆的用户名
  11. java 0点时间_java 获取当前时间与零点的时间差
  12. 高性能、分布式、低延迟的发布订阅中间件对比 Redis 和 emitter
  13. iphonex适配游戏_iPhoneX王者荣耀适配问题 iPhoneX游戏问题详解
  14. 论文解读:Generated Knowledge Prompting for Commonsense Reasoning
  15. 自动柜员机是不是微型计算机,微型计算机基础知识.pptx
  16. Linux、mysql密码初始化
  17. 把pdf转换成ppt的软件
  18. ElasticSearch入门:使用ES来实现模糊查询功能
  19. MySQL专题四——MySQL函数(聚合函数)
  20. java加密算法之对称加密算法AES

热门文章

  1. 基于SSH网上体育用品销售系统
  2. 网站在线监控工具Statping
  3. 【爬虫】scrapy下载股票列表(四)——对接mongodb保存数据
  4. 英语口语六级模拟测试软件,2016年11月英语六级考试口语试题模拟
  5. 使用nsis制作的安装包安装后,无法卸载干净的问题
  6. python快递费用计算_python调用快递鸟api实现查快递
  7. STM32 JTAG SWD (PB3 PB4用作普通IO)及启动模式
  8. 距离度量 —— 切比雪夫距离(Chebyshev Distance)
  9. 简易记账开发笔记之GridLayout
  10. win10 - 鼠标(左键,右键)操作卡, 挂掉,死机问题的解决思路