码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1116 Play on Words(欧拉通路)

时间:2015-11-21 22:31:27      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:

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
 

 先将头尾转换为数字。算出各个点的入出度数。标记0-26哪些点有出现。

 判断是否为欧拉通路。1、根<=1 

                    2、有0个入出度数不同  ||  2个入出度数不同且相差1

其余全部不是欧拉通路。

技术分享
 1 #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<math.h>
 7 #include<algorithm>
 8 #include<queue>
 9 #include<set>
10 #include<bitset>
11 #include<map>
12 #include<vector>
13 #include<stdlib.h>
14 #include <stack>
15 using namespace std;
16 #define PI acos(-1.0)
17 #define max(a,b) (a) > (b) ? (a) : (b)
18 #define min(a,b) (a) < (b) ? (a) : (b)
19 #define ll long long
20 #define eps 1e-10
21 #define MOD 1000000007
22 #define N 1006
23 #define inf 1e12
24 int n;
25 char s[N];
26 int a[36];
27 int b[36];
28 int vis[36],fa[36];
29 void init(){
30    for(int i=0;i<33;i++){
31       fa[i]=i;
32    }
33 }
34 int find(int x){
35    return fa[x]==x?x:fa[x]=find(fa[x]);
36 }
37 void merge(int x,int y){
38    int root1=find(x);
39    int root2=find(y);
40    if(root1==root2) return;
41    fa[root1]=root2;
42 }
43 int main()
44 {
45    int t;
46    scanf("%d",&t);
47    while(t--){
48       init();//尼玛又给忘了。。。
49       memset(a,0,sizeof(a));
50       memset(b,0,sizeof(b));
51       memset(vis,0,sizeof(vis));
52       scanf("%d",&n);
53       for(int i=0;i<n;i++){
54          scanf("%s",s);
55          int len=strlen(s);
56          int num1=s[0]-a;
57          int num2=s[len-1]-a;
58          merge(num1,num2);
59          b[num1]++;
60          a[num2]++;
61          vis[num1]=1;
62          vis[num2]=1;
63       }
64       int cnt=0;
65       for(int i=0;i<26;i++){
66          if(find(i)==i && vis[i]){
67             cnt++;
68          }
69       }
70       if(cnt>1){
71          printf("The door cannot be opened.\n");
72          continue;
73       }
74       int tmp=0;
75       int p[30];
76       for(int i=0;i<26;i++){
77          if(a[i]!=b[i] && vis[i]){
78             p[tmp]=i;
79             tmp++;
80          }
81       }
82       if(tmp==0){
83          printf("Ordering is possible.\n");
84          continue;
85       }
86       if(tmp==2 && (a[p[0]]-b[p[0]]==1 && b[p[1]]-a[p[1]]==1 || b[p[0]]-a[p[0]]==1 && a[p[1]]-b[p[1]]==1)){
87             printf("Ordering is possible.\n");
88             continue;
89       }
90       printf("The door cannot be opened.\n");
91    }
92     return 0;
93 }
View Code

 

hdu 1116 Play on Words(欧拉通路)

标签:

原文地址:http://www.cnblogs.com/UniqueColor/p/4984904.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!