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 ...
随机推荐
- Form开发中组件控制的几个常用方法
转自:http://oracleseeker.com/2009/09/01/graphical_component_control_in_oracle_ebs_form/ 在Oracle EBS 的F ...
- Qt Quick 简单教程 - 1 (代码备忘)
qmlscene 未安装 由于出现上面的情况,我开始转战Windows 下学习,昨天安装好了Qt Sdk了,哟吼吼吼. mail.qml内容: import QtQuick 2.3 import Qt ...
- YII中路径别名
路径别名 system:代表framework目录 system.web:代表framework/web目录 zii:代表framework/zii目录 webroot:代表项目下的app目录(htt ...
- php生成验证码图片
0:效果图 1:index.php用来生成验证码图片 <?php session_start(); header ('Content-Type: image/png'); $image=imag ...
- Android线程与异步消息处理机制
在程序开发时,对于一些比较耗时的操作,我们通常会为其开辟一个单独的线程来执行,这样可以尽可能的减少用户等待的时间.在Android中,默认情况下,所有的操作都是在主线程中进行的,这个主线程负责管理与U ...
- MVC中使用jquery的浏览器缓存问题
jquery在浏览器ajax调用的时候,对缓存提供了很好的支持,POST方式不能被缓存,使用POST的原因,明确了数据不能被缓存,或者避免JSON攻击(JSON返回数据的时候可以被入侵) jquery ...
- SetTimer的使用
SetTimer函数用于创建一个计时器,KillTimer函数用于销毁一个计时器.计时器属于系统资源,使用完应及时销毁. SetTimer的函数原型如下:UINT_PTR SetTimer( HWND ...
- Java_类和对象(完美总结)_转载_覆盖和隐藏的区别,覆盖就不能使用了,而隐藏提供全局方法名或者全局变量名还可以使用
转载自海子:http://www.cnblogs.com/dolphin0520/p/3803432.html Java:类与继承 对于面向对象的程序设计语言来说,类毫无疑问是其最重要的基础.抽象.封 ...
- ssh(Struts2+hibernate+spring)简单分页
实体类+实体映射+entity(pagebean)+dao层+service层+action层
- 在js中window.open通过“post”传递参数
在js中window.open通过“post”传递参数的步骤如下: 如:在A.jsp中 有一个js方法 winow.open,目标地址是 xx.do 1.在A.jsp建一个form,把要设置的值通过j ...