poj 1386 Play on Words(欧拉路径存在的判断问题)

Play on Words
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 8092   Accepted: 2848

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.


此题只需要判断有向图的欧拉路径是否存在,存在欧拉路径的条件有:

1、图连通,有一个顶点出度大入度1,有一个顶点入度大出度1,其余都是出度=入度;

2、图连通,所有的顶点出度=入度

以上,1是对欧拉路径的定义,2是对欧拉回路的定义,当然只要满足其中一条就可以了

解题步骤:

1、用并查集判断图是不是连通图

2、判断个点出入度的关系

此题为poj 2337的简易版,只需判断不用求路径

代码:

 

  1 #include<algorithm>
  2 #include<iostream>
  3 #include<stdio.h>
  4 #include<string.h>
  5 #include<math.h>
  6 #include<queue>
  7 #include<stack>
  8 using namespace std;
  9 
 10 char str[1000];
 11 
 12 int parent[30];
 13 //下面数组分别为出度,入度,存在标记,
 14 int in[30],out[30],existmark[30];
 15 
 16 //并查集1
 17 int findp(int u)
 18 {
 19     while(u!=parent[u])
 20     {
 21         u=parent[u];
 22     }
 23     return u;
 24 }
 25 
 26 //并查集2
 27 void unit(int u,int v)
 28 {
 29     int r1=findp(u);
 30     int r2=findp(v);
 31     if(r1==r2)
 32         return ;
 33     parent[r1]=r2;
 34 }
 35 
 36 //初始化
 37 void init()
 38 {
 39     int i;
 40     for(i=0;i<26;i++)
 41         parent[i]=i;
 42     memset(in,0,sizeof(in));
 43     memset(out,0,sizeof(out));
 44     memset(existmark,0,sizeof(existmark));
 45 }
 46 
 47 int main()
 48 {
 49     int t;
 50     scanf("%d",&t);
 51     while(t--)
 52     {
 53         int n;
 54         scanf("%d",&n);
 55         init();
 56         int i,v,u;
 57         for(i=0;i<n;i++)
 58         {    
 59             scanf("%s",str);
 60             u=str[0]-'a';
 61             v=str[strlen(str)-1]-'a';
 62             out[u]++;
 63             in[v]++;
 64             existmark[u]=1;
 65             existmark[v]=1;
 66             unit(u,v);
 67         }
 68         //并查集结果判断是不是连通
 69         int flag=0;
 70         for(i=0;i<26;i++)
 71         {
 72             if(existmark[i]&&parent[i]==i)
 73                 flag++;
 74         }
 75         if(flag>1)
 76         {
 77             printf("The door cannot be opened.\n");
 78             continue;
 79         }
 80         //判断出度入度差的性质判断是否符合
 81         int a=0,b=0,start=-1;
 82         for(i=0;i<26;i++)
 83         {
 84             if(in[i]!=out[i]&&existmark[i])
 85             {
 86                 if(1==in[i]-out[i])
 87                 {
 88                     a++;
 89                 }else if(1==out[i]-in[i])
 90                 {
 91                     b++;
 92                     start=i;
 93                 }else
 94                 {
 95                     break;
 96                 }
 97             }
 98         }
 99         if(i<26)
100         {
101             printf("The door cannot be opened.\n");
102             continue;
103         }else
104         {
105             if(!((0 == a + b) || (1 == a && 1 == b)))
106             {
107                 printf("The door cannot be opened.\n");
108                 continue;
109             }
110             printf("Ordering is possible.\n");
111         }
112 
113     }
114     return 0;
115 }

 

 

 

posted @ 2013-06-13 21:29  crazy_apple  阅读(294)  评论(0编辑  收藏  举报