UVa 10129 Play on Words(有向图欧拉路径)
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(有向图欧拉路径)的更多相关文章
- Play on Words UVA - 10129 欧拉路径
关于欧拉回路和欧拉路径 定义:欧拉回路:每条边恰好只走一次,并能回到出发点的路径欧拉路径:经过每一条边一次,但是不要求回到起始点 ①首先看欧拉回路存在性的判定: 一.无向图每个顶点的度数都是偶数,则存 ...
- UVa 10129 (并查集 + 欧拉路径) Play on Words
题意: 有n个由小写字母的单词,要求判断是否存在某种排列使得相邻的两个单词,前一个单词末字母与后一个单词首字母相同. 分析: 将单词的两个字母看做节点,则一个单词可以看做一条有向边.那么题中所求的排列 ...
- UVa 10129 Play on Words(并查集+欧拉路径)
题目链接: https://cn.vjudge.net/problem/UVA-10129 Some of the secret doors contain a very interesting wo ...
- Uva 10129 单词
题目链接:https://uva.onlinejudge.org/external/101/10129.pdf 把单词的首字母和最后一个字母看做节点,一个单词就是一个有向边.有向图的欧拉定理,就是除了 ...
- Uva 10129 - Play on Words 单词接龙 欧拉道路应用
跟Uva 10054很像,不过这题的单词是不能反向的,所以是有向图,判断欧拉道路. 关于欧拉道路(from Titanium大神): 判断有向图是否有欧拉路 1.判断有向图的基图(即有向图转化为无向图 ...
- POJ 1386 Play on Words (有向图欧拉路径判定)
Play on Words Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8768 Accepted: 3065 Des ...
- POJ 2337 Catenyms (有向图欧拉路径,求字典序最小的解)
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8756 Accepted: 2306 Descript ...
- UVa 10129单词(欧拉回路)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- uva 10129 play on words——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABNUAAANeCAYAAAA1BjiHAAAgAElEQVR4nOydabWsuhaFywIasIAHJK
随机推荐
- JAVA_Class.forName
Class.forName(xxx.xx.xx) 返回的是一个类 ,作用是要求JVM查找并加载指定的类,也就是说JVM会执行该类的静态代码段 --------- 首先,在java里面任何class都要 ...
- python语言中的数据类型之集合
数据类型 集合类型 set 用途:1.关系运算 2.去重 定义方式:在{}内用逗号分隔开多个元素,但元素的特点是 1.集合内元素必须是不可变类型 2.集合内元素无序 集合内元素不能 ...
- Linux:客户端的实现
写了一个简单的服务器软件,但是没有写客户端.现在我将客户端实现了,其实昨天已经说了客户端的实现步骤了. 步骤: socket() 初始化 connet()链接 从标准输入读数据fgets() 传数据到 ...
- Linux下使用命令行配置IPMI
ipmitool是什么: 百度百科给的解释已经够用了,简单说就是“IPMI(Intelligent Platform Management Interface)即智能平台管理接口是使硬件管理具备“智能 ...
- mybatis中事务简单使用
一步: 事务只用在service层方法上加 @Transactional(propagation = Propagation.REQUIRED) :发现如果没有它,增加执行-->1/ ...
- C# 递归获取 文件夹的 所有文件
public void Director(string dir, List<string> list) { DirectoryInfo d = new DirectoryInfo(dir) ...
- SPSS-回归分析
回归分析(一元线性回归分析.多元线性回归分析.非线性回归分析.曲线估计.时间序列的曲线估计.含虚拟自变量的回归分析以及逻辑回归分析) 回归分析中,一般首先绘制自变量和因变量间的散点图,然后通过数据在散 ...
- yii配置访问路由权限配置
'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ 'logout' => ['post', 'ge ...
- html -引入其他html页面
其他页面html为:ip.html 主页面代码 <body> <div id="ip"></div> </body> <scr ...
- 消息队列RabbitMQ与Spring
1.RabbitMQ简介 RabbitMQ是流行的开源消息队列系统,用erlang语言开发.RabbitMQ是AMQP(高级消息队列协议)的标准实现. 官网:http://www.rabbitmq.c ...