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 首先是,票上没有提到的点是不需要去的. 然后我们先考虑这个图有几个连通分量,我们可以用一个并查集来维护,假设 ...
随机推荐
- CentOS下 elasticsearch集群安装
1.进入root目录并下载elasticsearch cd /root wget https://download.elastic.co/elasticsearch/elasticsearch/ela ...
- 基于阿里云的JavaEE系统框架介绍
基于阿里云的系统框架展望 1) CDN 用于缓存静态文件等等.七牛和阿里的都还可以. 七牛要做的久一点,各种图片处理的接口要完善一些 阿里的CDN要稍微好一点点,但是没有不安全的访问方式,访问稍微没有 ...
- 前端生成验证码图片utils
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncod ...
- python科学计算_numpy_函数库
1.常规函数与排序 常用统计函数: 求和:sum().均值:mean().标准差:std().方差:var().最小值:min().最大值:max().最大值与最小值之差:ptp().最大值的下标:a ...
- Linux服务管理1-1 课程简介与系统运行级别
- 最新swift4.0 图片进行尺寸大小及体积压缩
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px Menlo; color: #4dbf56; background-color: #282b3 ...
- GUI—ST_emWin移植
GUI-ST_emWin移植 一.st_emwin移植 1.stemwin源文件下载:ST官网 https://my.st.com/content/my_st_com/en/search.html#q ...
- Python 学习笔记大纲
Python Basic 第一章:Python基础の快速认识基本语法 (点击进入博客)我的第一个HelloPython程序.如何实现用户输入.Python的自带电池(模块).变量.格式化.条件判断.循 ...
- 魔方 NewLife.Cube
魔方 是一个基于 ASP.NET MVC 的 用户权限管理平台,可作为各种信息管理系统的基础框架. 演示:http://cube.newlifex.com 源码 演示账号:admin/admin 源码 ...
- input表单的type属性详解,不同type不同属性之间区别
目标:详解表单input标签type属性常用的属性值 一.input标签和它的type属性 PS:input 元素可以用来生成一个供用户输入数据的简单文本框. 在默认的情况下, 什么样的数据均可以输入 ...