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循环套着判断能 ...
随机推荐
- Java中的静态变量、静态方法问题
由关键字static所定义的变量与方法,分别称为静态变量和静态方法,它们又都被称为静态成员 1.静态方法 无需本类的对象也可以调用此方法,调用形式为“类名.方法名”,静态方法常常为其他类提供一些方法而 ...
- python程序编写中常见错误
1,NameError语法错误 s还没定义,给s赋值就行了 2,IndexError 索引错误 对于列表l1来说,只有4个元素,所以l1的Index只能是0-3,当你所输入的Index不在这范围,就会 ...
- [转]ionic工作原理
本文转自:https://segmentfault.com/a/1190000011495654 1.Ionic的工作原理 Ionic通过cordova把一个Web应用嵌入原生应用 用户打开一个ion ...
- 【手记】解决“未能创建 SSL/TLS 安全通道”异常
之前写了一个桌面程序,程序会间歇性访问某个https接口,一直用的好好的,今天突然报错了,异常就发生在访问接口的地方,曰“请求被中止,未能创建 SSL/TLS 安全通道.”,另外有台电脑也有跑该程序, ...
- Dynamics CRM 2016/365 窗体中添加按钮
一.工具下载,及界面介绍 1.下载XrmToolBox工具(XrmToolBox for Microsoft Dynamics CRM/365 CE) 链接:https://www.xrmtoolbo ...
- 【表格设置】HTML中合并单元格,对列组合应用样式,适应各浏览器的内容换行
1.常用表格标签 普通 <table> | <tr> | | <th ...
- jquery 获得下拉框的值《转》
获取Select : 获取select 选中的 text : $("#ddlRegType").find("option:selected").text(); ...
- linux下使用gcc编译运行C/C++程序
编译C 首先,程序编译过程有: 1.预处理(展开宏,头文件,检查代码是否有误) 2.编译(将.c转为汇编代码.s) 3.汇编(将汇编代码.s转为机器代码.o) 4.链接(将所有机器代码.o和库文件链 ...
- JS如何判断一个对象是否为空、是否有某个属性
一.js判断一个对象是否为空 方法一: let obj1 = {} let obj2 = {a:1} function empty(obj){ for (let key in obj){ return ...
- CSS-高度塌陷问题
目录 CSS-高度塌陷问题 表现 产生的原因 高度塌陷的解决办法: BFC相关 CSS-高度塌陷问题 表现 例如: HTML: <div class="first"> ...