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

UVA10129-Play on Words(欧拉路径)

时间:2018-08-21 19:02:31      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:bec   for   amp   lin   important   cstring   turn   script   alt   

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(欧拉路径)

标签:bec   for   amp   lin   important   cstring   turn   script   alt   

原文地址:https://www.cnblogs.com/npugen/p/9513163.html

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