Play on Words

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7791    Accepted Submission(s): 2676

Problem Description
Some
of the secret doors contain a very interesting word puzzle. The team of
archaeologists has to solve it to open that doors. Because there is no
other way to open the doors, the puzzle is very important for us.

There
is a large number of magnetic plates on every door. Every plate has one
word written on it. The plates must be arranged into a sequence in such
a way that every word begins with the same letter as the previous word
ends. For example, the word ``acm'' can be followed by the word
``motorola''. Your task is to write a computer program that will read
the list of words and determine whether it is possible to arrange all of
the plates in a sequence (according to the given rule) and consequently
to open the door.

 
Input
The
input consists of T test cases. The number of them (T) is given on the
first line of the input file. Each test case begins with a line
containing a single integer number Nthat indicates the number of plates
(1 <= N <= 100000). Then exactly Nlines follow, each containing a
single word. Each word contains at least two and at most 1000 lowercase
characters, that means only letters 'a' through 'z' will appear in the
word. The same word may appear several times in the list.
 
Output
Your
program has to determine whether it is possible to arrange all the
plates in a sequence such that the first letter of each word is equal to
the last letter of the previous word. All the plates from the list must
be used, each exactly once. The words mentioned several times must be
used that number of times.
If there exists such an ordering of
plates, your program should print the sentence "Ordering is possible.".
Otherwise, output the sentence "The door cannot be opened.".
 
Sample Input
3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok
 
Sample Output
The door cannot be opened.
Ordering is possible.
The door cannot be opened.
 
Source
 题意:给你T组数据,n个字符串,问能否成语接龙,使得全部字符串用完;
思路:欧拉路径+并查集;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define esp 0.00000000001
const int N=1e5+,M=1e6+,inf=1e9;
const ll INF=1e18+;
int fa[],du[],flag[];
int Find(int x)
{
return x==fa[x]?x:fa[x]=Find(fa[x]);
}
void update(int u,int v)
{
int x=Find(u);
int y=Find(v);
if(x!=y)
{
fa[x]=y;
}
}
void init()
{
for(int i=;i<=;i++)
fa[i]=i;
memset(du,,sizeof(du));
memset(flag,,sizeof(flag));
}
char a[N];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
int n,p=-;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%s",a);
int u=a[]-'a'+;
int v=a[strlen(a)-]-'a'+;
update(u,v);
du[u]++;
du[v]--;
flag[u]++;
flag[v]++;
}
int ans1=,ans2=;
for(int i=;i<=;i++)
{
if(!flag[i])
continue;
if(du[i]==)
{
if(p==-)
p=Find(i);
if(Find(i)!=p)
{
ans1=-;
break;
}
}
if(du[i]==-)
ans1++;
if(du[i]==)
ans2++;
if(du[i]<=-||du[i]>=)
{
ans1=-;
break;
}
}
if((ans1==&&ans2==)||(ans1==&&ans2==))
printf("Ordering is possible.\n");
else
printf("The door cannot be opened.\n");
}
return ;
}

hdu 1116 Play on Words 欧拉路径+并查集的更多相关文章

  1. Play on Words HDU - 1116(欧拉路判断 + 并查集)

    题意: 给出几个单词,求能否用所有的单词成语接龙 解析: 把每个单词的首字母和尾字母分别看作两个点u 和 v,输入每个单词后,u的出度++, v的入度++ 最后判断是否能组成欧拉路径 或 欧拉回路,当 ...

  2. hdu 5458 Stability(树链剖分+并查集)

    Stability Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Total ...

  3. [HDU 3712] Fiolki (带边权并查集+启发式合并)

    [HDU 3712] Fiolki (带边权并查集+启发式合并) 题面 化学家吉丽想要配置一种神奇的药水来拯救世界. 吉丽有n种不同的液体物质,和n个药瓶(均从1到n编号).初始时,第i个瓶内装着g[ ...

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

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

  5. hdu 5652 India and China Origins 并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题目大意:n*m的矩阵上,0为平原,1为山.q个询问,第i个询问给定坐标xi,yi,表示i年后这 ...

  6. hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them

    http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...

  7. HDU 5458 Stability (树链剖分+并查集+set)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 给你n个点,m条边,q个操作,操作1是删边,操作2是问u到v之间的割边有多少条. 这题要倒着做才 ...

  8. hdu 4619 Warm up 2(并查集)

    借用题解上的话,就是乱搞题.. 题意理解错了,其实是坐标系画错了,人家个坐标系,我给当矩阵画,真好反了.对于题目描述和数据不符的问题,果断相信数据了(这是有前车之鉴的hdu 4612 Warm up, ...

  9. hdu 5441 Travel 离线带权并查集

    Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...

随机推荐

  1. 关于oracle 11g 64位与 32位的 plsql、及其他32位应用程序共存的问题

    因为 plsql 不支持 64位 oracle 客户端,所以plsql 必须使用 oracle 的 32位 instanclient 包.  解压缩后放一个目录,例如: D:\Oracle\insta ...

  2. loadruner报错:Step download timeout(120 seconds)的一个解决方法

    一个网友问了我一个问题如下: loadruner报错:Error -27728: Step download timeout (120 seconds) 如何解决 语 法检查通过,但是在并发执行一个查 ...

  3. JavaEE基础(二十三)/递归

    1.File类递归练习(统计该文件夹大小) 需求:1,从键盘接收一个文件夹路径,统计该文件夹大小 2.File类递归练习(删除该文件夹) 需求:2,从键盘接收一个文件夹路径,删除该文件夹 3.File ...

  4. easyui datagrid 每条数据后添加操作按钮

    easyui datagrid 每条数据后添加“编辑.查看.删除”按钮 1.给datagrid添加操作字段:字段值 <table class="easyui-datagrid" ...

  5. canvas 动画

    1.随机产生形状,做360度运转,带有一个开始开始按钮一个停止按钮 var canvas=$('.mycanvas'); canvas.attr("width",500);//$( ...

  6. VS 6.00 工程项目文件详解

    *.dsp(DeveloperStudio Project):是VC++的工程配置文件,比如说你的工程包含哪个文件,你的编译选项是什么等等,编译的时候是按照.dsp的配置来的.*.dsw(Develo ...

  7. Linux用户组与用户组进阶命令

    1.用户锁定 : passwd -l user1 2.解除用户锁定:passwd -u user1 3.用户无密码登记:passwd -d user1 4.添加到附属用户组:gpasswd -a us ...

  8. 1. python中的随机函数

         本系列不会对python语法,理论作详细说明:所以不是一个学习教材:详细查考Vamei 大神:通俗易懂:是一个很好(基础-中级-高级)的学习教程.而这里只是我一个学习python的某些专题的 ...

  9. [转]android使用shape stroke描边只保留底部

    在项目中遇到这种情况:由于一些原因,自己需要用LinearLayout的垂直布局做出ListView的那种效果,但是ListView是自带了分割线的,而且顶部底部都是没有分割线的,每个item中间都是 ...

  10. WCF自定义地址路由映射(不用svc文件)

    一般在创建WCF服务时会用Serivce.svc文件访问,地址如:http://localhost/applicationname/Serivce.svc/Name 现在用路由映射成:http://l ...