POJ 1386 Play on Words

Play on Words

Time Limit: 1000ms
Memory Limit: 10000KB
This problem will be judged on PKU. Original ID: 1386
64-bit integer IO format: %lld      Java class name: Main
 
 
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

 
解题:判断图是否可以欧拉巡游。
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 30;
18 int uf[maxn],in[maxn],out[maxn];
19 int Find(int x){
20     if(x == uf[x]) return uf[x];
21     return uf[x] = Find(uf[x]);
22 }
23 void Union(int x,int y){
24     int tx = Find(x);
25     int ty = Find(y);
26     if(tx != ty) uf[tx] = ty;
27 }
28 int main() {
29     int t,n,cnt,innum,outnum,len;
30     char str[1010];
31     scanf("%d",&t);
32     while(t--){
33         scanf("%d",&n);
34         memset(in,0,sizeof(in));
35         memset(out,0,sizeof(out));
36         for(int i = 0; i < maxn; i++) uf[i] = i;
37         for(int i = 0; i < n; i++){
38             scanf("%s",str);
39             len = strlen(str)-1;
40             out[str[0]-'a']++;
41             in[str[len]-'a']++;
42             Union(str[0]-'a',str[len]-'a');
43         }
44         for(int i = cnt = 0; i < 27; i++)
45             if((in[i]||out[i])&&uf[i] == i) cnt++;
46         if(cnt != 1) {puts("The door cannot be opened.");continue;}
47         bool flag = true;
48         innum = outnum = 0;
49         for(int i = 0; i < maxn; i++){
50             if(in[i] == out[i]) continue;
51             if(in[i] - out[i] == 1) outnum++;
52             else if(out[i] - in[i] == 1) innum++;
53             else {flag = false;break;}
54         }
55         if(flag && (innum == 1 && outnum == 1 || innum == 0 && outnum == 0)) puts("Ordering is possible.");
56         else puts("The door cannot be opened.");
57     }
58     return 0;
59 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/3966088.html