题目链接: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 有向欧拉回路的更多相关文章

  1. POJ 1386 Play on Words(欧拉图的判断)

    Play on Words Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11838   Accepted: 4048 De ...

  2. poj 1386 Play on Words门上的单词【欧拉回路&&并查集】

    题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...

  3. [欧拉回路] poj 1386 Play on Words

    题目链接: http://poj.org/problem?id=1386 Play on Words Time Limit: 1000MS   Memory Limit: 10000K Total S ...

  4. poj 1386 Play on Words(有向图欧拉回路)

    /* 题意:单词拼接,前一个单词的末尾字母和后一个单词的开头字母相同 思路:将一个单词的开头和末尾单词分别做两个点并建一条有向边!然后判断是否存在欧拉回路或者欧拉路 再次强调有向图欧拉路或欧拉回路的判 ...

  5. HDU 1116 || POJ 1386 || ZOJ 2016 Play on Words (欧拉回路+并查集)

    题目链接 题意 : 有很多门,每个门上有很多磁盘,每个盘上一个单词,必须重新排列磁盘使得每个单词的第一个字母与前一个单词的最后一个字母相同.给你一组单词问能不能排成上述形式. 思路 :把每个单词看成有 ...

  6. POJ 1386 判断欧拉回路

    题意:要开启一扇门,n个单词是密码,n个单词中,如果一个单词的首字母和前一个单词的尾字母相同,并且每个单词都能这么连起来且只用一次,则门可以开启,否则不能开启,现给出单词,判断门是否可以开. 有向图欧 ...

  7. POJ 1386 Play on Words(单词建图+欧拉通(回)路路判断)

    题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...

  8. poj 1386 Play on Words(有向图欧拉路+并查集)

    题目链接:http://poj.org/problem?id=1386 思路分析:该问题要求判断单词是否能连接成一条直线,转换为图论问题:将单词的首字母和尾字母看做一个点,每个单词描述了一条从首字母指 ...

  9. POJ 1386 Play on Words(欧拉路)

    http://poj.org/problem?id=1386 题意: 给出多个单词,只有单词首字母与上一个单子的末尾字母相同时可以连接,判断所有字母是否可以全部连接在一起. 思路: 判断是否存在欧拉道 ...

随机推荐

  1. C# 随机颜色的方法

    public string GetRandomColor() { Random RandomNum_First = new Random((int)DateTime.Now.Ticks); // 对于 ...

  2. text-rendering 详解

    原文链接:http://www.feeldesignstudio.com/2013/05/text-rendering Text-rendering 属性是一个非标准属性,主要用来告诉渲染引擎(ren ...

  3. js构造函数,索引数组和属性的属性

    本文主要介绍和小结js的构造函数,关联数组的实现方式和使用,及不可变对象和它的实现方式及他们使用过程中要注意的点 <script> function p(){ var len=argume ...

  4. C++实现01串排序

    题目内容:将01串首先按长度排序,长度相同时,按1的个数从少到多进行排序,1的个数相同时再按ASCII码值排序. 输入描述:输入数据中含有一些01串,01串的长度不大于256个字符. 输出描述:重新排 ...

  5. Python学习教程(learning Python)--1.2.4 Python格式化输出科学计数

    Python在浮点数据输出时,可以采用科学计数法的方式输出. 现举两个例子说明一下如何使用. eg1. 无精度要求的科学计数法浮点数据输出 >>> print(format(1234 ...

  6. <! [if IE 神奇的条件注释 ]>

    早上起来无聊,看到某学长发的一张代码截图有条件注释,正好,研究一下. 条件注释: 在IE中用来区分IE版本.是否为IE的代码神器! 在其他的浏览器里是不好使的. 不过也值得了,IE都区分出来了,其他的 ...

  7. prettyprint

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  8. 使用dom4j技术对xml文件的基本操作

    1.pojo类:Notice package com.green.notice.storage; import java.util.ArrayList; import java.util.List; ...

  9. hdu 2094 产生冠军

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2094 产生冠军 Description 有一群人,打乒乓球比赛,两两捉对撕杀,每两个人之间最多打一场比 ...

  10. 自学asp.net mvc(三)

    1.将前台框架的登录页面代码,复制到Login.cshtml. 2.将文本框替换. 3.缓存机制. 4.类图