hdu 1116 Play on Words

来源:互联网 发布:校园网络拓扑图 编辑:程序博客网 时间:2024/06/18 14:27

题目链接点击打开链接

Play on Words

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


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 1272 1162 1301 1217 
 


 

我们把单词的首尾两个字母看成是点,那么整个单词可以看做是边,那么这道题也就是一道简单的判断是否为欧拉图的问题。

这道题要用到并查集,首先判断所有的点是否全部连通,然后再对于有向图,可以完成一笔画的有两个情况

1 为欧拉图 ,即所有点的出度等于入度

2 为半欧拉图,即只有两个点出度不等于入度,且一个点出度比入度大一,另一个点入度比出度大一

AC代码:

#include<iostream>#include<string>#include<string.h>using namespace std;int t;int n;char str[1010];bool check[27];bool flag;int father[27];int chudu[27];int rudu[27];int from,to;int find(int x){    if(x!=father[x])        father[x]=find(father[x]);    return father[x];}void join(int x,int y){    int xx=find(x);    int yy=find(y);    if(xx!=yy)        father[xx]=yy;}int main(){    cin>>t;    while(t--)    {        memset(check,false,sizeof(check));        flag=false;;        for(int i=0;i<27;i++)        {            father[i]=i;            chudu[i]=0;            rudu[i]=0;        }        cin>>n;        for(int i=1;i<=n;i++)        {           cin>>str;           int len=strlen(str);           from=str[0]-'a';           to=str[len-1]-'a';           check[from]=true;           check[to]=true;           join(from,to);           chudu[from]++;           rudu[to]++;        }        for(int i=0;i<26;i++)        {            for(int j=i+1;j<26;j++)            {                if(check[i]&&check[j])                {                    if(find(i)!=find(j))                    {                        flag=true;                        break;                    }                }            }            if(flag)             break;        }        if(flag)        {            cout<<"The door cannot be opened."<<endl;            continue;        }        int cnt=0;        bool a=false;        bool b=false;       for(int i=0;i<26;i++)       {           if(check[i])           {               if(rudu[i]!=chudu[i])               {                cnt++;                if(rudu[i]-chudu[i]==1)                    a=true;                if(chudu[i]-rudu[i]==1)                    b=true;               }           }       }       if(cnt==0)        cout<<"Ordering is possible."<<endl;       else if(cnt==2&&a&&b)        cout<<"Ordering is possible."<<endl;        else         cout<<"The door cannot be opened."<<endl;    }    return 0;}



原创粉丝点击