POJ1386 Play on Words
| Time Limit: 1000MS | Memory Limit: 10000KB | 64bit IO Format: %I64d & %I64u |
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
欧拉道路问题。这题欧拉回路或者欧拉道路都算成立。
建好图以后先判一下连通性,如果图不连通,肯定不成立。
↑DFS BFS 并查集都可以
然后是判欧拉路径。
如果是欧拉回路,那么所有点的入度等于出度。
如果是欧拉路径,那么只有两个点的入度不等于出度,其一是起始点,出度比入度大1,另一个是结束点,入度比出度大1
之前一直WA,不知怎么改着改着就对了。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int mxn=;
bool vis[mxn];
int n;
int fa[mxn];
int in[mxn],out[mxn];
void init(int n){
for(int i=;i<=n;i++)fa[i]=i;
}
int find(int x){
if(fa[x]==x)return x;
else return fa[x]=find(fa[x]);
}
char s[];
int main(){
int T;
scanf("%d",&T);
while(T--){
memset(vis,,sizeof vis);
memset(in,,sizeof in);
memset(out,,sizeof out);
scanf("%d",&n);
init();
int i,j;
for(i=;i<=n;i++){
scanf("%s",s);
int u=s[]-'a'+;
int v=s[strlen(s)-]-'a'+;
in[v]++;out[u]++;
vis[u]=vis[v]=;
int x=find(u),y=find(v);
if(x!=y)fa[y]=x;
}
int st=,ed=;bool flag=;
int cnt=;
for(i=;i<=;i++){
if(vis[i] && find(i)==i){
cnt++;
}
if(out[i]!=in[i]){//出度不等于入度时判定
if(out[i]==in[i]+){
if(!st)st=i;
else flag=;
}
else if(in[i]==out[i]+){
if(!ed)ed=i;
else flag=;
}
else flag=;
}
}
if(cnt!=){//非连通特判
printf("The door cannot be opened.\n");
continue;
}
if(flag==)printf("The door cannot be opened.\n");
else printf("Ordering is possible.\n");
}
return ;
}
POJ1386 Play on Words的更多相关文章
- 【poj1386】 Play on Words
http://poj.org/problem?id=1386 (题目链接) 题意 给出n个单词,判断它们能否首尾相接的排列在一起. Solution 将每一格单词的首字母向它的尾字母连一条有向边,那么 ...
- poj-1386(欧拉回路)
题意:给你n个单词,每个单词可以和另一个单词连接,前提是(这个单词的尾字母等下一个单词的首字母),问你有没有一种连法能够连接所有的单词: 解题思路:每个单词可以看成是首字母指向尾字母的一条边,那么就变 ...
- poj1386 字符串类型的一笔画问题 欧拉回路
Play on Words Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10685 Accepted: 3625 De ...
- poj1386单词连接(欧拉欧拉欧拉)
///单词连接,欧拉回路通路都可以(有向图) ///主要构图:比如possibilities就构造p->s的边////题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词 ...
- poj1386有向图判断是否存在欧拉回路或者欧拉路
有向图的图联通是指基图联通,也就是把有向图的边改成无向图然后看是否连通.判断联通可用dfs或者并查集. 题意就是给你n个由小写字母构成的字符串,问你能不能将这n个字符串连接起来,B能接在A后面的条 ...
- Play on Words
poj1386:http://poj.org/problem?id=1386 题意:给你n个单词,问你是否能够通过调整单词的顺序存在这样的一个序列,使得 每个单词的首字母是前一个单词的尾字母. 题解: ...
- D2欧拉路,拓扑排序,和差分约束
第一题:太鼓达人:BZOJ3033 题意:给出k,求一个最长的M位01串,使其从每一个位置向后走k个得到 的M个k位01串互不相同(最后一个和第一个相邻,即是一个环).输出 字典序最小的答案. 2 ≤ ...
- ACM学习大纲
1 推荐题库 •http://ace.delos.com/usaco/ 美国的OI 题库,如果是刚入门的新手,可以尝试先把它刷通,能够学到几乎全部的基础算法极其优化,全部的题解及标程还有题目翻译可以b ...
- POJ 1386 Play on Words (有向图欧拉路径判定)
Play on Words Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8768 Accepted: 3065 Des ...
随机推荐
- Mysql 索引 简介
Mysql索引 索引的分类 索引的创建 索引的注意事项 什么是索引 索引是存储引擎用于快速查找记录的一种数据结构. 索引由数据库中一列或者多列组成,作用是提高表的查询速度. 索引的优点,提高检索数据的 ...
- 包围轮廓的矩形边界 opencv
#include<opencv2/opencv.hpp> #include<iostream> using namespace std; using namespace cv; ...
- Python装饰器探究——装饰器参数
Table of Contents 1. 探究装饰器参数 1.1. 编写传参的装饰器 1.2. 理解传参的装饰器 1.3. 传参和不传参的兼容 2. 参考资料 探究装饰器参数 编写传参的装饰器 通常我 ...
- 3,MongoDB之数据类型
一.MongoDB 之数据类型 首先我们要先了解一下MongoDB中有什么样的数据类型: Object ID :Documents 自生成的 _id String: 字符串,必须是utf-8 Boo ...
- 3 网格 landing page
0.大框架 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <tit ...
- Codeforces 787D Legacy 线段树 最短路
题意: 有\(n(1 \leq n \leq 10^5)\)个点,\(q(1 \leq q \leq 10^5)\)条路和起点\(s\) 路有三种类型: 从点\(v\)到点\(u\)需要花费\(w\) ...
- Python数据类型三
一.帮助 如果想知道一个对象(object)更多的信息,那么可以调用help(object)!另外还有一些有用的方法,dir(object)会显示该对象的大部分相关属性名,还有object._ doc ...
- 常用doc 命令
开始-->运行 regedit 进入注册表补充些: 1. gpedit.msc-----组策略 2. sndrec32-------录音机 3. Nslookup-------IP地址侦测器 4 ...
- http客户端缓存
这篇文章写得比较,点击查看
- Mecanim动画
1.基础 现在Animation编辑器给个模型设计一个动画,都会自动为此模型加上Animator组件,并产生一个controller后缀的控制器和一个相关的anim后缀的动画剪辑, unity根据An ...