1. 程式人生 > >UVA10129-Play on Words(歐拉路徑)

UVA10129-Play on Words(歐拉路徑)

歐拉 a10 技術分享 分享圖片 cst out include eth space

Problem UVA10129-Play on Words

Accept: 2534 Submit: 19477

Time Limit: 3000 mSec

技術分享圖片 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 ?rst line of the input ?le. 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.

技術分享圖片 Output

Your program has to determine whether it is possible to arrange all the plates in a sequence such that the ?rst 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.

題解:歐拉路徑模板題,這裏面運用並查集判斷聯通,對於歐拉路徑的判斷,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.\n");
50         else printf("The door cannot be opened.\n");
51     }
52     return 0;
53 }

UVA10129-Play on Words(歐拉路徑)