博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1116 Play on Words【欧拉通路or回路】
阅读量:7097 次
发布时间:2019-06-28

本文共 2816 字,大约阅读时间需要 9 分钟。

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)所有的点联通
2)欧拉回路中所有点的入度和出度一样。
3)欧拉通路中终点的入度 - 出度 = 1,起点的 初度 - 入度 = 1, 其他的所有点入度 = 出度;
code:
View Code
#include
#include
int f[26]; int in[26]; int out[26]; int v[26]; int find(int x) {
int r=x; while(r!=f[r]) r=f[r]; int i=x,j; while(i!=r) {
j=f[i]; f[i]=r; i=j; } return r; } void join(int x,int y) {
int fx=find(x); int fy=find(y); if(fx!=fy) {
if(fx
1) printf("The door cannot be opened.\n"); else {
for(i=tot=0;i<26;i++) if(in[i]!=out[i]&&v[i]) x[tot++]=i; if(tot==0) printf("Ordering is possible.\n"); else if(tot==2&&((in[x[0]]-out[x[0]]==1&&out[x[1]]-in[x[1]]==1)||(in[x[1]]-out[x[1]]==1&&out[x[0]]-in[x[0]]==1))) printf("Ordering is possible.\n"); else printf("The door cannot be opened.\n"); } } return 0; }

转载于:https://www.cnblogs.com/dream-wind/archive/2012/03/16/2399558.html

你可能感兴趣的文章
字符编码
查看>>
php下intval()和(int)
查看>>
WordPress超级基本教程(转)
查看>>
Python基础 3----文件和网络
查看>>
模块的耦合和内聚
查看>>
对话框
查看>>
迁移SQL SERVER 数据库实例
查看>>
HttpClient工具类v1.7
查看>>
Sqlite中使用rowid来表示行号,用于分页。
查看>>
HDU 4916 树形dp
查看>>
远程数据库迁移数据
查看>>
ZH奶酪:LAMP环境中如何重新部署一个Yii2.0 web项目
查看>>
一些有用的java 框架
查看>>
访问不了firefox附加组件页面怎么办
查看>>
Docker image 镜像介绍
查看>>
Java线程池
查看>>
ArrayList,LinkedList,Vector,Stack之间的区别
查看>>
Freemarker常用技巧(二)
查看>>
2.C#中通过委托Func消除重复代码
查看>>
[转] 基于PHP Stream Wrapper开发有趣应用场景
查看>>