HDU 1116 Play on Words (Oraton Road)

Source: Internet
Author: User
Tags in degrees

Test instructions: Give out n words, ask to determine whether these words can be solitaire, rules like idiom solitaire, is the current word's tail letter is the first letter of the next word.

Idea: Look like Euler's path-related topics, but the point is how to abstract out.

Just began to think for a long while did not want to understand, the old thought of a word as a vertex to go, is not turned over.

After reading the problem Solving report (http://blog.csdn.net/niushuai666/article/details/6917777), I wanted to come over.

The word's head and tail letters should be regarded as vertices, and the word corresponds to the edge of the connected vertex. Then, if you think about it, the figure will have at most 26 vertices (because there are only 26 lowercase letters, the topic requires a lowercase letter), then how to determine if there is a Euler pathway on the way (that is, to determine whether it is a half Eulerian graph or Eulerian graph).

For information on Eulerian graph, see:

#include <iostream>#include<stdio.h>#include<string.h>using namespacestd;#defineMAXN 26//up to 26 pointsintFA[MAXN];BOOLEXIST[MAXN];//whether the mark Point existsint inch[MAXN], out[MAXN];//in degrees, out ofintDIFF[MAXN];//the point at which the degree of entry is not equal to the degreeintSet_find (intd) {    if(fa[d]<0)returnD; returnfa[d]=Set_find (Fa[d]);}voidSet_join (intXinty) {x=set_find (x); Y=Set_find (y); if(x!=y) fa[x]=y;}intMain () {intT,n,i; Charword[1024x768];//Word    intb;//first letter, tail letter    intRoot//number of root nodes    intM//The number of points that are not equal to the out degreescanf"%d",&t);  while(t--) {memset (FA,-1,sizeof(FA)); memset (exist,0,sizeof(exist)); memset (inch,0,sizeof(inch)); memset ( out,0,sizeof( out)); Root=0; M=0; scanf ("%d",&N);  while(n--) {scanf ("%s", Word); A=word[0]-'a'; b=word[strlen (Word)-1]-'a'; Exist[a]=exist[b]=true; ++ out[A]; ++inch[b]; Set_join (b,a);//b Connect to a.        }         for(i=0; i<maxn;++i)if(exist[i]&&fa[i]<0)++Root; if(root>1) printf ("The door cannot be opened.\n");//Diagram not connected        Else{             for(i=0; i<maxn;++i)if(inch[i]!= out[i]) diff[m++]=i; if(m==0) printf ("Ordering is possible.\n");//all points in degrees are equal to the degrees of Eulerian graph            Else if(m==2&&(                        (inch[diff[0]]- out[diff[0]]==1&& out[diff[1]]-inch[diff[1]]==1)||                        ( out[diff[0]]-inch[diff[0]]==1&&inch[diff[1]]- out[diff[1]]==1)) printf ("Ordering is possible.\n");//satisfies the semi-Eulerian graph condition and is half Eulerian graph            Elseprintf"The door cannot be opened.\n");//not Eulerian graph, not half-Eulerian graph.        }    }    return 0;}
View Code

HDU 1116 Play on Words (Oraton Road)

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.