HDU1116 欧拉回路/通路

Play on Words

                                                              Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

                                                                     Total Submission(s): 2775    Accepted Submission(s): 881

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.
思路:并查集&&欧拉通路
1)所有的点联通
2)欧拉回路中所有点的入度和出度一样。
3)欧拉通路中起点的入度 - 出度 = 1,终点的 初度 - 入度 = 1,
其他的所有点入度 = 出度;
 
一开始忘记初始化了,WA了……
View Code
 1 //218ms 508KB
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <iostream>
 5 using namespace std;
 6 //1<=N<=100000 len<1000
 7 #define M 30
 8 int father[M],vis[M],in[M],out[M],ans[M];
 9 char ch[1005];
10 int findx(int x)
11 {
12     if(father[x]!=x)
13      father[x]=findx(father[x]);
14     return father[x];
15 }
16 void merge(int a,int b)
17 {
18     int x,y;
19     x=findx(a);
20     y=findx(b);
21     if(x!=y) father[x]=y;
22 }
23 int main()
24 {
25     int T,n,a,b;
26     scanf("%d",&T);
27     while(T--)
28     {
29         scanf("%d",&n);
30         memset(vis,0,sizeof(vis));
31         memset(in,0,sizeof(in));
32         memset(out,0,sizeof(out));
33         for(int i=0;i<26;i++)
34          father[i]=i;
35 
36         for(int i=0;i<n;i++)
37         {
38             scanf("%s",ch);
39             a=ch[0]-'a';
40             b=ch[strlen(ch)-1]-'a';
41             merge(a,b);
42             out[a]++;
43             in[b]++;
44             vis[a]=1;
45             vis[b]=1;
46         }
47         for(int i=0;i<26;i++)
48           father[i]=findx(i);
49 
50         int cnt=0;
51         for(int i=0;i<26;i++)
52           if(vis[i]&&father[i]==i)
53             cnt++;
54         if(cnt>1)
55         {
56             printf("The door cannot be opened.\n");
57             continue;
58         }
59         int j=0;
60         for(int i=0;i<26;i++)
61           if(vis[i]&&out[i]!=in[i])
62             ans[j++]=i;
63 
64         if(j==0)
65         {
66             printf("Ordering is possible.\n");
67             continue;
68         }
69         if(j==2&&( (out[ans[0]]-in[ans[0]]==1&&in[ans[j-1]]-out[ans[j-1]]==1)
70                  ||(out[ans[j-1]]-in[ans[j-1]]==1&&in[ans[0]]-out[ans[0]]==1) ))
71         {
72             printf("Ordering is possible.\n");
73             continue;
74         }
75         printf("The door cannot be opened.\n");
76     }
77 }

 

转载于:https://www.cnblogs.com/-sunshine/archive/2012/07/25/2608551.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值