hdu 1116 Play on Words(欧拉通路)
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.
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 Nthat indicates the number of plates ( <= N <= ). Then exactly Nlines follow, each containing a single word. Each word contains at least two and at most lowercase characters, that means only letters 'a' through 'z' will appear in the word. The same word may appear several times in the list.
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.".
acm
ibm acm
malform
mouse ok
ok
The door cannot be opened.
Ordering is possible.
The door cannot be opened.
先将头尾转换为数字。算出各个点的入出度数。标记0-26哪些点有出现。
判断是否为欧拉通路。1、根<=1
2、有0个入出度数不同 || 2个入出度数不同且相差1
其余全部不是欧拉通路。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 1006
#define inf 1e12
int n;
char s[N];
int a[];
int b[];
int vis[],fa[];
void init(){
for(int i=;i<;i++){
fa[i]=i;
}
}
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
void merge(int x,int y){
int root1=find(x);
int root2=find(y);
if(root1==root2) return;
fa[root1]=root2;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
init();//尼玛又给忘了。。。
memset(a,,sizeof(a));
memset(b,,sizeof(b));
memset(vis,,sizeof(vis));
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%s",s);
int len=strlen(s);
int num1=s[]-'a';
int num2=s[len-]-'a';
merge(num1,num2);
b[num1]++;
a[num2]++;
vis[num1]=;
vis[num2]=;
}
int cnt=;
for(int i=;i<;i++){
if(find(i)==i && vis[i]){
cnt++;
}
}
if(cnt>){
printf("The door cannot be opened.\n");
continue;
}
int tmp=;
int p[];
for(int i=;i<;i++){
if(a[i]!=b[i] && vis[i]){
p[tmp]=i;
tmp++;
}
}
if(tmp==){
printf("Ordering is possible.\n");
continue;
}
if(tmp== && (a[p[]]-b[p[]]== && b[p[]]-a[p[]]== || b[p[]]-a[p[]]== && a[p[]]-b[p[]]==)){
printf("Ordering is possible.\n");
continue;
}
printf("The door cannot be opened.\n");
}
return ;
}
hdu 1116 Play on Words(欧拉通路)的更多相关文章
- HDU 5883 F - The Best Path 欧拉通路 & 欧拉回路
给定一个图,要求选一个点作为起点,然后经过每条边一次,然后把访问过的点异或起来(访问一次就异或一次),然后求最大值. 首先为什么会有最大值这样的分类?就是因为你开始点选择不同,欧拉回路的结果不同,因为 ...
- ACM/ICPC 之 DFS求解欧拉通路路径(POJ2337)
判断是欧拉通路后,DFS简单剪枝求解字典序最小的欧拉通路路径 //Time:16Ms Memory:228K #include<iostream> #include<cstring& ...
- POJ 1300 欧拉通路&欧拉回路
系统的学习一遍图论!从这篇博客开始! 先介绍一些概念. 无向图: G为连通的无向图,称经过G的每条边一次并且仅一次的路径为欧拉通路. 如果欧拉通路是回路(起点和终点相同),则称此回路为欧拉回路. 具有 ...
- poj 2513 连接火柴 字典树+欧拉通路 好题
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27134 Accepted: 7186 ...
- poj2513- Colored Sticks 字典树+欧拉通路判断
题目链接:http://poj.org/problem?id=2513 思路很容易想到就是判断欧拉通路 预处理时用字典树将每个单词和数字对应即可 刚开始在并查集处理的时候出错了 代码: #includ ...
- hdu1116有向图判断欧拉通路判断
Play on Words Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- Colored Sticks POJ - 2513 并查集+欧拉通路+字典树hash
题意:给出很多很多很多很多个棒子 左右各有颜色(给出的是单词) 相同颜色的可以接在一起,问是否存在一种 方法可以使得所以棒子连在一起 思路:就是一个判欧拉通路的题目,欧拉通路存在:没奇度顶点 或者 ...
- 欧拉回路&欧拉通路判断
欧拉回路:图G,若存在一条路,经过G中每条边有且仅有一次,称这条路为欧拉路,如果存在一条回路经过G每条边有且仅有一次, 称这条回路为欧拉回路.具有欧拉回路的图成为欧拉图. 判断欧拉通路是否存在的方法 ...
- POJ2513Colored Sticks(欧拉通路)(字典树)(并查集)
Colored Sticks Time Limit: 5000MS Memory ...
- POJ 2513 无向欧拉通路+字典树+并查集
题目大意: 有一堆头尾均有颜色的木条,要让它们拼接在一起,拼接处颜色要保证相同,问是否能够实现 这道题我一开始利用map<string,int>来对颜色进行赋值,好进行后面的并查操作以及欧 ...
随机推荐
- redhat6.3 64位更新源(使用网易源)全过程记录
本篇博客参考:http://chinaxiaoyu.diandian.com/post/2013-01-24/40046529897.首先在浏览器中输入http://tel.mirrors.163.c ...
- tr转换或删除字符
字符处理命令:tr —— 转换或删除字符 逐个字符处理而不是处理单词的tr [OPTION]... SET1 [SET2] -d: 删除出现在字符集中的所有字符 tr ab AB
- sun.misc.BASE64Encoder找不到包,解决方法
右键项目->属性->java bulid path->jre System Library->access rules->resolution选择accessible,以 ...
- with ffmpeg to encode video for live streaming and for recording to files for on-demand playback
We've been doing some experimentation with ffmpeg to encode video for live streaming and for recordi ...
- 【Java基础】foreach循环
从一个小程序说起: class lesson6foreach { public static void main(String[] args) { int array[]={2,3,1,5,4,6}; ...
- Alter的用法(添加字段,删除字段,修改字段名)
1.在表emp中新增字段sexy(性别) alter table emp add sexy varchar2(2); 新增多个字段cxx 和shoneworn alter table emp add ...
- Sql Server相关的性能计数器
OS Memory and Paging 性能计数器: 1.Memory\Availability Mbytes 未使用的物理内存(非页面文件),通常情况下它应该大于100MB 2.Memory\ ...
- 我们为什么要遵循W3C标准规范
大部分的站长和拥有网站的企业负责人都会知道,每当有浏览器发布大更新的时候,我们刚建立不久的网站就会发生无法预知的严重错误,我们只能重新建立或改版网站,使其可以应归新发布的浏览器.好比1996-1999 ...
- php 记住密码自动登录
当我们登录网站管理后台的时候,会有提示说记住登录状态,记住我等这样的提示,这个选项有什么用呢?如果选中了记登录状态后,下次在浏览这个网站后 台时就不用在填写用户名和密码了,在去登录了.下面写了一小代码 ...
- Windows -DOS 下Yii创建应用及出错处理
Win7下,以管理员身份运行cmd.exe 本例是在wamp环境下:切换到d盘,再切换到framework目录.............1--C:\windows\system32>d: 2-- ...