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 题意: 给出多个单词,只有单词首字母与上一个单子的末尾字母相同时可以连接,判断所有字母是否可以全部连接在一起. 思路: 判断是否存在欧拉道 ...
随机推荐
- [转]给C++初学者的50个忠告
1.把C++当成一门新的语言学习(和C没啥关系!真的.): 2.看<Thinking In C++>,不要看<C++变成死相>: 3.看<The C++ Prog ...
- 全排列 (codevs 1294)题解
[题目描述] 给出一个n, 请输出n的所有全排列(按字典序输出). [样例输入] 3 [样例输出] 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 [解题思路] 听说C++有作 ...
- Oracle并行事务回滚相关参数及视图
/******相关参数****/fast_start_parallel_rollback1.取值有3种:false,low,high2.各值含义:false ---禁用并行回滚功能 ...
- STM32F0_新建软件工程详细过程
前言 由于ST公司推出比STM32F1性价比更高的F0芯片,现在市面上F0芯片的占有率也非常高.F0芯片属于M0内核,主频48M(当然,可以超频的,但尽量不要超的太多),资源大小可根据项目需求来选型. ...
- IOS笔记 : addChildViewController
一下addChildViewController,一个ViewController可以添加多个子ViewController,但是这 些子ViewController只有一个是显示到父视图中的,可以通 ...
- 用Python作GIS之五:从示例入手—example函数
进入STARS后,最简单的学习方法就是演示示例数据.对于源码的分析也可以从这里入手. 以下为出发菜单项“Example Project”的函数example:def example(se ...
- DB2行转列(多维度)
多维度下进行行列转换,下面的行列转换时根据客户,所属银行机构进行的行列转换. -----------------建表 CREATE TABLE CUST_BANK_INFO ( CUST_ID ), ...
- java 命令对象简单学习实现:
命令模式:首先我们要知道命令模式的基本定义:来自客户端的请求传入一个对象,从而使你可用不同的请求对客户进行参数化.用于“行为请求者”与“行为实现者”解耦,可实现二者之间的松耦合,以便适应变化.分离变化 ...
- iOS开发多线程篇—单例模式(ARC)
iOS开发多线程篇—单例模式(ARC) 一.简单说明: 设计模式:多年软件开发,总结出来的一套经验.方法和工具 java中有23种设计模式,在ios中最常用的是单例模式和代理模式. 二.单例模式说明 ...
- extjs panel自动滚动
指定两个参数 height:600, autoScroll:true, http://bbs.csdn.net/topics/280012147