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.

题意

给你n个字符串,判断是否能接成一串(上一串的尾==下一串的头)

题解

判断有向图是否成欧拉路径需要2个条件

1.当前图连通(这里可以用并查集,可以用dfs)

2.最多只能有2个点入度In!=出度Out,而且必须是其中1个点出度=入度+1(起点),另1个点入度=出度+1才行(终点)

代码

 #include<bits/stdc++.h>
using namespace std;
int F[];
int Find(int x)
{
return F[x]==x?x:F[x]=Find(F[x]);
}
void Join(int x,int y)
{
if(F[x]==-)F[x]=x;
if(F[y]==-)F[y]=y;
int fx=Find(x);
int fy=Find(y);
if(fx!=fy)
F[fx]=fy;
}
int main()
{
int n,T;
char s[];
scanf("%d",&T);
while(T--)
{
int In[]={},Out[]={};
memset(F,-,sizeof(F));
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%s",s);
int l=strlen(s);
int a=s[]-'a',b=s[l-]-'a';
In[a]++;Out[b]++;
Join(a,b);
}
int flag=,flag1=,flag2=,flag3=;
for(int i=;i<;i++)//图连通
if(F[i]!=-&&F[i]==i)
if(flag3==)flag3=;
else flag=;
for(int i=;i<;i++)
{
int h=In[i],t=Out[i];
if(h==t)//入度=出度
continue;
else if(h-t==)//入度=出度+1
if(flag1==)flag=;
else flag1=;
else if(t-h==)//出度=入度+1
if(flag2==)flag=;
else flag2=;
else
flag=;
}
if(flag)printf("Ordering is possible.\n");
else printf("The door cannot be opened.\n");
}
return ;
}

UVa 10129 Play on Words(有向图欧拉路径)的更多相关文章

  1. Play on Words UVA - 10129 欧拉路径

    关于欧拉回路和欧拉路径 定义:欧拉回路:每条边恰好只走一次,并能回到出发点的路径欧拉路径:经过每一条边一次,但是不要求回到起始点 ①首先看欧拉回路存在性的判定: 一.无向图每个顶点的度数都是偶数,则存 ...

  2. UVa 10129 (并查集 + 欧拉路径) Play on Words

    题意: 有n个由小写字母的单词,要求判断是否存在某种排列使得相邻的两个单词,前一个单词末字母与后一个单词首字母相同. 分析: 将单词的两个字母看做节点,则一个单词可以看做一条有向边.那么题中所求的排列 ...

  3. UVa 10129 Play on Words(并查集+欧拉路径)

    题目链接: https://cn.vjudge.net/problem/UVA-10129 Some of the secret doors contain a very interesting wo ...

  4. Uva 10129 单词

    题目链接:https://uva.onlinejudge.org/external/101/10129.pdf 把单词的首字母和最后一个字母看做节点,一个单词就是一个有向边.有向图的欧拉定理,就是除了 ...

  5. Uva 10129 - Play on Words 单词接龙 欧拉道路应用

    跟Uva 10054很像,不过这题的单词是不能反向的,所以是有向图,判断欧拉道路. 关于欧拉道路(from Titanium大神): 判断有向图是否有欧拉路 1.判断有向图的基图(即有向图转化为无向图 ...

  6. POJ 1386 Play on Words (有向图欧拉路径判定)

    Play on Words Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8768   Accepted: 3065 Des ...

  7. POJ 2337 Catenyms (有向图欧拉路径,求字典序最小的解)

    Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8756   Accepted: 2306 Descript ...

  8. UVa 10129单词(欧拉回路)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. uva 10129 play on words——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABNUAAANeCAYAAAA1BjiHAAAgAElEQVR4nOydabWsuhaFywIasIAHJK

随机推荐

  1. 机器学习进阶-案例实战-停车场车位识别-keras预测是否停车站有车

    import numpy import os from keras import applications from keras.preprocessing.image import ImageDat ...

  2. C# 调用 C++ 的 DLL 返回值为 bool 时,值混乱

    现象:C++ 导出函数的返回值为 false,C# 调用该函数获取的返回值却为 true . 原因:C++ 导出函数返回 false 时,采取的方式是: 将 C# 定义的用来接收返回值的 bool 所 ...

  3. Hive安装 和管理

  4. ABAP-2-会计凭证批量数据导入本地ACCESS

    ABAP-1-会计凭证批量数据导入本地ACCESS 上一版本出现问题: A.若TXT数据条目超过800万(大概1.3G),则将TXT导入ACCESS过程不成功,ACCESS数据表为空.(Access单 ...

  5. sqoop连接SqlServer2012示例

    sqoop import --connect 'jdbc:sqlserver://192.168.xx.xx:1433;username=sa;password=xxxx;database=WindE ...

  6. 实现一个简单的shell

    使用已学习的各种C函数实现一个简单的交互式Shell,要求:1.给出提示符,让用户输入一行命令,识别程序名和参数并调用适当的exec函数执行程序,待执行完成后再次给出提示符.2.该程序可识别和处理以下 ...

  7. Linux命令:ssh-add

    ssh-add帮助 SSH-ADD() BSD General Commands Manual SSH-ADD() NAME ssh-add — adds private key identities ...

  8. linux 自定义模块来缓存skb的意义

    linux中,管理网卡收发报文的结构是sk_buff,这个结构比freebsd中的m_buf复杂的多,这个也是为什么现在用户态协议栈大多采用bsd为基础来实现的一个原因. struct sk_buff ...

  9. eclipse打断点,进行弹窗提示后点击是才进入debug视图,这个要怎么恢复

    window --> preferences --> Run/Debug --> Perspectives 里的 open the associated perspective wh ...

  10. 自定义标签在IE6-8的困境

    或许未来前端组件化之路都是自定义标签,但这东西早在20年前,JSTL已在搞了.现在Web Component还只有webkit支持.但一个组件库,还需要一个特殊的标识它们是一块的.不过这个XML已经帮 ...