HDU1116 Play on Words [European Union] + [query set], hdu1116 European Union
Play on WordsTime Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 5736 Accepted Submission (s): 1897
Problem DescriptionSome 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.
InputThe 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.
OutputYour 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 shocould print the sentence "Ordering is possible.". Otherwise, output the sentence "The door cannot be opened .".
Sample Input
32acmibm3acmmalformmouse2okok
Sample Output
The door cannot be opened.Ordering is possible.The door cannot be opened.
SourceCentral Europe 1999 classic question.
Meaning: N strings. When the end word of a string is the same as the first word of another string, the two strings can be connected at the beginning and end. Ask if there is a scheme to concatenate these strings into a sequence. Question: During the competition, I went to SPFA to relax the longest path and thought about it. As a result, I had a long struggle in the composition. Instead, I forgot my previous ouyi, Axi, fortunately, I have reviewed this question and checked it and the European Union. Undirected Europe: When a graph is connected, the degrees of all vertices are even or the degrees of only two vertices are odd. Directed to Europe: in the case of graph Unicom, the inbound degrees of all vertices are equal to the outbound degrees, or only one vertex is greater than the outbound degree. At the same time, only one vertex is less than the outbound degree, the point where the absolute value of the inbound and outbound degrees is greater than 1 is not allowed.
# Include <stdio. h> # include <string. h> # define maxn 28int pre [maxn], vis [maxn], in [maxn], out [maxn]; char str [1002]; int abs (int k) {return k <0? -K: k;} int ufind (int k) {int a = k, B; while (pre [k]! =-1) k = pre [k]; while (! = K) {B = pre [a]; pre [a] = k; a = B;} return k;} bool unite (int a, int B) {a = ufind (a); B = ufind (B); if (a = B) return false; else {pre [a] = B; return true ;}} int main () {int T, N, I, j, flag1, flag2, cnt, a, B; scanf ("% d", & T); while (T --) {scanf ("% d", & N); memset (pre,-1, sizeof (pre); memset (vis, 0, sizeof (vis )); memset (in, 0, sizeof (in); memset (out, 0, sizeof (out); cnt = flag1 = flag2 = 0; while (N --) {scanf ("% S", str); a = * str-'A'; B = str [strlen (str)-1]-'A'; if (! Vis [a]) {vis [a] = 1; ++ cnt;} if (! Vis [B]) {vis [B] = 1; ++ cnt ;}++ out [a]; ++ in [B]; if (unite (, b) -- cnt;} if (cnt! = 1) {// forest printf ("The door cannot be opened. \ n ") ;}else {for (I = 0; I <26; ++ I) {if (in [I] + 1 = out [I]) ++ flag1; else if (out [I] + 1 = in [I]) ++ flag2; else if (abs (in [I]-out [I])> 1) break;} if (I <26) printf ("The door cannot be opened. \ n "); else if (! Flag1 &&! Flag2 | flag1 = 1 & flag2 = 1) printf ("Ordering is possible. \ n") ;}} return 0 ;}