题目链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=1070

题目类型: 欧拉道路

题目:

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的更多相关文章

  1. Poj 2337 Catenyms(有向图DFS求欧拉通路)

    题意: 给定n个单词, 问是否存在一条欧拉通路(如acm,matal,lack), 如果存在, 输出字典序最小的一条. 分析: 这题可以看作http://www.cnblogs.com/Jadon97 ...

  2. UVa 12118 检查员的难题 (dfs判连通, 构造欧拉通路)

    题意: 分析: 欧拉通路:图连通:图中只有0个或2个度为奇数的结点 这题我们只需要判断选择的边构成多少个联通块, 再记录全部联通块一共有多少个奇度顶点. 然后我们在联通块中连线, 每次连接两个联通块就 ...

  3. CodeForces - 508D Tanya and Password(欧拉通路)

    Description While dad was at work, a little girl Tanya decided to play with dad characters. She has ...

  4. 欧拉图 欧拉回路 欧拉通路 Euler

    欧拉图 本文链接:http://www.cnblogs.com/Ash-ly/p/5397702.html 定义: 欧拉回路:图G的一个回路,如果恰通过图G的每一条边,则该回路称为欧拉回路,具有欧拉回 ...

  5. 欧拉图 欧拉回路 欧拉通路 Euler的认识 (转)

    转:https://www.cnblogs.com/Ash-ly/p/5397702.html 定义: 欧拉回路:图G的一个回路,如果恰通过图G的每一条边,则该回路称为欧拉回路,具有欧拉回路的图称为欧 ...

  6. nyoj 42 一笔画 欧拉通路

    http://acm.nyist.net/JudgeOnline/problem.php?pid=42 一笔画问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 zyc ...

  7. Inspector's Dilemma(欧拉通路)

    In a country, there are a number of cities. Each pair of city is connected by a highway, bi-directio ...

  8. hdu3472 混合图判断欧拉通路

    对于欧拉回路,先判断出度入度的差是否为偶数,然后最大流一次. 此题是判断有无欧拉通路,前提要判断图是否连通,然后欧拉通路的条件:要么出入度差没有奇数,或者只有2个点. 所以先统计差为奇数的个数,如果不 ...

  9. FZU 2112 并查集、欧拉通路

    原题:http://acm.fzu.edu.cn/problem.php?pid=2112 首先是,票上没有提到的点是不需要去的. 然后我们先考虑这个图有几个连通分量,我们可以用一个并查集来维护,假设 ...

随机推荐

  1. cleanMyMac

    想看外国网站可以找我,facebook.youtube.XX大片等,只要8元钱,无限制用到服务器关闭.看大片流畅不成问题 需要cleanMyMac的请加微信只要10或直接拍 http://a.p6ff ...

  2. 前端之 HTML🎃

    HTML这知识点很多很杂,所以整理很乱.所以将就看.

  3. 漂亮的提示框SweetAlert使用教程

    一.简介 所使用过的弹出框插件,SweetAlert是最好用的.发展至今,已经有两个版本,一个是原版 t4t5/sweetalert , 一个是分支版 limonte/sweetalert2 ,更新相 ...

  4. php代码审计一些笔记

    之前学习了seay法师的代码审计与及80sec的高级审计,整理了一些笔记在印象里面,也发到这里作为记录 1,漏洞挖掘与防范(基础篇) sql注入漏洞            挖掘经验:注意点:登录页面, ...

  5. Go执行远程ssh命令

    使用包:golang.org/x/crypto/ssh 以下封装一个发送命令的Cli结构体 type Cli struct { IP string //IP地址 Username string //用 ...

  6. win10安装Tensorflow

    win10安装Tensorflow 前提: 保证你的pip>=8.1版本 否则利用python -m pip install -U pip  进行升级,或下载pip源文件 确定你的显卡是否支持c ...

  7. rabbitmq:centos7安装与python调用

    1.centos安装rabbitmq 官网下载或者yum list |grep rabbitmq搜索安装,官网是最新的版本 wget http://www.rabbitmq.com/releases/ ...

  8. js 拼接table 的方法

    <html> <head> <title>test page</title> <script type='text/javascript'> ...

  9. 2017年 JavaScript 框架回顾 -- 前端框架

    概述: 对于 JavaScript 社区来说,npm 的主要功能之一就是帮助开发者发掘所需的 npm Registry 中的库和框架.npm 强大的搜索功能能够帮助找到一组相关的软件包,同时其内置的的 ...

  10. c#中常用集合类和集合接口之接口系列【转】

    常用集合接口系列:http://www.cnblogs.com/fengxiaojiu/p/7997704.html 常用集合类系列:http://www.cnblogs.com/fengxiaoji ...