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. MEF IOC使用

    IOC介绍 IOC:控制反转,DI:依赖注入.按我的理解应该是一个东西.作用目前我看到的主要是解除各个层之间的强耦合,实现接口分离.MEF优点: 1.net4 自带,无需安装扩展(引用System.C ...

  2. MVC导入命名空间

    为什么要导入 一次性导入,避免每个页面都要导入,代码看起来更为清晰,不再带一个长长的命名空间,视图里面可以直接写类名了. 导入方法 在Views文件夹的web.config的namespaces里面配 ...

  3. servlet中doPost()和doGet()

    转载至 http://blog.163.com/grandry_it_bird/blog/static/1751633362010102615553610/ 一般来说我们是用不到doGet方法的,do ...

  4. Entity Framework Core Like 查询揭秘

    在Entity Framework Core 2.0中增加一个很酷的功能:EF.Functions.Like(),最终解析为SQL中的Like语句,以便于在 LINQ 查询中直接调用. 不过Entit ...

  5. [转]Java7中的ForkJoin并发框架初探(上)——需求背景和设计原理

    详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp83 这篇我们来简要了解一下JavaSE7中提供的一个新特性 -- For ...

  6. js操作cookie 使用详解

    详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp62  JavaScript中的另一个机制:cookie,则可以达到真正全 ...

  7. JMeter打开脚本失败 如何解决?

    最近有碰到JMeter打开之前的脚本,报错了,见下图: 后来发现这是因为之前保存脚本的 jmeter 和这次打开脚本的 jmeter 版本不一致(图一)或者版本一致而插件没有保持同步(图二)的原因: ...

  8. AppiumDesktop用法介绍

    转自:http://www.jianshu.com/p/bf1ca3d4ac76 写这篇文章的心情 真的很开心,我看着官网介绍竟然对AppiumDesktop略懂皮毛了.今天特意写出来,希望可以帮助一 ...

  9. Domain Driven Design

    在Spring官网的第一个tutorial中看到了这种 设计模式 Domain Driven Design 找到了篇介绍这个得文章: What is Domain Driven Design? &qu ...

  10. OpenCppCoverage 的使用

    OpenCppCoverage 的使用 OpenCppCoverage 是一款好用方便的 C++ 代码覆盖率检测工具,可以独立在命令行运行也可以作为 Visual Studio 13/15/17 的插 ...