Play on Words
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3730 Accepted Submission(s): 1192
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
欧拉通路: 通过图中每条边且只通过一次,并且经过每一顶点的通路。
欧拉回路: 通过图中每条边且只通过一次,并且经过每一顶点的回路。
无向图是否具有欧拉通路或回路的判定:
欧拉通路:图连通;图中只有0个或2个度为奇数的节点
欧拉回路:图连通;图中所有节点度均为偶数
有向图是否具有欧拉通路或回路的判定:
欧拉通路:图连通;除2个端点外其余节点入度=出度;1个端点入度比出度大1;一个端点入度比出度小1 或 所有节点入度等于出度
欧拉回路:图连通;所有节点入度等于出度
Sample Output
The door cannot be opened. Ordering is possible. The door cannot be opened.记录学习/* HDOJ 1116 考察欧拉回路和欧拉通路的定义: 首先要存在欧拉通路和欧拉回路,那么这个图必须是连通图。 有向图的欧拉通路:除了有两个顶点,一个入度比出度大一,一个出度比入度大一,其他所有顶点都是出度等于入度 有向图的欧拉回路:所有顶点都是出度等于入度 由于这道题目是抽象为有向图,所以就只需要考虑有向图的情况就OK咯。 用并查集判断是否是连通图。 所以这道题目可以说是并查集和图论的结合。 */ #include <iostream> #include <string> using namespace std; #pragma warning(disable : 4996) #define MAX 27 int In[MAX]; int Out[MAX]; int father[MAX]; int flag[MAX]; int Find(int x) { if (x != father[x]) { father[x] = Find(father[x]); } return father[x]; } void Union(int x, int y) { x = Find(x); y = Find(y); if(x != y) { father[x] = y; } } int main() { freopen("in.txt", "r", stdin); int nCase, n, a, b, i, f, d1, d2, d3; char str[1002]; cin >> nCase; while(nCase--) { for(i = 0; i < MAX; i++) { father[i] = i; In[i] = 0; Out[i] = 0; flag[i] = 0; } cin >> n; for(i = 0; i < n; i++) { cin >> str; a = str[0] - 'a'; b = str[strlen(str) - 1] - 'a'; Union(a, b); Out[a]++; In[b]++; flag[a] = flag[b] = 1; } //先判断是不是连通图 f = 0; for(i = 0;i < 26; i++) { if(flag[i] && father[i] == i) { f++; } } if(f > 1) //非连通 { cout<<"The door cannot be opened."<<endl; continue; } d1 = 0; d2 = 0; d3 = 0; //下面判断是否存在欧拉通(回)路 for(i = 0; i < 26; i++) { if(flag[i] && In[i] != Out[i]) { d1++; if(In[i] - Out[i] == 1) { d2++; //统计入度比出度大一的结点数 } else if(Out[i] - In[i] == 1) { d3++; //统计出度比入度大一的结点数 } } } if(d1 == 0) //存在欧拉回路 { cout<<"Ordering is possible."<<endl; } else if((d1 == 2)&&(d2 == 1)&&(d3 == 1)) //存在欧拉通路 { cout<<"Ordering is possible."<<endl; } else { cout<<"The door cannot be opened."<<endl; } } return 0; }