传送门:

http://acm.hdu.edu.cn/showproblem.php?pid=1116

Play on Words

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

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
 
Recommend
Eddy   |   We have carefully selected several similar problems for you:  1217 1325 1269 1596 1281 
 
题目意思:
给你n个单词,问你这n个单词能不能相连成一条链或者一个环
相连的条件是前面一个单词的最后一个字母等于后面这个单词的第一个字母
分析:对每个单词,单词头和单词尾的字母看成一个结点,然后根据规则,可以相连的就连起来,用并查集查看所有单词结点的连通性,如果连成的图不是一个连通图,那么这些结点肯定不可以构成环或者无分叉的链
如果是连通图,再根据欧拉回路性质(一条链或环性质)判断
对链来说:链头的入度=出度-1,链尾的入度=出度+1,链中间结点的出度=入度
对环来说:每个结点的出度=入读
 
code:
#include<iostream>
#include<stdio.h>
#include<memory.h>
using namespace std;
#define max_v 30
int in[max_v],out[max_v];
int vis[max_v];
int pa[max_v];
int rk[max_v];
int n;
void make_set(int x)
{
pa[x]=x;
rk[x]=;
}
int find_set(int x)
{
if(x!=pa[x])
pa[x]=find_set(pa[x]);
return pa[x];
}
void union_set(int x,int y)
{
x=find_set(x);
y=find_set(y);
if(x==y)
return ;
if(rk[x]>rk[y])//按秩合并
pa[y]=x;
else
{
pa[x]=y;
if(rk[x]==rk[y])
rk[y]++;
}
}
void init()//初始化
{
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(vis,,sizeof(vis));
for(int i=; i<max_v; i++)//并查集初始化
make_set(i);
}
int main()
{
int t;
scanf("%d",&t);
string str;
while(t--)
{
init();
scanf("%d",&n);
for(int i=; i<n; i++)
{
cin>>str;
int x=str[]-'a';
int y=str[str.length()-]-'a';
in[x]++;//入度
out[y]++;//出度
vis[x]=;//出现过的标记
vis[y]=;
union_set(x,y);//合并
}
int root=;
for(int i=; i<max_v; i++)
{
if(vis[i]==&&pa[i]==i)//判断连通性
{
root++;
if(root>=)
break;
}
}
if(root>=)//不连通
{
printf("The door cannot be opened.\n");
continue;
}
int s1=,s2=,s3=;
for(int i=; i<max_v; i++)
{
if(vis[i]&&in[i]!=out[i])
{
if(in[i]==out[i]-)//链头
s1++;
else if(in[i]==out[i]+)//链尾
s2++;
else//不是链
s3++;
}
}
if(s3)
printf("The door cannot be opened.\n");
else if((s1==&&s2==)||(s1==&&s2==))//链或者环
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 1116 Play on Words 欧拉路径+并查集

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

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

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

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

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

  5. hdu 1116 欧拉回路+并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...

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

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

  7. HDU 1116 || POJ 1386 || ZOJ 2016 Play on Words (欧拉回路+并查集)

    题目链接 题意 : 有很多门,每个门上有很多磁盘,每个盘上一个单词,必须重新排列磁盘使得每个单词的第一个字母与前一个单词的最后一个字母相同.给你一组单词问能不能排成上述形式. 思路 :把每个单词看成有 ...

  8. hdu 3018 Ant Trip 欧拉回路+并查集

    Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem ...

  9. HDU1878 欧拉回路---(并查集+图论性质)

    http://acm.hdu.edu.cn/showproblem.php?pid=1878 欧拉回路 Time Limit: 2000/1000 MS (Java/Others)    Memory ...

随机推荐

  1. php格式化保留2位小数

    <td align="center"><?php echo sprintf("%.2f",$v[r][red_bag_money]);?> ...

  2. js中获取css样式的两种方式

    1. 对象.style.样式名  弊端就是只能获取行内样式 2.window.getComputedStyle(对象,null); 最好用第二种方式 <!DOCTYPE html> < ...

  3. Catalan数的通项公式(母函数推导)

    首先 \[h_n=\sum_{i}h_ih_{n-i-1}\] 写出 \(h\) 的母函数 \(H(x)\) 那么 \[H(x)=H^2(x)x+1,H(x)=\frac{1-\sqrt{1-4x}} ...

  4. Windows API 查找窗体,发送Windows消息

    最近项目中需要做Windows消息截获操作,在网上找了一些资料. public class WindowsAPI { /// <summary> /// 回调函数代理 /// </s ...

  5. linux 下crontab相关定时触发的配置规则

    linux 下crontab相关定时触发的配置规则: 1.基本格式 :* * * * * command(分 时 日 月 周 命令)2.取值范围:分钟1-59 每分钟用*或者 */1表示小时1-23( ...

  6. 用AutoHotkey实现Excel从表B提取匹配数据到表A

    说明:为表述方便,待填的表为[表A],资料库的表称为[表B].该工具可以快捷地从[表B]中提取相关数据到[表A],顺序和列可自定义. 使用方法:1.打开[ExcelGetFromB.exe](如要打开 ...

  7. July 31st 2017 Week 31st Monday

    Elegance is the only beauty that never fades. 优雅是唯一不会褪色的美. Even the most beautiful apperace would be ...

  8. ZT pthread_detach

    pthread_detach 创 建一个线程默认的状态是joinable, 如果一个线程结束运行但没有被join,则它的状态类似于进程中的Zombie Process,即还有一部分资源没有被回收(退出 ...

  9. [Codeup 25481] swan

    莫名其妙还找到了另一个铟炔锶烃的OJ : Codeup墓地 25481: swan 时间限制: 1 Sec  内存限制: 128 MB献花: 86  解决: 13[献花][花圈][TK题库] 题目描述 ...

  10. [Assignment] C++2

    作业要求:PAT1025 翻转链表 代码在这里