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 首先是,票上没有提到的点是不需要去的. 然后我们先考虑这个图有几个连通分量,我们可以用一个并查集来维护,假设 ...
随机推荐
- using 40 logical processors based on SQL Server licensing SqlServer CPU核心数限制问题
公司服务器是120核心cpu,但是实际应用中只有40核,原因是业务部门发现服务器cpu承载30%的时候sql 就会卡死: 然后从sqlserver 去查询,cpu核心数: SELECT COUNT(1 ...
- [Spark内核] 第30课:Master的注册机制和状态管理解密
本課主題 Master 接收 Worker, Driver, Application Master 处理 Driver 狀态变换 Master 处理 Executor 狀态变换 [引言部份:你希望读者 ...
- Qt用Zip压缩文件夹的一些坑
环境: QT3.3.8 vs2005 QDir dir("/home/Blinux/html"); if ( !dir.exists() ) { //目录不存在 } QString ...
- 15.5 自学Zabbix之路15.5 Zabbix数据库表结构简单解析-其他 表
点击返回:自学Zabbix之路 自学Zabbix之路15.5 Zabbix数据库表结构简单解析-其他 表 1. Actions表 actions表记录了当触发器触发时,需要采用的动作. 2.Aler ...
- wc--Linux
这个命令的功能也很好记,因为它功能很有限: wc -c filename:显示一个文件的字节数 wc -m filename:显示一个文件的字符数 wc -l filename:显示一个文件的行数 w ...
- Mongodb百亿级数据添加,修改,删除,查询等性能测试【四】
集群的结构,大家可以查看我的另一遍文章,Mongodb的三种集群 在最后一种集群中,介绍到. 目前使用的数据就是最后一个测试集群,留下的数据. 简单介绍一下,四个分片的配置 192.168.99.6 ...
- ES6小点心之通用弹窗
小点心,顾名思义,开箱即食,拿来即用. 前端业务逻辑主要分为[交互效果]和[数据展示]两方面.数据展示可使用 MVVM 框架来实现.前端的交互效果常用的也就那么几种,比如弹窗,楼层定位,倒计时,下拉刷 ...
- 魔方 NewLife.Cube
魔方 是一个基于 ASP.NET MVC 的 用户权限管理平台,可作为各种信息管理系统的基础框架. 演示:http://cube.newlifex.com 源码 演示账号:admin/admin 源码 ...
- LevelDB的源码阅读(三) Put操作
在Linux上leveldb的安装和使用中我们写了这么一段测试代码,内容以及输出结果如下: #include <iostream> #include <string> #inc ...
- 微信小程序之swiper轮播图中的图片自适应高度
小程序中的轮播图很简单,官方都有例子的,但是唯一的缺陷就是swiper是固定死的150px高度,这样如果传入的图片大于这个高度就会被隐藏.辣么,怎样让图片自适应不同分辨率捏. 我的思路是:获取屏幕宽度 ...