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.".
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、用并查集判连通性,根节点如果不是一个那么不是。
2、判断入度以及出度。欧拉回路的充要条件是每个结点入度等于出度,欧拉路的条件则为除了首结点出度比入度大一,尾结点入度比出度大一,其余结点入度等于出度。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #define maxlen 100010 6 int father[maxlen]; 7 int visited[maxlen]; 8 int indegree[maxlen]; 9 int outdegree[maxlen]; 10 int maps[30][30]; 11 using namespace std; 12 void init() 13 { 14 for(int i=0; i<=maxlen; ++i) 15 { 16 visited[i]=indegree[i]=outdegree[i]=0; 17 father[i]=i; 18 } 19 } 20 int Find(int x) 21 { 22 return father[x]==x?x:father[x]=Find(father[x]); 23 } 24 void Union(int x,int y) 25 { 26 x=Find(x); 27 y=Find(y); 28 if(x!=y) 29 father[x]=y; 30 } 31 int main() 32 { 33 int t,n; 34 char str[1010]; 35 int s,e; 36 scanf("%d",&t); 37 while(t--) 38 { 39 scanf("%d",&n); 40 init(); 41 for(int i=1; i<=n; ++i) 42 { 43 scanf("%s",str); 44 int len=strlen(str); 45 s=str[0]-'a'+1; 46 e=str[len-1]-'a'+1; 47 visited[s]=visited[e]=1; 48 outdegree[s]++; 49 indegree[e]++; 50 Union(s,e); 51 } 52 int root=0; 53 int innum=0,outnum=0; 54 int flag=1; 55 for(int i=1; i<maxlen; ++i) 56 { 57 if(visited[i]) 58 { 59 if(father[i]==i) 60 root++; 61 if(root>1) 62 { 63 flag=0; 64 break; 65 } 66 if(indegree[i]!=outdegree[i]) 67 { 68 if(indegree[i]-outdegree[i]==1) 69 innum++; 70 else if(outdegree[i]-indegree[i]==1) 71 outnum++; 72 else 73 { 74 flag=0; 75 break; 76 } 77 } 78 } 79 } 80 if(flag&&((innum==0&&outnum==0)||(innum==1&&outnum==1))) 81 printf("Ordering is possible. "); 82 else 83 printf("The door cannot be opened. "); 84 } 85 }