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>来对颜色进行赋值,好进行后面的并查操作以及欧 ...
随机推荐
- UESTC_棋盘游戏 CDOJ 578
最近昀昀学习到了一种新的棋盘游戏,这是一个在一个N×N的格子棋盘上去搞M个棋子的游戏,游戏的规则有下列几条: 棋盘上有且仅有一个出口 开始时没有哪个棋子在出口,而且所有棋子都不相邻(这里的相邻是指上下 ...
- UESTC_How many good substrings CDOJ 1026
Icerain likes strings very much. Especially the strings only consist of 0 and 1,she call them easy s ...
- k-d Tree in TripAdvisor
Today, TripAdvisor held a tech talk in Columbia University. The topic is about k-d Tree implemented ...
- PSPInstance Object | Web Python
PSPInstance Object | Web Python The PSPInstance object is available to all PSP pages through the psp ...
- 【转】android camera(三):camera V4L2 FIMC
关键词:android camera CMM 模组 camera参数 CAMIF V4L2 平台信息:内核:linux系统:android 平台:S5PV310(samsung exynos ...
- libaio under MIPS architecture /在mips架构下使用的libaio
First, you can find libaio source in http://libaio.sourcearchive.com/ Second,download the libaio_0.3 ...
- DNS,ARP,RARP,NAT,WINS的作用和区别
DNS 域名服务系统,是将域名(比如www.cnblogs.com)转成ip地址.arp 地址转换协议,是将ip地址转成mac地址(物理地址,可用ipconfig /all查看).rarp从mac转到 ...
- Spring--------web应用中保存spring容器
---恢复内容开始--- 问题:在一个web应用中我使用了spring框架,但有一部分模块或组件并没有托管给Spring,比如有的可能是一个webservice服务类,如果我想在这些非托管的类里使用托 ...
- jdbc 日期 时间
//pstmt.setDate(1, new Date(new java.util.Date().getTime())); pstmt.setTime(1, new Time(new java.uti ...
- 无法启动计算机"."上的服务w3svc
在启动IIS服务的时候出现错误: 无法启动计算机"."上的服务w3svc 解决方法: /* 修复错误 运行命令提示符 fsutil resource setautoreset tr ...