并查集之 Play on Words

Play on Words

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2589    Accepted Submission(s): 830


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
 

 

Recommend
Eddy
 
 
 
 
 
一个有向图是欧拉图当且仅当该图的基图(将所有有向边变为无向边后形成的无向图,
这里同样不考虑度数为0的点)是连通的且所有点的入度等于出度;一个有向图是半欧拉图
当且仅当该图的基图是连通的且有且只有一个点的入度比出度少1(作为欧拉路径的起点),
有且只有一个点的入度比出度多1(作为终点),其余点的入度等于出度
 
wa了两次 原来是欧拉回路那里敲错了  呵呵 得小心啊
View Code
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <string>
 4 using namespace std;
 5 
 6 int p[26],chu[26],ru[26],flag[26];
 7 
 8 void init()
 9 {
10     for (int i=0;i<26;i++)
11     {
12         p[i]=i;
13         chu[i]=0;
14         ru[i]=0;
15         flag[i]=0;
16     }
17 }
18 
19 int find(int x)
20 {
21     if (p[x]!=x)
22     {
23         p[x]=find(p[x]);
24     }
25     return p[x];
26 }
27 
28 void uion(int x,int y)
29 {
30     x=find(x);
31     y=find(y);
32     p[x]=y;    
33 }
34 
35 int main()
36 {
37     string a;
38     int m,n,i,x,y;
39     scanf("%d",&n);
40     while (n--)
41     {
42         init();
43         cin>>m;
44         for (i=0;i<m;i++)
45         {
46             cin>>a;
47             x=a[0]-'a';
48             y=a[a.size()-1]-'a';
49             chu[x]++;
50             ru[y]++;
51             flag[x]=flag[y]=1;
52             uion(x,y);
53         }
54         int star=0,end=0,equal=0;
55         int con=1,f;
56         for (i=0;i<26;i++)
57             if(flag[i])
58             {
59                 f=find(i);
60                 if (ru[i]==chu[i]+1) star++;
61                 if (ru[i]+1==chu[i]) end++;
62                 if (ru[i]!=chu[i]) equal++;
63             }
64             for (i=0;i<26;i++)
65                 if(flag[i])
66                 {
67                     if (f!=find(i))
68                     {    
69                         con=0;
70                     }            
71                 }
72                 if (((star==1&&end==1&&equal==2)||equal==0)&&con==1)
73                 {
74                     printf("Ordering is possible.\n");
75                 }
76                 else printf("The door cannot be opened.\n");
77     }
78     return 0;
79 }

 

原文地址:https://www.cnblogs.com/wujianwei/p/2585737.html