HDU 1116 (and check set + Euler path)

Source: Internet
Author: User

Play on Words

Time limit:10000/5000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 7080 Accepted Submission (s): 2398


Problem Descriptionsome of the secret doors contain a very interesting word puzzle. The team of archaeologists have to solve it to open that doors. Because There is no other-a-to-open the doors, the puzzle is very important for us.

There is a large number of magnetic plates on every door. Every plate have one word written on it. The plates must be arranged to a sequence in such a-to-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 would read the list of words and determine whether it's 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 in 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 of the word contains at least-lowercase characters, which means only letters ' a ' through ' Z ' would appear In the word. The same word may appear several times in the list.

Outputyour program have to determine whether it's possible to arrange all the plates in a sequence such that's the first let ter of each word was equal to the last letter of the previous word. The plates from the list must is used, each exactly once. The words mentioned several times must is 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."

Sample Input32acmibm3acmmalformmouse2okok

Sample outputthe door cannot be opened. Ordering is possible. The door cannot be opened. Test instructions: Determine whether the given word can be connected to a string. 1. Determine the connected component 2. Determine Euler path in detail: http://blog.csdn.net/niushuai666/article/details/6917777
#include <stdio.h>#include<iostream>#include<string.h>#include<algorithm>#include<math.h>using namespacestd;Const intN = -;BOOLVis[n];///determine if the current letter has occurredintFather[n];intIndegree[n],outdegree[n];///in and out ofint_find (intx) {    if(X==father[x])returnx; return_find (father[x]);}intUnion (intAintb) {    intx =_find (a); inty =_find (b); if(x==y)return 0; FATHER[X]=y; return 1;}voidinit () {memset (Vis,false,sizeof(VIS)); memset (Indegree,0,sizeof(Indegree)); memset (Outdegree,0,sizeof(Outdegree));  for(intI=0; i<n;i++) father[i]=i;}intMain () {inttcase; scanf ("%d",&tcase);  while(tcase--){        intN; Charstr[1005]; scanf ("%d",&N);        Init ();  while(n--) {scanf ("%s", str); intLen =strlen (str); ints = str[0]-'a'; intE = str[len-1]-'a';            Union (s,e); Vis[s]= Vis[e] =true; Indegree[e]++; Outdegree[s]++; }        intAns =0; BOOLflag=false, flag1=false; int inch=0, out=0;  for(intI=0; i<n;i++){            if(Vis[i]) {if(father[i]==i) ans++; if(indegree[i]!=Outdegree[i]) {                    if(indegree[i]-outdegree[i]==1)inch++; Else if(outdegree[i]-indegree[i]==1) out++; ElseFlag1 =true; }            }            if(ans>1) {flag=true; }        }        if(!flag&&!flag1&&inch==0&& out==0) printf ("Ordering is possible.\n"); Else if(!flag&&!flag1&&inch==1&& out==1) printf ("Ordering is possible.\n"); Elseprintf"The door cannot be opened.\n"); }}

HDU 1116 (and check set + Euler path)

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.