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

POJ1386 Play on Words

时间:2016-07-12 23:21:28      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:

 

Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u

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

 

欧拉道路问题。这题欧拉回路或者欧拉道路都算成立。

建好图以后先判一下连通性,如果图不连通,肯定不成立。

↑DFS BFS 并查集都可以

然后是判欧拉路径。

  如果是欧拉回路,那么所有点的入度等于出度。

  如果是欧拉路径,那么只有两个点的入度不等于出度,其一是起始点,出度比入度大1,另一个是结束点,入度比出度大1

 

之前一直WA,不知怎么改着改着就对了。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 using namespace std;
 7 const int mxn=1000;
 8 bool vis[mxn];
 9 int n;
10 int fa[mxn];
11 int in[mxn],out[mxn];
12 void init(int n){
13     for(int i=1;i<=n;i++)fa[i]=i;
14 }
15 int find(int x){
16     if(fa[x]==x)return x;
17     else return fa[x]=find(fa[x]);
18 }
19 char s[1100];
20 int main(){
21     int T;
22     scanf("%d",&T);
23     while(T--){
24         memset(vis,0,sizeof vis);
25         memset(in,0,sizeof in);
26         memset(out,0,sizeof out);
27         scanf("%d",&n);
28         init(28);
29         int i,j;
30         for(i=1;i<=n;i++){
31             scanf("%s",s);
32             int u=s[0]-a+1;
33             int v=s[strlen(s)-1]-a+1;
34             in[v]++;out[u]++;
35             vis[u]=vis[v]=1;
36             int x=find(u),y=find(v);
37             if(x!=y)fa[y]=x;
38         }
39         int st=0,ed=0;bool flag=0;
40         int cnt=0;
41         for(i=1;i<=26;i++){
42             if(vis[i] && find(i)==i){
43                 cnt++;
44             }
45             if(out[i]!=in[i]){//出度不等于入度时判定 
46                 if(out[i]==in[i]+1){
47                     if(!st)st=i;
48                     else flag=1;
49                 }
50                 else if(in[i]==out[i]+1){
51                     if(!ed)ed=i;
52                     else flag=1;
53                 }
54                 else flag=1;
55             }
56         }
57         if(cnt!=1){//非连通特判 
58             printf("The door cannot be opened.\n");
59             continue;
60             }
61         if(flag==1)printf("The door cannot be opened.\n");
62         else printf("Ordering is possible.\n");
63     }
64     return 0;
65 }

 

POJ1386 Play on Words

标签:

原文地址:http://www.cnblogs.com/SilverNebula/p/5665135.html

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