hdu 1116
欧拉回路,利用并查集来实现;
代码:
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
int a[],f[],b[];
bool vis[]; int find(int x)
{
return f[x]==-?x:f[x]=find(f[x]);
} void combine(int x,int y)
{
int n=find(x);
int m=find(y);
if(n!=m) f[n]=m;
} bool oula()
{
int st=-;
for(int i=; i<; i++)
{
if(a[i]||b[i])
if(st==-)st=find(i);
else if(st!=find(i)) return false;
}
vector<int>v;
for(int i=; i<; i++)
if(a[i]!=b[i])
v.push_back(a[i]-b[i]);
return v.size()==||v.size()==&&v[]*v[]==-;
} int main()
{
int t,n;
char s[];
scanf("%d",&t);
while(t--)
{
memset(a,,sizeof a);
memset(f,-,sizeof f);
memset(b,,sizeof b);
int ans=,cnt=;
bool flag=;
scanf("%d",&n);
for(int i=; i<n; i++)
{
scanf("%s",&s);
int l=strlen(s);
int x=s[]-'a',y=s[l-]-'a';
a[x]++,b[y]++;
combine(x,y);
}
if(oula()) printf("Ordering is possible.\n");
else puts("The door cannot be opened.");
}
return ;
}
hdu 1116的更多相关文章
- 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 欧拉回路+并查集
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 1116 Play on Words(欧拉路径(回路))
http://acm.hdu.edu.cn/showproblem.php?pid=1116 题意:判断n个单词是否可以相连成一条链或一个环,两个单词可以相连的条件是 前一个单词的最后一个字母和后一个 ...
- HDU 1116 Play on Words(并查集和欧拉回路)(有向图的欧拉回路)
Play on Words Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- POJ 1386&&HDU 1116 Play on Words(我以后再也不用cin啦!!!)
Play on Words Some of the secret doors contain a very interesting word puzzle. The team of archaeolo ...
- HDU 1116 Play on Words(有向欧拉判断)
题目链接 题意:给出一些单词,问全部单词能否首尾相连 直接 将每一个单词第一个和最后一个字母建立一条有向边,保证除了首尾两个出入度不相等,其他的要保证相等.还有一个条件就是 首尾两个出入度差为1 同时 ...
- 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 ...
随机推荐
- JS火狐与IE的差别
function isIE(){ //ie? ) return true; else return false; } if(!isIE()){ //firefox innerText define H ...
- setValue 和 setObject 的区别
在使用NSMutableDictionary 的时候经常会使用setValue forKey 与 setObject forKey,他们经常是可以交互使用的. 1.setValue forKey的定义 ...
- 解析XML文件时做得修改
在.h文件中找到需要在Build Settings中设置的东西,查找Header Search Paths,然后添加/usr/include/libxml2
- 需要设置jdk的三处位置:
需要设置jdk的三处位置:1.tomcat需要一个JDK : Windows--->Preferences--->MyEclipse--->Servers--->Tomcat- ...
- java strtus2 注解配置入门(一)
因为工作的原因,所以接触到一些项目,有的项目虽然看着能有跟着做,可是具体里面的框架是别人配置的,具体框架还是不是非常的了解,所以这里在看一下我学到的 一点关于struts2中注解开发的一点点. 直接代 ...
- win10 Administrator
和Win7/Win8.1一样,Win10的管理员账户Administrator是默认隐藏和关闭的,因为该账户权限极高,被不法分子利用后存在极大风险.但如果你想在某些特殊情况下使用该账户,就需要手动将其 ...
- CSS常见选择器
一.元素选择器 p,html,h1, h2 1.多个元素一起设置同一种风格, 则用逗号“,”隔开(选择器分组) 2.通配符选择, *{Color:red} 表示文档中所有元素都为红色 二.类选择器 ...
- iOS 高级开发 runtime(三)
三 .动态添加方法 我们可以通过runtime动态地添加方法.那么到底啥叫动态添加方法呢?动态添加方法就是当我们程序运行时才知道我们应该调用哪个方法.我们首先需要了解这一点,当我们编写完一段代码后,我 ...
- Source Insight及常用插件
Source Insight及常用插件 1.Source Insight 2.插件 <1>.使用快捷键注释,单行注释,多行注释,#if 0注释 <2>.跳转到当前文件所在的文件 ...
- 使用hwclock同步RTC(硬件时钟)和OS Clock(操作系统时钟)
Linux下面有个命令叫做hwclock,其主要的功能是用来在RTC和操作系统之间进行时钟同步.既可以将RTC的时钟同步到操作系统,也可以在通过hwclock将当前系统时间.Internet时间.本地 ...