UVA10129-Play on Words(欧拉路径)

Problem UVA10129-Play on Words

Accept: 2534  Submit: 19477

Time Limit: 3000 mSec

UVA10129-Play on Words(欧拉路径) 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.

UVA10129-Play on Words(欧拉路径) 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 N that indicates the number of plates (1 ≤ N ≤ 100000). Then exactly N lines 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.

UVA10129-Play on Words(欧拉路径) 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.’

UVA10129-Play on Words(欧拉路径) Sample Input

3

2

acm

ibm

3 acm

malform

mouse

2

ok

ok

UVA10129-Play on Words(欧拉路径) Sample output

The door cannot be opened.

Ordering is possible.

The door cannot be opened.

题解:欧拉路径模板题,这里面运用并查集判断联通,对于欧拉路径的判断,lrj的代码给了一个很好的模板,简单严密

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <vector>
 6 using namespace std;
 7 
 8 const int maxl = 1000+10;
 9 const int kind = 26;
10 char str[maxl];
11 int n,pre[kind],deg[kind];
12 bool used[kind];
13 
14 int findn(int x){
15     return x == pre[x] ? x : pre[x] = findn(pre[x]);
16 }
17 
18 int main()
19 {
20     //freopen("input.txt","r",stdin);
21     int iCase;
22     scanf("%d",&iCase);
23     while(iCase--){
24         scanf("%d",&n);
25         memset(used,false,sizeof(used));
26         memset(deg,0,sizeof(deg));
27         for(int i = 0;i < 26;i++) pre[i] = i;
28         int cnt = 26;
29         for(int i = 1;i <= n;i++){
30             scanf("%s",str);
31             int x1 = str[0]-'a',x2 = str[strlen(str)-1]-'a';
32             used[x1] = used[x2] = true;
33             deg[x1]++,deg[x2]--;
34             int fx1 = findn(x1),fx2 = findn(x2);
35             if(fx1 != fx2){
36                 pre[fx1] = fx2;
37                 cnt--;
38             }
39         }
40         vector<int> ans;
41         for(int i = 0;i < kind;i++){
42             if(!used[i]) cnt--;
43             else if(deg[i] != 0){
44                 ans.push_back(deg[i]);
45             }
46         }
47         bool ok = false;
48         if(cnt==1 && (ans.empty() || (ans.size()==2) && (ans[0]==1 || ans[0]==-1))) ok = true;
49         if(ok) printf("Ordering is possible.
");
50         else printf("The door cannot be opened.
");
51     }
52     return 0;
53 }