题目链接:

http://poj.org/problem?id=1386

Play on Words
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9685   Accepted: 3344

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

[Submit]   [Go Back]   [Status]  
[Discuss]

题目意思:

给n个单词。假设A单词的最后一个字母和B单词的第一个字母是一样的。则说明A和B可以连在一起。

问全部单词是否连成一串。

解题思路:

推断连通性+欧拉通路

仅仅考虑单词的首字母和末尾字母。把出现的字母当成节点,一个单词的首字母和尾字母连成一条边。

先用并查集推断是否连通。然后推断是否有欧拉通路。

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std; #define Maxn 30 bool vis[Maxn];
int dei[Maxn],deo[Maxn],fa[Maxn],n;
char sa[1100]; int Find(int x)
{
int temp=x;
while(fa[x]!=x)
x=fa[x];
while(fa[temp]!=x)
{
int cur=fa[temp];
fa[temp]=x;
temp=cur;
}
return x;
}
void Unio(int x,int y)
{
x=Find(x),y=Find(y);
if(x!=y)
fa[x]=y;
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t; scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(vis,false,sizeof(vis));
memset(dei,0,sizeof(dei));
memset(deo,0,sizeof(deo)); for(int i=1;i<=26;i++)
fa[i]=i; while(n--)
{
scanf("%s",sa+1);
int len=strlen(sa+1);
deo[sa[1]-'a'+1]++;
dei[sa[len]-'a'+1]++;
vis[sa[1]-'a'+1]=true;
vis[sa[len]-'a'+1]=true;
Unio(sa[1]-'a'+1,sa[len]-'a'+1);
} int la=-1,nui=0,nuo=0;
bool ans=true; for(int i=1;i<=26;i++)
{
if(vis[i])
{
if(la==-1)
la=Find(i);
else if(la!=Find(i))
{
//printf("->i:%d la:%d\n",i,la);
//system("pause");
ans=false;
break;
}
if(dei[i]-deo[i]==1)
nui++;
else if(deo[i]-dei[i]==1)
nuo++;
else if(deo[i]!=dei[i])
{
ans=false;
break;
}
//printf("i:%d la:%d\n",i,la);
//system("pause"); }
}
if(!ans||nui>=2||nuo>=2||(nui+nuo)==1)
printf("The door cannot be opened.\n");
else
printf("Ordering is possible.\n"); }
return 0;
}

[欧拉回路] poj 1386 Play on Words的更多相关文章

  1. POJ 1386 Play on Words(欧拉图的判断)

    Play on Words Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11838   Accepted: 4048 De ...

  2. poj 1386 Play on Words 有向欧拉回路

    题目链接:http://poj.org/problem?id=1386 Some of the secret doors contain a very interesting word puzzle. ...

  3. poj 1386 Play on Words门上的单词【欧拉回路&&并查集】

    题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...

  4. poj 1386 Play on Words(有向图欧拉回路)

    /* 题意:单词拼接,前一个单词的末尾字母和后一个单词的开头字母相同 思路:将一个单词的开头和末尾单词分别做两个点并建一条有向边!然后判断是否存在欧拉回路或者欧拉路 再次强调有向图欧拉路或欧拉回路的判 ...

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

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

  6. POJ 1386 判断欧拉回路

    题意:要开启一扇门,n个单词是密码,n个单词中,如果一个单词的首字母和前一个单词的尾字母相同,并且每个单词都能这么连起来且只用一次,则门可以开启,否则不能开启,现给出单词,判断门是否可以开. 有向图欧 ...

  7. POJ 1386 Play on Words(单词建图+欧拉通(回)路路判断)

    题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...

  8. poj 1386 Play on Words(有向图欧拉路+并查集)

    题目链接:http://poj.org/problem?id=1386 思路分析:该问题要求判断单词是否能连接成一条直线,转换为图论问题:将单词的首字母和尾字母看做一个点,每个单词描述了一条从首字母指 ...

  9. POJ 1386 Play on Words(欧拉路)

    http://poj.org/problem?id=1386 题意: 给出多个单词,只有单词首字母与上一个单子的末尾字母相同时可以连接,判断所有字母是否可以全部连接在一起. 思路: 判断是否存在欧拉道 ...

随机推荐

  1. magnify.m —— 图像局部放大镜工具函数

    magnify.m 函数下载地址:magnify - File Exchange - MATLAB Central: magnify.m 函数在执行时,是一种交互式处理. 简单演示如下: clear, ...

  2. centos7 阿里云yum源更换

    个人比较喜欢阿里云yum源,同时使用centos7 首先 cd /etc/yum.repos.d/ wget -O /etc/yum.repos.d/CentOS-Base.repo http://m ...

  3. 如何安装MySQL?(二)

    MYSQL的两种安装方式 MSI安装 ZIP安装 第一步: 第二步: 第三步: 这里我选择下载到桌面吧! 第四步: 第五步: 第六步: 第七步: 典型安装:除了安装MySQL的服务器,还安装MySQL ...

  4. ios发布以后关键信息确认与nslog

    发布以后信息查看的路径: xcode->window->devices and …->查看如图的log. 通常在发布以后,处于安全和性能的考虑,会禁止打印log:但是在关键的信息需要 ...

  5. 洛谷3833 [SHOI2012]魔法树

    SHOI2012 D2T3 题目描述 Harry Potter 新学了一种魔法:可以让改变树上的果子个数.满心欢喜的他找到了一个巨大的果树,来试验他的新法术. 这棵果树共有N个节点,其中节点0是根节点 ...

  6. [CEOI2007]树的匹配Treasury(树形DP+高精)

    题意 给一棵树,你可以匹配有边相连的两个点,问你这棵树的最大匹配时多少,并且计算出有多少种最大匹配. N≤1000,其中40%的数据答案不超过 108 题解 显然的树形DP+高精. 这题是作为考试题考 ...

  7. 使用PXE+NFS EFI引导安装RHEL6/7以及Kickstart安装

    PXE引导的步骤: 1.开机后选择网络启动,client端向server端的dhcpd发起获取IP地址的dhcp请求. 2.server端分配IP后,dhcpd会同时根据其配置文件,通过TFTP协议发 ...

  8. 监控web服务(http,本地 / 远程监控nginx)

    监控 httpd 服务一: #!/bin/bash #描述: 秒级别监控 http 服务 while [ 1 -lt 2 ] do sleep 10 ai=`netstat -ntl | grep & ...

  9. TCP 三次握手,四次挥手

    TCP 三次握手,四次挥手 1. TCP 三次握手 建立连接前,客户端和服务端需要通过握手来确认对方: 客户端发送 syn(同步序列编号) 请求,进入 syn_send 状态,等待确认 服务端接收并确 ...

  10. 【Codeforces Round #462 (Div. 1) B】A Determined Cleanup

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 设\(设f(x)=a_d*x^{d}+a_{d-1}*x^{d-1}+...+a_1*x+a_0\) 用它去除x+k 用多项式除法除 ...