Play on Words
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7807 Accepted Submission(s): 2681
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.
题意:给出一系列字符串,判断是不是可以完成首尾相接(条件是后一项的首字母等于前一项的尾字母),所有字符串都要包含。
并查集:http://blog.csdn.net/dellaserss/article/details/7724401/ 推荐,理解起来非常容易。
简要说明就是给出的图,每一个联通块看作一个帮派,每个帮派又一个老大。帮派之间的不会打架。当两个陌生人遇到一起时,问一下他们的老大是不是一样,若是一样,就是一个帮派,也就是一个连通块的。若不是,则吧两个帮派合并成一个,两个老大随便选一个认另一个当老大。这样就是一个帮派的了,也就是一个连通图的。每当连接两个联通快成一个的时候,需要一条边。假设n个人,联通起来最多需要n-1个边,若有k个帮派,则只需n-1-k个边即可;
欧拉回路:1.有向图:每个点的出度==入度或者只有两个点(起点和终点)abs(入度-出度==1)
2.无向图:每个点的度为偶数
Code Render Status :
Rendered By HDOJ C++ Code Render Version 0.01 Beta
#include<iostream> #include<cstring> #include<stack> #include<algorithm> #include<cmath> #define INF 0x3f3f3f3f using namespace std; int flag[30],father[30],in[30],out[30],p[30]; int find(int x) { int r = x; while(father[r] != r) r = father[r]; int i = x,j; while(i != r) { j = father[i]; father[i] = r; i = j; } return r; } void join(int x,int y) { int fx = find(x),fy = find(y); if(fx != fy) father[fx] = fy; } int main() { int t,ans; cin >> t; while(t--) { memset(flag,0,sizeof(flag)); memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); for(int i = 0;i <= 26;i++) father[i] = i; int n; cin >> n; char s[1010]; for(int i = 0;i < n;i++) { cin >> s; int a = s[0] - 'a'; int b = s[strlen(s)-1] - 'a'; join(a,b); in[a]++; out[b]++; flag[a] = flag[b] = 1; } ans = 0; for(int i = 0;i < 26;i++) { father[i] = find(i); if(flag[i]&&father[i] == i) ans++; } if(ans > 1) { cout<<"The door cannot be opened."<<endl; continue; } ans = 0; for(int i = 0;i < 26;i++) { if(flag[i] && in[i] != out[i]) { p[ans++] = i; } } if(ans == 0) { cout<<"Ordering is possible."<<endl; continue; } if(ans == 2&&((out[p[0]]-in[p[0]] == 1&&in[p[1]]-out[p[1]]==1) ||(out[p[1]]-in[p[1]]==1 && in[p[0]]-out[p[0]]==1))) { cout<<"Ordering is possible."<<endl; continue; } cout<<"The door cannot be opened."<<endl; } return 0; }