hdu 1116 Play on Words (欧拉路径)
来源:互联网 发布:银联数据服务有限公司 编辑:程序博客网 时间:2024/04/28 16:51
Play on Words
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.
#include<stdio.h>#include<string.h>#define N 100010#define M 1010int in[30],out[30],pre[30],count[30];char str[M];int find(int k){ if(pre[k]!=k)pre[k]=find(pre[k]); return pre[k];}int main(){ int i,n,T; scanf("%d",&T); while(T--) { scanf("%d",&n); for(i=0;i<26;i++) pre[i]=i; memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); memset(count,0,sizeof(count)); //统计出现字母类数 while(n--) { scanf("%s",str); int a,b,n1=strlen(str)-1,f1,f2; a=str[0]-'a'; b=str[n1]-'a'; out[a]++; in[b]++; count[a]=1; count[b]=1; f1=find(a); f2=find(b); if(f1!=f2) pre[f1]=f2; } int num=0; for(i=0;i<26;i++) //是否连通 if(pre[i]==i&&count[i]) num++; if(num>1) printf("The door cannot be opened.\n"); else { int f1=0,f2=0,flag=1; for(i=0;i<26;i++) { if(in[i]-out[i]==1) f1++; else if(in[i]-out[i]==-1) f2++; else if(in[i]!=out[i]) //入度等于出度 flag=0; } if(flag&&f1==f2) printf("Ordering is possible.\n"); else printf("The door cannot be opened.\n"); } } return 0;}
- hdu 1116 Play on Words (欧拉路径)
- HDU 1116 && POJ 1386 Play on Words(欧拉路径)
- HDU 1116&UVA10129 Play on Words 欧拉路径
- [HDU 1116]Play on Words(欧拉回路/欧拉路径+并查集)
- HDU 1116 Play on Words(欧拉路径(有向图)+并查集)
- HDU 1116 && UVA 10129 && POJ 1386 ------ Play on Words(欧拉路径 + 并查集)
- hdu oj 1116 Play on Words(并查集+欧拉路径)
- HDU 1116 Play on Words(欧拉回路)
- HDU 1116 Play on Words (欧拉回路)
- (欧拉回路) hdu 1116 Play on Words
- hdu 1116 Play on Words(欧拉通路)
- HDU 1116 Play on Words(欧拉通路)
- hdu 1116 Play on Words(并查集+欧拉回路|| 欧拉路径)
- POJ1386 Play on Words 欧拉路径
- poj1386 Play on Words 欧拉路径
- 欧拉路径 Play on Words
- 【并查集 欧拉路径】hdu 1116 Play on Words
- poj1386 Play on Words (欧拉路径)
- IntelliJ IDEA 12.0 序列号及其配置
- PHP连接sqlserver2008r2配置
- XML---DOM4J解析XML文件
- RelativeLayout.LayoutParams
- 递归版插入排序
- hdu 1116 Play on Words (欧拉路径)
- 数据结构全攻略--概念篇之图
- 开源 免费 java CMS - FreeCMS1.5-数据对象-job
- QT MessageBox 头文件引用
- php自定义编写的函数 代替json函数json_encode
- 基于纹理特征的指纹识别
- 如何向Action传List、Map等参数
- invalid conversion from 'void* (*)()' to 'void* (*)(void*)'
- cocos2d-x宏定义