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. 缓存框架有使用过哪些?memcache和redis有什么区别?项目中,怎么去选择?

    缓存有:ehcache,memcache和redis等 区别: 1. Redis和Memcache都是将数据存放在内存中,都是内存数据库.不过memcache还可用于缓存其他东西,例如图片.视频等等. ...

  2. NSTimer内存泄漏

    用NSTimer调用 timer = [NSTimer scheduledTimerWithTimeInterval:timerInterval target:self selector:@selec ...

  3. 尚硅谷springboot学习16-slf4j的使用

    如何在系统中使用SLF4j 以后开发的时候,日志记录方法的调用,不应该来直接调用日志的实现类,而是调用日志抽象层里面的方法: 给系统里面导入slf4j的jar和 logback的实现jar impor ...

  4. PHP常量总结

    概念 常量我们可以理解为值不变的量.常量只要被定义了,在程序脚本的其他任何地方都不能改变. 因为常量被定义后不能被修改,所以即使重定义常量的值,常量的值也是第一次定义常量时的值 常量不管在哪里被定义, ...

  5. walle自动部署增量上线

    walle的部署大家都会,全量上线也会,今天突然想用下增量上线,试了好多次都不行,咨询了开发的同事终于明白了,特写个笔记省的忘了 如上图我们网站根目录为/data/ifengsite/htdocs/x ...

  6. 计算机网络协议包头赏析-TCP

    仍然先把TCP报文段的格式放在这里,然后我们看图说话: TCP报文段也分为首部和数据两部分,首部默认情况下一般是20字节长度,但在一些需求情况下,会使用“可选字段”,这时,首部长度会有所增加. 下面, ...

  7. Java泛型类型擦除以及类型擦除带来的问题

    目录 1.Java泛型的实现方法:类型擦除 1-2.通过两个例子证明Java类型的类型擦除 2.类型擦除后保留的原始类型 3.类型擦除引起的问题及解决方法 3-1.先检查,再编译以及编译的对象和引用传 ...

  8. centos6.5下oracle自动备份删除指定天数的文件

    第一步先做一个备份 #!/bin/sh export ORACLE_BASE=/home/oracle/app export ORACLE_HOME=/dbhome_1 export ORACLE_S ...

  9. func 的参数修饰

    1.0 在声明一个 Swift的方法的时候,我们一般不去指定参数前面的修饰符,而是直接声明参数: func incrementor(variable : Int) ->Int { } 这个方法接 ...

  10. swift中UIImageView的创建

    let imageView = UIImageView() let imageView1 = UIImageView(frame: CGRectMake(, , , )) // 创建出来的UIImag ...