Uva10129 - Play on Words 欧拉通路 DFS
题目链接:
题目类型: 欧拉道路
题目:
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.
题目大意翻译:
有一些秘密的门包含着非常有趣的单词迷题, 考古学家队伍必须解决它们才能够打开大门。 因为没有其他方法能偶打开这些门, 所以解决那些迷题对我们非常重要。
在每个门上有很多个有磁力的盘子,盘子上面写着单词。 必须重新移动放置这些盘子,让它们形成一个队列:队列中,除了第一个单词,每个单词的开头和上一个单词的结尾字母
一样。例如, motorola的后面可以接上acm。
你的任务是写一个程序, 读入一系列单词,然后计算确定它们是否有可能被排成这样的队列。
样例输入:
- 3
- 2
- acm
- ibm
- 3
- acm
- malform
- mouse
- 2
- ok
- ok
样例输出:
- The door cannot be opened.
- Ordering is possible.
- The door cannot be opened.
解题思路:
把字母看作结点,单词看成有向边,则当且仅当图中有欧拉通路时问题有解。
欧拉通路的两个条件:①基图(忽略边的方向后得到的无向图)联通
②最多只能有两个点的入度不等于出度,而且必须是其中一个点的出度恰好比入度大1,另一个入度比出度大1
AC代码如下
- #include <iostream>
- #include <cstring>
- #define maxn 100
- using namespace std;
- int kase;
- int n;
- int G[maxn][maxn];
- int vis[maxn];
- int in[maxn],out[maxn];
- void dfs(int u)
- {
- vis[u] = true;
- for(int i = ; i < maxn; ++i)
- if(G[u][i] && !vis[i])
- dfs(i);
- }
- int main()
- {
- cin >> kase;
- while(kase--)
- {
- memset(G,,sizeof(G));
- memset(vis,,sizeof(vis));
- memset(in,,sizeof(in));
- memset(out,,sizeof(out));
- cin >> n;
- char str[];
- for(int i = ; i < n; ++i)
- {
- cin >> str;
- int a = str[] - 'a';
- int b = str[strlen(str)-]-'a';
- //注意这里转化为无向图
- ++G[a][b];
- ++G[b][a];
- ++out[a];
- ++in[b];
- }
- //若存在欧拉通路,最多只能有两个点的入度不等于出度
- //而且必须是其中一个点的出度恰好比入度大1
- //另一个的入度比出度大1
- bool flag = true;
- int num1 = ,num2 = ;
- for(int i = ;i < maxn; ++i)
- {
- if(in[i] != out[i])
- {
- if(in[i] == out[i] + ) num1++;
- else if(out[i] == in[i] + ) num2++;
- else{ flag = false; break; }
- }
- }
- if( num1 && num2 && num1 + num2 > ) flag = false;
- if(flag)
- {
- for(int i = ; i < maxn; ++i) //转化成无向图,dfs判断是否联通
- if(out[i]) { dfs(i); break; }
- bool flag2 = true;
- for(int i = ; i < maxn; ++i)
- if((in[i] || out[i]) && !vis[i])
- {
- flag2 = false;
- break;
- }
- if(flag2)
- cout << "Ordering is possible." <<endl;
- else
- cout << "The door cannot be opened." << endl;
- }
- else
- cout << "The door cannot be opened." << endl;
- }
- return ;
- }
Uva10129 - Play on Words 欧拉通路 DFS的更多相关文章
- Poj 2337 Catenyms(有向图DFS求欧拉通路)
题意: 给定n个单词, 问是否存在一条欧拉通路(如acm,matal,lack), 如果存在, 输出字典序最小的一条. 分析: 这题可以看作http://www.cnblogs.com/Jadon97 ...
- UVa 12118 检查员的难题 (dfs判连通, 构造欧拉通路)
题意: 分析: 欧拉通路:图连通:图中只有0个或2个度为奇数的结点 这题我们只需要判断选择的边构成多少个联通块, 再记录全部联通块一共有多少个奇度顶点. 然后我们在联通块中连线, 每次连接两个联通块就 ...
- CodeForces - 508D Tanya and Password(欧拉通路)
Description While dad was at work, a little girl Tanya decided to play with dad characters. She has ...
- 欧拉图 欧拉回路 欧拉通路 Euler
欧拉图 本文链接:http://www.cnblogs.com/Ash-ly/p/5397702.html 定义: 欧拉回路:图G的一个回路,如果恰通过图G的每一条边,则该回路称为欧拉回路,具有欧拉回 ...
- 欧拉图 欧拉回路 欧拉通路 Euler的认识 (转)
转:https://www.cnblogs.com/Ash-ly/p/5397702.html 定义: 欧拉回路:图G的一个回路,如果恰通过图G的每一条边,则该回路称为欧拉回路,具有欧拉回路的图称为欧 ...
- nyoj 42 一笔画 欧拉通路
http://acm.nyist.net/JudgeOnline/problem.php?pid=42 一笔画问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 zyc ...
- Inspector's Dilemma(欧拉通路)
In a country, there are a number of cities. Each pair of city is connected by a highway, bi-directio ...
- hdu3472 混合图判断欧拉通路
对于欧拉回路,先判断出度入度的差是否为偶数,然后最大流一次. 此题是判断有无欧拉通路,前提要判断图是否连通,然后欧拉通路的条件:要么出入度差没有奇数,或者只有2个点. 所以先统计差为奇数的个数,如果不 ...
- FZU 2112 并查集、欧拉通路
原题:http://acm.fzu.edu.cn/problem.php?pid=2112 首先是,票上没有提到的点是不需要去的. 然后我们先考虑这个图有几个连通分量,我们可以用一个并查集来维护,假设 ...
随机推荐
- 有关opacity或RGBA设置颜色值及元素的透明值
opacity声明来设置元素的透明值,当opacity设置元素的透明值,内部的文字及元素也会透明,通过RGBA设置的颜色值只针对当前元素,内部的文字及元素的透明值并未发生变化 opacity声明来 ...
- 为Python添加中文关键字
狗屎咖啡 2 个月前 原址: https://zhuanlan.zhihu.com/p/31159526 swizl/cnpython 1. 大部分语法,可以按下面方法加同义的中文token第1步. ...
- Android studio Error:org.gradle.api.internal.tasks.DefaultTaskInputs$TaskInputUnionFileCollection cannot be cast to
http://blog.csdn.net/FlyRabbit_1/article/details/74536317 Error:org.gradle.api.internal.tasks.Defaul ...
- sql sever[基本] ''增删改'' 随笔
结构语言分类 DDL(数据定义语言) create drop alter 创建删除以及修改数据库,表,存储过程,触发器,索引.... DML(数据操作语言) insert delete ...
- Lucene.net(4.8.0) 学习问题记录二: 分词器Analyzer中的TokenStream和AttributeSource
前言:目前自己在做使用Lucene.net和PanGu分词实现全文检索的工作,不过自己是把别人做好的项目进行迁移.因为项目整体要迁移到ASP.NET Core 2.0版本,而Lucene使用的版本是3 ...
- 阿里maven仓库地址 和 oschina maven仓库地址
<mirror> <id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> < ...
- Cent OS 6.5下源码安装php7.2
1.安装php需要的扩展 # yum install libxml2 libxml2-devel openssl openssl-devel libcurl libcurl-devel libjpeg ...
- [原创]Nginx反向代理及负载均衡
1.基本命令 # 启动nginx start nginx.exe # windowsnginx -c /usr/local/nginx/conf/nginx.conf # Linux# 优雅的停止ng ...
- SP3精密星历简介
IGS精密星历采用sp3格式,其存储方式为ASCII文本文件,内容包括表头信息以及文件体,文件体中每隔15 min给出1个卫星的位置,有时还给出卫星的速度.它的特点就是提供卫星精确的轨道位置.采样率为 ...
- 开启 TLS 1.3 加密协议,极速 HTTPS 体验
随着互联网的发展,用户对网络速度的要求也越来越高,尤其是目前在大力发展 HTTPS 的情况下,TLS 加密协议变得至关重要.又拍云在 HTTPS 的普及和性能优化上,始终做着自己的努力和贡献.2018 ...