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 题意: 给出多个单词,只有单词首字母与上一个单子的末尾字母相同时可以连接,判断所有字母是否可以全部连接在一起. 思路: 判断是否存在欧拉道 ...
随机推荐
- Silverlight中DataPager控件扩展
大家一定遇到这样的情况,想改变一下SL的DataPager的显示信息,比如希望分页控件上显示数据的总数.那么就需要扩展一下DataPager控件即可. 其实扩展DataPager很简单,只要获取到Da ...
- MongoDB(3):小的细节问题
1.文档 {“greeting”:“hello,world”,“foo”: 3} 文档中的键/值对是有序的,下面的文档与上面的文档是完全不同的两个文档. {“foo”: 3 ,“greeting”:“ ...
- 从零开始之ecshop基础篇(17)
目标:基于自定义的mvc框架开发的案例(项目) 项目周期 需求分析 典型的业务逻辑: 电子商务:商城(京东),B2C,C2C(淘宝),团购,秒杀,代购 内容管理:新浪门户类,优酷视频管理, ...
- js 月历 时间函数 月份第一天 星期的判断
返回值为0-6,其中返回值0为星期天:如同,php中的日期函数一样判断.
- 用O(1)的时间复杂度,找到栈和队列中的最小(大)值
最近刷剑指offer,看到两道编程题,考察在O(1)的复杂度内,找出最值. 觉得很有意思,很有借鉴意义,故记录在此. 需要注意的是,这里所说的O(1) 有个前提, 就是已经通过某种容器的存储方式进行初 ...
- mac ulimit
sudo sysctl -w kern.maxfilesperproc=1048576ulimit -n 1048576
- 解决mysql登陆时出现“ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysql/mysql.sock' (2)”
mariadb同样适用 首先检查mysql状态 linux-6yo1:~ # /etc/init.d/mysql status Checking for service MySQL: unused m ...
- Android--监听ListView滚动到最底部
监听ListView滚动到最底部使用 onScrollStateChanged(AbsListView view, int scrollState) 方法,代码大致如下: // 监听listview滚 ...
- wifi链接配置
linux 命令行配置wlan无线网卡 无线网卡配置此页由Linux Wiki日(星期四) 09:28的工作基础上.本文介绍在Linux命令行界面中手动配置无线网卡的方法.目前流行的多数发行版都支持用 ...
- postgresql 连接数
改文件 postgresql.conf 里的 #max_connections=32 为 max_connections=1024 以及另外相应修改 share_buffer 参数. 执行SELECT ...