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. 160926、Java-SpringMVC统一异常处理

    从零开始学 Java - Spring MVC 统一异常处理 看到 Exception 这个单词都心慌 如果有一天你发现好久没有看到Exception这个单词了,那你会不会想念她?我是不会的.她如女孩 ...

  2. Install Sogou IM 2.0 in Ubuntu14.04+/Xfce

    Ubuntu14.04+ 安装搜狗输入法 搜狗输入法是一款非常友好的输入法产品,从Ubuntu14.04开始对Linux支持,不过只是Debian系的,是Ubuntu优麒麟组引入的.优麒麟是针对国人设 ...

  3. [转]jexus的安装

    转自http://www.cnblogs.com/xiaodiejinghong/p/3720921.html 这是一个集成了 mono3.4.0 和 jexus5.6.0 的 jexus+mono ...

  4. linux添加somebody到组

    添加xiluhua到组:test_group usermod -a test_group  xiluhua

  5. count(*)、count(val)和count(1)的解释

    一.关于count的一些谣言: 1.count(*)比count(val)更慢!项目组必须用count(val),不准用count(*),谁用扣谁钱! 2.count(*)用不到索引,count(va ...

  6. Nagios监控远端的mysql

    工作原理: 利用特定的用户定期访问指定的mysql数据库.当不能访问或连不通时则报警. 1.在生产库上安装nagios插件    安装略    备注:编译完显示一定要有mysql支持,不然没有chec ...

  7. Python代码项目目录规范v1.0

    程序目录规范:bin # 存放可执行程序 xxxx.py # 程序主程序(入口文件)config # 存放配置信息 settings.py # 全局配置文件(可能暂时未应用)db # 存放数据文件 c ...

  8. destoon 深度整合discuz x2 UC 之免邮箱二次验证

    destoon中member/my.php,信息发布入口处判断是否已在dx中做了验证,如果已经验证,则不再提示验证,否则其中dt的验证页面. 在home.php.php. group.php. for ...

  9. jquery 当前链接激活传递参数|div的切换显示

    一.链接激活时传递参数 $("a").click(function(){ var obj=$(this).attr("field"); //获取当前field ...

  10. 20145227《Java程序设计》第3次实验报告

    20145227<Java程序设计>第3次实验报告 实验步骤与内容 一.实验内容 XP基础 XP核心实践 相关工具 二.实验过程 (一)敏捷开发与XP 1.XP是以开发符合客户需要的软件为 ...