HDU1116(Play On Words)可做欧拉回路模板
来源:互联网 发布:mac打开移动硬盘空白 编辑:程序博客网 时间:2024/06/10 00:25
水题吧。。搞清楚欧拉回路的判断方法这题的难度就为0了。并查集判联通还有结点出入度关系搞清楚就好了。
Play on Words
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4339 Accepted Submission(s): 1406
Total Submission(s): 4339 Accepted Submission(s): 1406
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.
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.".
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
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 <iostream>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std; const int maxn=30; int p[maxn];int f[maxn];int indegree[maxn];int outdegree[maxn];int vis[maxn];char str[maxn]; int Find_set(int x){ return p[x]==x?x:p[x]=Find_set(p[x]);} int Union(int a,int b){ int x=Find_set(a); int y=Find_set(b); if(x!=y) p[y]=x;} int main(){ int t,n,i; scanf("%d",&t); while(t--) { for(int i=0;i<=26;i++) p[i]=i; memset(indegree,0,sizeof(indegree)); memset(outdegree,0,sizeof(outdegree)); memset(vis,0,sizeof(vis)); memset(f,0,sizeof(f)); scanf("%d",&n); for(int ii=1;ii<=n;ii++) { scanf("%s",str); int first=str[0]-'a'; vis[first]=1; int last=str[strlen(str)-1]-'a'; vis[last]=1; indegree[last]++;//记录入度出度; outdegree[first]++; Union(first,last); } int root_num=0; for(int i=0;i<=26;i++) { if(vis[i]&&p[i]==i) root_num++;//统计根节点个数; } if(root_num>1) { printf("The door cannot be opened.\n"); continue;//未构成欧拉回路; } for(root_num=0,i=0;i<=26;i++) { if(indegree[i]!=outdegree[i]) f[root_num++]=i; } if(root_num==0) { printf("Ordering is possible.\n"); continue; } if(root_num==2) if((indegree[f[0]]==outdegree[f[0]]+1&&indegree[f[1]]==outdegree[f[1]]-1)||(indegree[f[0]]==outdegree[f[0]]-1&&indegree[f[1]]==outdegree[f[1]]+1)) { printf("Ordering is possible.\n"); continue; } printf("The door cannot be opened.\n"); } return 0;}
- HDU1116(Play On Words)可做欧拉回路模板
- hdu1116 Play on Words
- hdu1116 Play on Words(典型欧拉回路)
- hdu1116 Play on Words 判断能否构成欧拉回路
- HDU1116-Play On Words 并查集,欧拉回路
- hdu1116—Play on Words(欧拉回路)
- HDU1116(Play on Words)
- ZOJ2016 POJ1386 HDU1116 Play on Words,欧拉路径及欧拉回路
- UVA10129 POJ1386 HDU1116 ZOJ2016 Play on Words【欧拉回路+并查集】
- HDU1116--Play on Words HDU(164)
- hdu1116——Play on Words
- hdu1116 Play on Words--并查集
- HDU1116 Play on Words 【欧拉图】+【并查集】
- HDU1116 Play on words(欧拉回路加并查集)
- POJ Play on Words(欧拉回路)
- poj1386 Play on Words (欧拉回路)
- (欧拉回路)Play on Words(P1386)
- Play on Words 并查集加欧拉回路
- S 串口编程 详解3 串口的初始化、打开/关闭
- 常用的浮点数存储格式:32-bit IEEE-754 floating-point format
- 【二叉树】后续遍历二叉树
- ubuntu下安装jdk7并配置默认使用的JDK版本(默认openjdk6)
- 7.3 杀死进程
- HDU1116(Play On Words)可做欧拉回路模板
- php中echo、print、print_r区别
- 7.4 桌面小部件:widgit的创建
- nyoj 58最少步数
- android帧布局以及注意事项
- Linux 启动过程分析
- Java基础测试题
- Openstack能走多远——Openstack、VMware浅析
- poj2886Who Gets the Most Candies?(线段树,记活人的数量,题目类似约瑟夫环)