HDOJ---1116 Play on Words[並查集+欧拉回路]

17725 ワード

Play on Words
Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2938    Accepted Submission(s): 935
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.
 
 
Source
Central Europe 1999
 
 
Recommend
Eddy
 
 
 
 
 
 
 
題意:acm->malform->mouse(a->m->m->m->m->m->m->m->e)のように、n個の単語の先頭と末尾をつなぎ合わせて単語チェーンを構成する
   パラレルコレクション+オラロード実装
      1.集を調べて連通を判定することは言うまでもない
      2.欧拉路、図の中から分かるように2つの端点を除いて、残りのアルファベットの入度と出度はすべて等しくて、2つの端点の出度と入度は1つ異なって、もう1つの可能性は、全体の図は1つの欧拉回路で、この時各端点の入度と出度はすべて等しいです
 
 
 
 
 
 
code:
  1 /*

  2     :            。

  3     :            。

  4     :       。

  5     :        。

  6 

  7 

  8         

  9               :       。

 10               :        2。

 11 

 12 

 13         

 14               :               。

 15               :                1,          1,          。

 16 

 17 

 18           

 19 */

 20 #include<iostream>

 21 using namespace std;

 22 

 23 #define MAXN 27

 24 

 25 int father[MAXN];

 26 int in[MAXN],out[MAXN];   //       

 27 int flag[MAXN];          //      

 28 int data[MAXN];           //    

 29 int n;

 30 

 31 void init()

 32 {

 33     for(int i=0;i<26;i++)

 34         father[i]=i;

 35     memset(in,0,sizeof(in));

 36     memset(out,0,sizeof(out));

 37     memset(flag,0,sizeof(flag));

 38 }

 39 

 40 int findset(int v)

 41 {

 42     while(v!=father[v])

 43         v=father[v];

 44     return v;

 45 }

 46 

 47 void Union(int a,int b)

 48 {

 49     int x,y;

 50     x=findset(a);

 51     y=findset(b);

 52     if(x!=y)

 53         father[x]=y;

 54 }

 55 

 56 int main()

 57 {

 58     int t;

 59     scanf("%d",&t);

 60     while(t--)

 61     {

 62         init();

 63         scanf("%d",&n);

 64         for(int i=0;i<n;i++)

 65         {

 66             char a,b;

 67             char str[1001];

 68             scanf("%s",str);

 69             a=str[0]-'a';

 70             b=str[strlen(str)-1]-'a';

 71             Union(a,b);

 72             in[b]++;

 73             out[a]++;

 74             flag[a]=flag[b]=1;

 75         }

 76         int cnt=0;

 77         for(int i=0;i<26;i++)

 78         {

 79             father[i]=findset(i);

 80             if(father[i]==i&&flag[i])

 81                 cnt++;                  //        

 82         }

 83         if(cnt>1)        //      

 84         {

 85             printf("The door cannot be opened.
"); 86 continue; 87 } 88 int k=0; 89 for(int i=0;i<26;i++) 90 { 91 if(flag[i]&&in[i]!=out[i]) 92 data[k++]=i; 93 } 94 if(k==0) 95 { 96 printf("Ordering is possible.
"); 97 continue; 98 } 99 int temp1=data[0]; 100 int temp2=data[1]; 101 if(k==2&&((in[temp1]-out[temp1]==1&&out[temp2]-in[temp2]==1)||(in[temp2]-out[temp2]==1&&out[temp1]-in[temp1]==1))) 102 { 103 printf("Ordering is possible.
"); 104 continue; 105 } 106 printf("The door cannot be opened.
"); 107 } 108 return 0; 109 }