HDU 1116 & poj 1386 play on words (Euler loop)

Source: Internet
Author: User

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 wordacm‘‘ can be followed by the wordMotorola ". 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 shocould 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.

Question: I won't say much about it, that is, the Euler loop;
However, this question is a little different, because if you use a node for this question, you will find that there are at most 26 nodes, which is awesome!
So this question can be as follows:
The determination of Euler's circuit and Euler's path can be summarized as follows:
1) connect all points
2) the inbound and outbound values in the Euler's loop are the same.
3) The inbound-outbound degree of the start point in the Euler's path is 1, the initial degree of the end point is 1, and all other points are inbound;
Undirected graph with Euler Loop Conditions
An undirected graph has an Euler loop. if and only when all vertices in the graph are of an even number.
Directed Graph with Euler's loop Condition
A directed graph has an Euler loop, and the inbound degrees of all vertices are equal to the outbound degrees.

# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; const int maxn = 30; char STR [1005]; int out [maxn], in [maxn], F [maxn]; int vis [maxn], p [maxn]; int find (int x) {If (F [x]! = X) f [x] = find (F [x]); Return f [X];} void merger (int x, int y) {int A = find (x); int B = find (y); if (! = B) f [a] = B;} int main () {int T, I, j; scanf ("% d", & T); While (t --) {int N; scanf ("% d", & N); getchar (); memset (VIS, 0, sizeof (VIS); memset (in, 0, sizeof (in); memset (Out, 0, sizeof (out); for (I = 0; I <26; I ++) {f [I] = I;} while (n --) {scanf ("% s", STR); int Len = strlen (STR ); int u = STR [0]-'A'; int v = STR [len-1]-'A'; merger (u, v); in [v] ++; out [u] ++; vis [u] = vis [v] = 1;} int CNT = 0; for (I = 0; I <26; I ++) f [I] = find (I); for (I = 0; I <26; I ++) {If (vis [I] & F [I] = I) {CNT ++ ;}} if (CNT> 1) // graph connectivity {printf ("The door cannot be opened. \ n "); // printf (" hehe \ n "); continue;} CNT = 0; for (I = 0; I <26; I ++) if (vis [I] & out [I]! = In [I]) {P [CNT ++] = I;} If (CNT = 0) {printf ("ordering is possible. \ n "); continue;} If (CNT = 2) {If (out [p [0]-in [p [0] = 1 & in [p [1]-out [p [1] = 1 | in [p [0]-out [p [0] = 1 & out [p [1]-in [p [1] = 1) {printf ("ordering is possible. \ n "); continue;} printf (" The door cannot be opened. \ n ") ;}return 0 ;}

HDU 1116 & poj 1386 play on words (Euler loop)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.