UVA10129-Play on Words(欧拉路径)
Problem UVA10129-Play on Words
Accept: 2534 Submit: 19477
Time Limit: 3000 mSec
Problem Description
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.
Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing a single integer number N that indicates the number of plates (1 ≤ N ≤ 100000). Then exactly N lines follow, each containing a single word. Each word contains at least two and at most 1000 lowercase characters, that means only letters ‘a’ through ‘z’ will appear in the word. The same word may appear several times in the list.
Output
Your program has to determine whether it is possible to arrange all the plates in a sequence such that the first letter of each word is equal to the last letter of the previous word. All the plates from the list must be used, each exactly once. The words mentioned several times must be used that number of times. 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.
题解:欧拉路径模板题,这里面运用并查集判断联通,对于欧拉路径的判断,lrj的代码给了一个很好的模板,简单严密
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
using namespace std; const int maxl = +;
const int kind = ;
char str[maxl];
int n,pre[kind],deg[kind];
bool used[kind]; int findn(int x){
return x == pre[x] ? x : pre[x] = findn(pre[x]);
} int main()
{
//freopen("input.txt","r",stdin);
int iCase;
scanf("%d",&iCase);
while(iCase--){
scanf("%d",&n);
memset(used,false,sizeof(used));
memset(deg,,sizeof(deg));
for(int i = ;i < ;i++) pre[i] = i;
int cnt = ;
for(int i = ;i <= n;i++){
scanf("%s",str);
int x1 = str[]-'a',x2 = str[strlen(str)-]-'a';
used[x1] = used[x2] = true;
deg[x1]++,deg[x2]--;
int fx1 = findn(x1),fx2 = findn(x2);
if(fx1 != fx2){
pre[fx1] = fx2;
cnt--;
}
}
vector<int> ans;
for(int i = ;i < kind;i++){
if(!used[i]) cnt--;
else if(deg[i] != ){
ans.push_back(deg[i]);
}
}
bool ok = false;
if(cnt== && (ans.empty() || (ans.size()==) && (ans[]== || ans[]==-))) ok = true;
if(ok) printf("Ordering is possible.\n");
else printf("The door cannot be opened.\n");
}
return ;
}
UVA10129-Play on Words(欧拉路径)的更多相关文章
- [欧拉路径]Play on Words UVA10129
传送门: UVA - 10129 题目大意: 给定一些单词(可能会重复出现),判断单词是否能排成一个序列,前提是单词的最后一个字母与下一个单词的第一个字母相同.输出"The door c ...
- Play on Words UVA - 10129 欧拉路径
关于欧拉回路和欧拉路径 定义:欧拉回路:每条边恰好只走一次,并能回到出发点的路径欧拉路径:经过每一条边一次,但是不要求回到起始点 ①首先看欧拉回路存在性的判定: 一.无向图每个顶点的度数都是偶数,则存 ...
- UVA10129 Play on Words —— 欧拉回路
题目链接:https://vjudge.net/problem/UVA-10129 代码如下: // UVa10129 Play on Words // Rujia Liu // 题意:输入n个单词, ...
- 【USACO 3.3】Riding The Fences(欧拉路径)
题意: 给你每个fence连接的两个点的编号,输出编号序列的字典序最小的路径,满足每个fence必须走且最多走一次. 题解: 本题就是输出欧拉路径. 题目保证给出的图是一定存在欧拉路径,因此找到最小的 ...
- hdu 3472 HS BDC(混合路的欧拉路径)
这题是混合路的欧拉路径问题. 1.判断图的连通性,若不连通,无解. 2.给无向边任意定向,计算每个结点入度和出度之差deg[i].deg[i]为奇数的结点个数只能是0个或2个,否则肯定无解. 3.(若 ...
- poj 2337 有向图输出欧拉路径
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10186 Accepted: 2650 Descrip ...
- nyoj 42 一笔画问题 欧拉路径
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=42 欧拉回路,欧拉路径水题~ 代码: #include "stdio.h&quo ...
- UESTC 917 方老师的分身IV --求欧拉路径
判断欧拉路径是否存在及求出字典序最小的欧拉路径问题(如果存在). 将字符串的第一个字母和最后一个字母间连边,将字母看成点,最多可能有26个点(a-z),如果有欧拉路径,还要判断是否有欧拉回路,如果有, ...
- POJ1780 Code(欧拉路径)
n位密码,要用尽可能短的序列将n位密码的10n种状态的子串都包括,那么要尽量地重合. 题目已经说最短的是10n + n - 1,即每一个状态的后n-1位都和序列中后一个状态的前n-1位重合. 这题是经 ...
- hdu 1116 并查集和欧拉路径
---恢复内容开始--- 把它看成是一个图 只是需要欧拉路径就可以了 首尾能连成一条线即可 如果要判断这个图是否连通 得用并查集 在hrbust oj里面看答案学到的方法 不用各种for循环套着判断能 ...
随机推荐
- npm包
https://www.cnblogs.com/xinxingyu/p/5736244.html node - glob模块讲解 https://github.com/isaacs/node- ...
- 设计模式之享元模式(Flyweight)
享元模式顾名思义就是羽量级模式或者蝇级模式,形容体量小的应用,该模式主要的设计目的是为了迎合系统大量相似数据的应用而生,减少用于创建和操作相似的细碎对象所花费的成本.大量的对象会消耗高内存,享元模式给 ...
- HDU1559
最大子矩阵 Time Limit: 30000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- blfs(systemv版本)学习笔记-wget的安装与配置
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! blfs wget项目地址:http://www.linuxfromscratch.org/blfs/view/8.3/basi ...
- 自定义基于jquery竖向瀑布流插件
公司新项目做了一个关于图片的板块,网上找了一些瀑布流插件都不是很适合自己,于是就自己造轮子写一个,并封装成插件github 于是就想分享一下,主要是为了更好的学习与记忆. 如果大家进来了,希望能给我g ...
- 【读书笔记】iOS-nonatomic
原子性atomicity(nonatomic),是关系线程安全的,但是会影响性能.如果确定不考虑线程安全问题可以使用nonatomic. 参考资料:<iPhone与iPad开发实战-iOS经典应 ...
- mysql之行(记录)的详细操作
在Mysql管理软件中, 可以通过sql语句中的dml语言来实现数据的操作, 包括 使用INSERT实现数据的插入 UPDATE实现数据的更新 使用DELETE实现数据的删除 使用SELECT查询数据 ...
- gulp使用 笔记
全局安装gulp,也需要本地安装gulp插件.全局安装gulp是为了执行gulp任务,本地安装gulp则是为了调用gulp插件的功能 //导入工具包 require('node_modules里对应模 ...
- 实践:配置keepalived实现主备热备份功能
图: 配置文件: 主服务器的配置如下: global_defs { router_id NodeA}vrrp_instance VI_1 { state MASTER #设置为主服务器 interfa ...
- 使用katalon自带Spy功能获取/验证控件Selector、XPath
背景 最近刚接手一个katalon编写的UI自动化项目,页面最近刚改版,已有用例很多查找元素失败.了解到katalon元素定位支持xpath,所以直接使用chrome开发者工具打开目标页面+获取xpa ...