Play on Words
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3743 Accepted Submission(s): 1198
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.
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.这题是判断图是否为欧拉回路,满足以下任一条件即为欧拉回路:1)所有的点联通(用并查集判断); 2)欧拉回路中所有点的入度和出度一样; 3)欧拉通路中起点的入度 - 出度 = 1,终点的 初度 - 入度 = 1, 且其他的所有点入度 = 出度。AC代码:#include <iostream> #include <cstring> #include <string> #include <cstdio> #include <algorithm> #include <queue> #include <map> #include <cmath> #include <vector> #include <cstdlib> using namespace std; int in[30],out[30],ans[30]; char s[1005]; int father[30]; bool vis[30]; int Find_set(int n) { if(n!=father[n]) father[n]=Find_set(father[n]); return father[n]; } void Union(int a,int b) { int ra=Find_set(a); int rb=Find_set(b); if(ra==rb) return; father[rb]=ra; } void init() { memset(vis,false,sizeof(vis)); memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); for(int i=0;i<30;i++) father[i]=i; } int main() { int t,n; cin>>t; while(t--) { cin>>n; init(); for(int i=0;i<n;i++) { scanf("%s",s); int a=s[0]-'a'; out[a]++; int b=s[strlen(s)-1]-'a'; in[b]++; vis[a]=true; vis[b]=true; Union(a,b); } int cnt=0; for(int i=0;i<26;i++) father[i]=Find_set(i); for(int i=0;i<26;i++) //用并查集判断是否所有点联通 if(vis[i]&&father[i]==i) cnt++; if(cnt>1) { printf("The door cannot be opened.\n"); continue; } else { int k=0; for(int i=0;i<26;i++) if(vis[i]&&in[i]!=out[i]) { ans[k++]=i; } if(k==0) //所有点的入度等于出度 { printf("Ordering is possible.\n"); continue; } //起点的入度 - 出度 = 1,终点的 初度 - 入度 = 1, 且其他的所有点入度 = 出度 if(k==2&&((out[ans[0]]-in[ans[0]]==1&&in[ans[1]]-out[ans[1]]==1)||(out[ans[1]]-in[ans[1]]==1&&in[ans[0]]-out[ans[0]]==1))) { printf("Ordering is possible.\n"); continue; } else printf("The door cannot be opened.\n"); } } return 0; }