Hdu 1116 Play on Words
Posted 寂地沉
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hdu 1116 Play on Words相关的知识,希望对你有一定的参考价值。
Play on Words
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8407 Accepted Submission(s): 2865
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> #include<stdlib.h> #define MAXN 100001 #define INF 100000000int T; int N; char word[1001]; int od[26],id[26]; int bused[26]; int parent[26]; struct edge { int u,v; }edges[MAXN]; int i,j; void UFset() { for(i=0;i<26;i++) parent[i]=-1; } int find(int x) { int s; for(s=x;parent[s]>=0;s=parent[s]); while(s!=x) { int tmp=parent[x]; parent[x]=s; x=tmp; } return s; } void Union(int R1,int R2) { int r1=find(R1),r2=find(R2); int tmp=parent[r1]+parent[r2]; if(parent[r1]>parent[r2]) { parent[r1]=r2; parent[r2]=tmp; } else { parent[r2]=r1; parent[r1]=tmp; } } bool bconnect() { int u,v,i; UFset(); for(i=0;i<N;i++) { u=edges[i].u;v=edges[i].v; if(u!=v&&find(u)!=find(v)) Union(u,v); } int first=-1; for(i=0;i<26;i++) { if(bused[i]==0)continue; if(first==-1)first=i; else if(find(i)!=find(first))break; } if(i<26)return false; else return true; } int main() { int u,v; int i,j; scanf("%d",&T); while(T--) { memset(od,0,sizeof(od)); memset(id,0,sizeof(id)); memset(bused,0,sizeof(bused)); scanf("%d",&N); for(j=0;j<N;j++) { scanf("%s",word); u=word[0]-‘a‘;v=word[strlen(word)-1]-‘a‘; od[u]++;id[v]++; bused[u]=bused[v]=1; edges[j].u=u;edges[j].v=v; } bool Euler=true; int one =0;//出度比入度多一的顶点个数 int none=0;//入度比出度多一的顶点的个数 for(j=0;j<26;j++) { if(bused[j]==0)continue; if(od[j]-id[j]>=2||id[j]-od[j]>=2) { Euler=false ;break; } if(od[j]==0&&id[j]==0) { Euler=false;break; } if(od[j]-id[j]==1) { one++; if(one>1) { Euler=false ;break; } } if(id[j]-od[j]==1) { none++; if(none>1) { Euler=false ;break; } } } if(one!=none) Euler=false; if(!bconnect())Euler =false; if(Euler)printf("Ordering is possible.\n"); else printf("The door cannot be opened.\n"); } return 0; }
以上是关于Hdu 1116 Play on Words的主要内容,如果未能解决你的问题,请参考以下文章
hdu1116 Play on Words--并查集&网络流
Play on Words HDU - 1116(欧拉路判断 + 并查集)