HDOJ 题目1116 Play on Words(欧拉图,并查集)

来源:互联网 发布:什么是多维数据分析 编辑:程序博客网 时间:2024/06/06 02:29

Play on Words

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5618    Accepted Submission(s): 1849


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
32acmibm3acmmalformmouse2okok
 

Sample Output
The door cannot be opened.Ordering is possible.The door cannot be opened.
 

Source
Central Europe 1999
 

Recommend
Eddy   |   We have carefully selected several similar problems for you:  1142 1281 1053 1054 1595 
思路:http://blog.csdn.net/niushuai666/article/details/6917777

所以这道题的大题思路就是:

1.并查集判断连通

2.将每个单词取出首字母和尾字母,转换为一条边,然后加入对应的连通分量中。如果这个字母出现过,visit数组标记为true。同时起点出度加1,终点入度加1.

3.判断一下:

1)这个图必须是连通的,即根结点只有一个。如果不是,直接结束本次算法。

2)如果这个图是连通的,判断每个结点的入度和出度情况。

        如果这个图是欧拉路,则每个顶点的出度等于入度。即out[i] = in[i]

如果这个图是半欧拉图,则起点的出度比入度大1,终点的入度比出度大1.其余顶点的出度等于入度。

如果满足上述条件,就可以将所有单词链接起来,否则不能。

当然,在判断出度入度的时候还有一点需要注意,那就是除了起点终点以外的顶点,出度必须等于入度(出度入度可以同时为2,即环),但是起点和终点必须保证出度和入度之差为1。

ac代码
#include<stdio.h>#include<string.h>int pre[101000],v[100010],out[100010],in[100010];int find(int x){if(x==pre[x])return x;return pre[x]=find(pre[x]);}void join(int a,int b){int fa,fb;fa=find(a);fb=find(b);if(fa!=fb){pre[fb]=fa;}}int main(){int t;scanf("%d",&t);while(t--){char s[100010];int len,i,j,k,st,to,w=1,n,sum=0,s1=0,s2=0;memset(out,0,sizeof(out));memset(in,0,sizeof(in));memset(v,0,sizeof(v));scanf("%d",&n);for(i=0;i<=30;i++)pre[i]=i;for(i=0;i<n;i++){scanf("%s",s);len=strlen(s);st=s[0]-'a'+1;to=s[len-1]-'a'+1;out[st]++;in[to]++;v[st]=v[to]=1;join(st,to);}for(i=1;i<=30;i++){if(v[i]){if(pre[i]==i)sum++;if(out[i]!=in[i]){if(out[i]-in[i]==1)s1++;else if(in[i]-out[i]==1)s2++;else{w=0;break;}}}if(sum>1){w=0;}if(!w)break;}if((s1==0&&s2==0&&w)||(s1==1&&s2==1&&w)){printf("Ordering is possible.\n");}elseprintf("The door cannot be opened.\n");}}


 
0 0
原创粉丝点击