Play on Words

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4263    Accepted Submission(s): 1384

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
Nthat indicates the number of plates (1 <= N <= 100000). Then exactly Nlines 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.

有向图欧拉通路判断题:

•欧拉路径:从图的某一个顶点出发,图中每条边走且仅走一次,最后到达某一个点;如果这样的路径存在,则称之为欧拉路径。
 
 
•无向图欧拉路径存在条件:至多有两个顶点的度数为奇数,其他顶点的度数均为偶数。
 
 
•有向图欧拉路径存在条件:至多有两个顶点的入度和出度绝对值差1(若有两个这样的顶点,则必须其中一个出度大于入度,另一个入度大于出度),其他顶点的入度与出度相等。
 
 
 
•半欧拉图 :具有欧拉通路而无欧拉回路的图
 
•欧拉回路:从图的某一个顶点出发,图中每条边走且仅走一次,最后回到出发点;如果这样的回路存在,则称之为欧拉回路。
 
 
•无向图欧拉回路存在条件:所有顶点的度数均为偶数并且连通。
 
•有向图欧拉回路存在条件:所有顶点的入度和出度相等并且连通。
 

 #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <vector>
#include <stack>
using namespace std;
#define ll long long int
int a[];
int b[];
int c[];
int find(int x)
{
if(x!=a[x])
a[x]=find(a[x]);
return a[x];
}
int main()
{
int n,m,r;
scanf("%d",&n);
for(r=; r<n; r++)
{
cin>>m;
int i,x,y;
for(i=; i<; i++)
a[i]=i;
memset(b,,sizeof(b));
memset(c,,sizeof(c));
char aa[];
for(i=; i<=m; i++)
{
scanf("%s",aa);
int t=strlen(aa);
x=aa[]-'a'+;
y=aa[t-]-'a'+;
b[x]++;
c[y]++;
int fx=find(x);
int fy=find(y);
if(fy!=fx)
a[fy]=fx;
}
int suma=,sumb=;;
int fla=;
for(i=; i<; i++)
{
if((b[i]||c[i])&&a[i]==i)
fla++;
}
if(fla==)
{
for(i=;i<;i++)
{
if(b[i]!=c[i])
{
if(b[i]-c[i]==)
suma++;
else if(c[i]-b[i]==)
sumb++;
else sumb+=;
}
}
if(suma<=&&sumb<=)
cout<<"Ordering is possible."<<endl;
else cout<<"The door cannot be opened."<<endl;
}
else
cout<<"The door cannot be opened."<<endl;
}
}

hdu1116有向图判断欧拉通路判断的更多相关文章

  1. 欧拉回路&欧拉通路判断

    欧拉回路:图G,若存在一条路,经过G中每条边有且仅有一次,称这条路为欧拉路,如果存在一条回路经过G每条边有且仅有一次, 称这条回路为欧拉回路.具有欧拉回路的图成为欧拉图. 判断欧拉通路是否存在的方法 ...

  2. poj2513- Colored Sticks 字典树+欧拉通路判断

    题目链接:http://poj.org/problem?id=2513 思路很容易想到就是判断欧拉通路 预处理时用字典树将每个单词和数字对应即可 刚开始在并查集处理的时候出错了 代码: #includ ...

  3. POJ 1386 Play on Words(有向欧拉通路 连通图)

    题意  见下方中文翻译 每一个单词能够看成首尾两个字母相连的一条边  然后就是输入m条边  推断是否能构成有向欧拉通路了 有向图存在欧拉通路的充要条件: 1. 有向图的基图连通: 2. 全部点的出度和 ...

  4. POJ 1300 欧拉通路&欧拉回路

    系统的学习一遍图论!从这篇博客开始! 先介绍一些概念. 无向图: G为连通的无向图,称经过G的每条边一次并且仅一次的路径为欧拉通路. 如果欧拉通路是回路(起点和终点相同),则称此回路为欧拉回路. 具有 ...

  5. ACM/ICPC 之 DFS求解欧拉通路路径(POJ2337)

    判断是欧拉通路后,DFS简单剪枝求解字典序最小的欧拉通路路径 //Time:16Ms Memory:228K #include<iostream> #include<cstring& ...

  6. Colored Sticks POJ - 2513 并查集+欧拉通路+字典树hash

    题意:给出很多很多很多很多个棒子 左右各有颜色(给出的是单词) 相同颜色的可以接在一起,问是否存在一种 方法可以使得所以棒子连在一起 思路:就是一个判欧拉通路的题目,欧拉通路存在:没奇度顶点   或者 ...

  7. POJ 2513 无向欧拉通路+字典树+并查集

    题目大意: 有一堆头尾均有颜色的木条,要让它们拼接在一起,拼接处颜色要保证相同,问是否能够实现 这道题我一开始利用map<string,int>来对颜色进行赋值,好进行后面的并查操作以及欧 ...

  8. poj 2513 连接火柴 字典树+欧拉通路 好题

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27134   Accepted: 7186 ...

  9. POJ2513Colored Sticks(欧拉通路)(字典树)(并查集)

                                                             Colored Sticks Time Limit: 5000MS   Memory ...

随机推荐

  1. Centos 7 PXE一键安装

    author:JevonWei 版权声明:原创作品 192.168.198.134作为安装服务器,由httpd服务共享安装程序 192.168.198.134作为dhcp服务器,客户机获取IP 一.安 ...

  2. MySQL的JOIN(二):JOIN原理

    表连接算法 Nested Loop Join(NLJ)算法: 首先介绍一种基础算法:NLJ,嵌套循环算法.循环外层是驱动表,循坏内层是被驱动表.驱动表会驱动被驱动表进行连接操作.首先驱动表找到第一条记 ...

  3. chrome开发工具指南(十一)

    检查资源 使用 Application 面板的 Frames 窗格可以按框架组织资源. 您也可以在 Sources 面板中停用 Group by folder 选项,按框架查看资源. 要按网域和文件夹 ...

  4. grunt之clean、copy

    心情不太好,正好这部分比较简单,记个流水账. ----------流水很清楚惜花这个责任,真的身份不过送运---------- clean.copy算是很重要也很简单的基本组件了. clean(V0. ...

  5. C# XmlDocument操作XML

    XML:Extensible Markup Language(可扩展标记语言)的缩写,是用来定义其它语言的一种元语言,其前身是SGML(Standard Generalized Markup Lang ...

  6. PHP初入,for循环使用

    一: 找出100-999之间的所有"水仙花数".所谓水仙花数是指一个三位 数,各位数字的立方和等于该数本身.(如153次方=1的3次方+5的3次方+3的3次方)并输出这些数字 想想 ...

  7. make: Nothing to be done for 'all' 解决方法

    make: Nothing to be done for 'all' 解决方法 1.这句提示是说明你已经编译好了,而且没有对代码进行任何改动. 若想重新编译,可以先删除以前编译产生的目标文件:make ...

  8. 【1414软工助教】团队作业9——测试与发布(Beta版本) 得分榜

    题目 团队作业9--测试与发布(Beta版本) 往期成绩 个人作业1:四则运算控制台 结对项目1:GUI 个人作业2:案例分析 结对项目2:单元测试 团队作业1:团队展示 团队作业2:需求分析& ...

  9. Beta阶段事后诸葛亮分析

    1.总结的提纲内容 a. 项目管理之事后诸葛亮会 设想和目标 1.我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件主要解决用户无意识花钱,无法清楚看见钱去 ...

  10. 团队作业八——第二次团队冲刺(Beta版本)第4天

    团队作业八--第二次团队冲刺(Beta版本)第4天 一.每个人的工作 (1) 昨天已完成的工作 做一下用户注册的功能和登录功能. (2) 今天计划完成的工作 完成界面跳转 (3) 工作中遇到的困难 界 ...