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
随机推荐
- as2 attachMovie库影片无法获取其影片里面的对象或方法
1.attachMovie的容器有两个,导致出错.举例子.创建了一个空的gameMc,然后容器又new个同个名字的,在这里不知道为什么不会替换,而是叠加 containerGame.createEmp ...
- db连接驱动
1.oracle 驱动jar包-->ojdbc6.jar 驱动类-->oracle.jdbc.driver.OracleDriver 驱动连接--> 第一种:jdbc:oracle: ...
- join和子查询的一点点思考
代码和表设计过程中,为了考虑数据库的范式,通常导致需要join多张表或子查询, 如报表场景, 可此种方式在大数据量的 情况下,效率较低. 如果能做适量的数据冗余,便可以减少join或子查询,效率较高 ...
- Java IO流学习总结六:ByteArrayInputStream、ByteArrayOutputStream
类的继承关系 InputStream |__ ByteArrayInputStream OutputStream |__ ByteArrayOutputStream ByteArrayInputStr ...
- zTree插件 角色、部门、人员分类选择
// 传参数调用 function test(){roleOrOrgSelect(3,function(data){console.log(data);});} /** * * @param type ...
- arcgis_SDE安装步骤
弄了将近一个星期的Oracle和ArcSDE终于让我给弄好了!下面把过程跟大家分享一下: 首先是Oracle10gR2的安装,在Oracle的官方网站上可以下到Oracle10gR2的安装程序,安装过 ...
- hdu3579-Hello Kiki-(扩展欧几里得定理+中国剩余定理)
Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- vue router 懒加载实现
在vue-cli脚手架中router文件夹中有index.js文件,里面的内容是 import Vue from 'vue'import Router from 'vue-router'import ...
- Bootstrap Popover
[Bootstrap Popover] 1.设置一个popover需要添加以下设置: 1)data-toggle="popover" 2)title="Example p ...
- hello world讲解1
1. HelloWorld程序解析---------------------------------------------------------------------------------- ...