poj 1386 Play on Words 有向欧拉回路
题目链接:http://poj.org/problem?id=1386
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.
题意:给一些单词能否以某种规则排成一个序列,这个规则就是每个单词开头的字母和前一个单词最后一个字母要相同,比如motorla可以排在acm的后面或者前面。
解法:有向图的欧拉回路判断。
每个单词只能用一次,我们可以以每个单词的首尾这两个字母作为节点,每个单词作为一条边。例如motorla,m 和 a 作为节点,otorl作为连接m和a节点的一条有向边。这样,就构成了有向图。
我们在判断有向图是否有欧拉回路的时候,首先得判断图是否连通,判断图是否连通的比较好的方法就是用并查集,然后再判断欧拉回路。
有向图的欧拉回路:连通的有向图中,要么每个节点的入度和出度个数都相同,要么有一个节点的入度比出度大1,有一个节点的出度比入度大1,其余的节点入度和出度个数都相同。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
const int maxn=+; struct node
{
int u,v;
}edge[maxn];
int n,father[];
int vis[],ind[],outd[];
char str[+]; int find(int x)
{
if (x==father[x]) return x;
return father[x]=find(father[x]);
} int main()
{
int t;
scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
memset(vis,,sizeof(vis));
memset(ind,,sizeof(ind));
memset(outd,,sizeof(outd));
memset(father,-,sizeof(father));
for (int i= ;i<n ;i++)
{
scanf("%s",str);
int len=strlen(str);
int a=str[]-'a';
int b=str[len-]-'a';
edge[i].u=a ;edge[i].v=b ;
vis[a]=vis[b]=;
ind[b]++ ;outd[a]++ ;
father[a]=a;
father[b]=b;
}
for (int i= ;i<n ;i++)
{
int u=edge[i].u;
int v=edge[i].v;
u=find(u) ;v=find(v) ;
if (u!=v) father[u]=v;
}
int flag=;
int k;
for (k= ;k< ;k++) if (vis[k]) {k=find(k);break; }
for (int i= ;i< ;i++) if (vis[i])
{
int u=find(i);
if (u!=k) {flag=;break; }
}
if (flag) {printf("The door cannot be opened.\n");continue; }
int uu=-,vv=-;
for (int i= ;i< ;i++) if (vis[i])
{
if (outd[i]-ind[i]==)
{
if (uu==-) uu=i;
else flag=;
}
else if (ind[i]-outd[i]==)
{
if (vv==-) vv=i;
else flag=;
}
else if (ind[i]-outd[i]> || outd[i]-ind[i]>) {flag=;break; }
}
if (flag) {printf("The door cannot be opened.\n");continue; }
for (int i= ;i< ;i++) if (vis[i] && i!=uu && i!=vv)
{
if (ind[i] != outd[i]) flag=;
}
if (flag) {printf("The door cannot be opened.\n");continue; }
else printf("Ordering is possible.\n");
}
return ;
}
poj 1386 Play on Words 有向欧拉回路的更多相关文章
- POJ 1386 Play on Words(欧拉图的判断)
Play on Words Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11838 Accepted: 4048 De ...
- poj 1386 Play on Words门上的单词【欧拉回路&&并查集】
题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...
- [欧拉回路] poj 1386 Play on Words
题目链接: http://poj.org/problem?id=1386 Play on Words Time Limit: 1000MS Memory Limit: 10000K Total S ...
- poj 1386 Play on Words(有向图欧拉回路)
/* 题意:单词拼接,前一个单词的末尾字母和后一个单词的开头字母相同 思路:将一个单词的开头和末尾单词分别做两个点并建一条有向边!然后判断是否存在欧拉回路或者欧拉路 再次强调有向图欧拉路或欧拉回路的判 ...
- HDU 1116 || POJ 1386 || ZOJ 2016 Play on Words (欧拉回路+并查集)
题目链接 题意 : 有很多门,每个门上有很多磁盘,每个盘上一个单词,必须重新排列磁盘使得每个单词的第一个字母与前一个单词的最后一个字母相同.给你一组单词问能不能排成上述形式. 思路 :把每个单词看成有 ...
- POJ 1386 判断欧拉回路
题意:要开启一扇门,n个单词是密码,n个单词中,如果一个单词的首字母和前一个单词的尾字母相同,并且每个单词都能这么连起来且只用一次,则门可以开启,否则不能开启,现给出单词,判断门是否可以开. 有向图欧 ...
- POJ 1386 Play on Words(单词建图+欧拉通(回)路路判断)
题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...
- poj 1386 Play on Words(有向图欧拉路+并查集)
题目链接:http://poj.org/problem?id=1386 思路分析:该问题要求判断单词是否能连接成一条直线,转换为图论问题:将单词的首字母和尾字母看做一个点,每个单词描述了一条从首字母指 ...
- POJ 1386 Play on Words(欧拉路)
http://poj.org/problem?id=1386 题意: 给出多个单词,只有单词首字母与上一个单子的末尾字母相同时可以连接,判断所有字母是否可以全部连接在一起. 思路: 判断是否存在欧拉道 ...
随机推荐
- Ado.net利用反射执行SQL得到实体
public Model.orderParent GetTraceIDforID(string orderid) { string sql = string.Format(" select ...
- Lua 的函数库 01
这里只介绍和插件编写比较有关的几个函数. 详细的Lua手册请参照Lua Reference Manual 5.1. table函数库 一部分的table函数只对其数组部分产生影响, 而另一部分则对整个 ...
- IOS学习4
---恢复内容开始--- UIScrollView 屏幕展示有限,超出一个屏时用户可滚动查看过多部分.UIView不具备滚动功能. -取消autolayout -设置CGSize contentSiz ...
- java基础笔记
1. 成员变量会自动的进行初始化,但是局部变量不会: 2. equals传引用值,==传地址值:当一个对象是引用类型时,就必须使用equals进行比较. 3. 继承:实现代码的复用,继承关系以一种验证 ...
- openstack的第二天
今天,在公司测试了还是网络有问题. 但是用了rdo还是成功了~明天就再试试怎么开放端口进来.
- Python脚本控制的WebDriver 常用操作 <七>浏览器前进和后退操作
下面将使用WebDriver来控制浏览器的前进和后退操作 测试用例场景 此操作和get.url()方法功能相同 Python脚本 # coding=gbk ''' Created on 2013年12 ...
- python入门总结-函数
函数形式: def functionname(paramlist): function body 局部变量不改变实参的值,如果需要改变,声明global.比如,global x 可以给函数默认值,注意 ...
- oracle 约束
约束是表中列的属性,用来维护数据结构完整性的一种手段约束的种类:NOT NULLUNIQUEPARIAMRY KEYFOREIGN KEYCHECK enble validate 检查现有数据和新数据 ...
- XAML特殊字符
此部分只限制在XAML中,代码中不受此类限制. 1.特殊字符转义 XAML 特殊字符转义 特殊字符 转义 小于号 < < 大于号 > > 取址符 & & 引号 ...
- 用Swift重写公司OC项目(Day2)--创建OC与Swift的桥接文件,进而调用OC类库
昨天把项目中的图标以及启动转场图片弄好了,那么今天,我们可以开始慢慢进入到程序的编写当中了. 由于swift较新,所以类库还不够完善,但是不用担心,苹果早就出了解决方案,那就是使用桥接文件,通过桥接文 ...