hdu1116 Play on Words(典型欧拉回路)

来源:互联网 发布:js怎么给控件赋值 编辑:程序博客网 时间:2024/06/10 03:18
L - Play on Words
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
Submit Status

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

32acmibm3acmmalformmouse2okok

Sample Output

The door cannot be opened.Ordering is possible.The door cannot be opened.
题意:所给n个单词是否连成一串(类似成语接龙)
分析:判断单联通的欧拉回路,使用并差集判断单联通性,然后判欧拉即可
#include<iostream>#include<cstdio>#include<cstring>#include<string>using namespace std;const int MAXN=30;int in[MAXN];int out[MAXN];int done[MAXN];int fa[MAXN];void init(){memset(done,0,sizeof done);memset(in,0,sizeof in);memset(out,0,sizeof out);int i;for(i=0;i<MAXN;i++)fa[i]=i;}int _find(int x){if(x!=fa[x])return fa[x]=_find(fa[x]);return x;}bool solve(){int i,r=0,a=0,b=0,c=0;for(i=0;i<MAXN;i++)if(done[i]&&fa[i]==i)r++;if(r>1)return 0;for(i=0;i<MAXN;i++)if(done[i]&&in[i]!=out[i]){if(in[i]==out[i]+1)a++;else if(in[i]+1==out[i])b++;else c++;}if(c)return 0;if(a==1&&b==1||a==0&&b==0)return 1;return 0;}int main(){char s[1300];int T,n;cin>>T;while(T--){scanf("%d",&n);init();while(n--){scanf("%s",s);int c1=s[0]-'a';int c2=s[strlen(s)-1]-'a';in[c2]++;out[c1]++;int x=_find(c1);int y=_find(c2);done[c1]=done[c2]=1;if(x!=y)fa[x]=y;}if(solve())printf("Ordering is possible.\n");else printf("The door cannot be opened.\n");}return 0;}


0 0
原创粉丝点击