poj 1386
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 11312 | Accepted: 3862 |
Description
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
Output
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.
Source
这一题是典型的欧拉道路题目。 欧拉道路的定义是: 除了起点和终点外, 其他点的“进出” 次数应该相等。 换句话说,除了起点和终点外, 其他点的度数应该是偶数。
对于有向图, 则必须其中一个点的出度恰好比入度大1, 另一个的入度比出度大。
如果奇点数不存在的话, 则可以从任意点出发,最终一定会回到该点(成为欧拉回路)。
题目给的单词量比较大,但是有用的只有首和尾的字母,所以只需要存首尾字母就可以了。
欧拉道路还有关键的一部是判断这一个图是连通的, 并且只有一个一个连通分支。
代码:
#include<cstdio>
#include<cstring>
using namespace std;
#define N 27
struct node{
int in,out;
}degree[N];
int fa[N],rank[N],mem[N],vis[N],top,t,n;
char str[];
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
int main(){
scanf("%d",&t);
while(t--){
for(int i=;i<=;i++){
fa[i]=i,rank[i]=;
}
memset(degree,,sizeof degree);
memset(vis,,sizeof vis);
top=;
scanf("%d",&n);
for(int i=;i<=n;i++){
memset(str,,sizeof str);
scanf("%s",str);
int a=str[]-'a'+,b=str[strlen(str)-]-'a'+;
if(!vis[a]){
vis[a]=;
mem[++top]=a;
}
if(!vis[b]){
vis[b]=;
mem[++top]=b;
}
degree[a].out++;degree[b].in++;
a=find(a);b=find(b);
if(a!=b){
if(rank[a]<rank[b]) fa[a]=b;
else{
fa[b]=a;
if(rank[a]==rank[b]) rank[a]++;
}
}
}
int tmp=find(mem[]),flag=;
for(int i=;i<=top;i++){
if(find(mem[i])!=tmp){
flag=;break;
}
}
if(flag){
printf("The door cannot be opened.\n");
continue;
}
int sum=,flag1=,flag2=,ok=;
for(int i=;i<=top&&sum<=&&ok;i++){
if(degree[mem[i]].in!=degree[mem[i]].out){
sum++;
if(degree[mem[i]].in==degree[mem[i]].out+) flag1++;
else if(degree[mem[i]].in==degree[mem[i]].out-) flag2++;
else ok=;
}
}
if(ok){
if(flag1==&&flag2== || flag1==&&flag2==)
printf("Ordering is possible.\n");
else
printf("The door cannot be opened.\n");
}
else
printf("The door cannot be opened.\n");
}
return ;
}
poj 1386的更多相关文章
- 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 Some of the secret doors contain a very interesting word puzzle. ...
- poj 1386 Play on Words(有向图欧拉路+并查集)
题目链接:http://poj.org/problem?id=1386 思路分析:该问题要求判断单词是否能连接成一条直线,转换为图论问题:将单词的首字母和尾字母看做一个点,每个单词描述了一条从首字母指 ...
- 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 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...
- [POJ 1386] Play on Words
[题目链接] http://poj.org/problem?id=1386 [算法] 将每个单词的首字母向尾字母连一条有向边,判断欧拉路径是否存在,即可 [代码] #include <algor ...
- [欧拉回路] poj 1386 Play on Words
题目链接: http://poj.org/problem?id=1386 Play on Words Time Limit: 1000MS Memory Limit: 10000K Total S ...
- POJ 1386&&HDU 1116 Play on Words(我以后再也不用cin啦!!!)
Play on Words Some of the secret doors contain a very interesting word puzzle. The team of archaeolo ...
- poj 1386 Play on Words(有向图欧拉回路)
/* 题意:单词拼接,前一个单词的末尾字母和后一个单词的开头字母相同 思路:将一个单词的开头和末尾单词分别做两个点并建一条有向边!然后判断是否存在欧拉回路或者欧拉路 再次强调有向图欧拉路或欧拉回路的判 ...
随机推荐
- STM32出现HardFault故障的解决方法
https://wenku.baidu.com/view/a4a7499afad6195f312ba6d2.html https://wenku.baidu.com/view/085b6fbe5022 ...
- vue $http请求服务
vue中的$http服务 需要引入一个叫vue-resource.js的文件,因为vue.js中没有$http服务.如果需要使用这个服务去百度下载vue-resource.js 然后引进项目即可. ...
- unity, 查看资源文件类型
- vsftpd被动模式配置
1.vsftp配置 参考文章http://linux008.blog.51cto.com/2837805/6105992.设置vsftpd.conf开启被动模式参数 #vim pasv ...
- Redis(十九):Redis压力测试工具benchmark
redis-benchmark使用参数介绍 Redis 自带了一个叫 redis-benchmark 的工具来模拟 N 个客户端同时发出 M 个请求. (类似于 Apache ab 程序).你可以使用 ...
- jQuery事件:bind、delegate、on的区别
最近在AngularJS的开发中,遇到一个神奇的事情:我们用到livebox来预览评论列表中的图片, 然而评论列表是由Angular Resource动态载入的.不可思议的是,点击这些动态载入的图片仍 ...
- iOS代码技巧之判断设备及状态
转自:http://my.oschina.net/joanfen/blog/149076 一.判断设备 01 //设备名称 02 return [UIDevice currentDevice].nam ...
- Building and running Node.js for Android
转自: http://www.goland.org/nodejsonandroid/ Building and running Node.js for Android October 14, 2014 ...
- Spring学习11-Spring管理各种数据源
Spring 完全可以不依赖容器,自己管理数据源,但是却依赖第三方的开源的数据源管理框架. Spring在第三方依赖包中包含了两个数据源的实现类包,其一是Apache的DBCP,其二是 C3P0 ...
- js基本知识6
1.2 复习 1. 节点 网页是有很多的节点组成的 . 元素节点 指的是 : 标签 li span 文本节点 属性节点 父子兄弟 父 parentNode nextSibling 孩子 childNo ...