UVA 10129 Play on Words (Oraton Road)

Source: Internet
Author: User

Test instructions

 Enter n (n <= 100000) words, whether all these words can be in a sequence, so that the first letter of each word is the same as the last letter of the previous word (for example: Acm,malform,mouse). Each word contains a maximum of 1000 lowercase letters. You can have duplicate words in the input.

Ideas:

 The two ends of a letter are opened to a node, the word as a forward edge, if the problem is borrowed, when and only if there is Oraton road in the diagram. All that is needed to determine whether the graph constructed by the word exists Oraton road, because there is a forward edge, so the use of the direction of the Tuola access to the decision can be.

Judging conditions

(1): Basemap is connected graph

(2): There can be two singularity, one of the degree of the ratio of 1, and the other one into the degree of a greater than 1.

For condition 1, here with and check set judgment, condition 2 statistics each point of the degree, into the degree, to judge on the line.

Code:

#include <stdio.h>#include<string.h>#include<iostream>#include<math.h>#include<queue>#include<stack>#include<algorithm>using namespacestd; Const intMAXV = in;intm;intPRE[MAXV +7];intOUTDEGREE[MAXV +7];intINDEGREE[MAXV +7];intFind (intx) {returnx = = Pre[x]? X:PRE[X] =Find (pre[x]);} And look up the search setvoidInitpre () { for(inti =0; I <= MAXV; i++) Pre[i] =i;} Initialize and check the array of the setintMixintXinty)//and merge of the check set {intFX = Find (x), FY =Find (y); if(FX! = FY) Pre[fx] =fy; }BOOLisconnct ()//Determine whether the graph is connected, that is, all the points are in a set {intCNT =0;  for(inti =1; I <= MAXV; i++)if((outdegree[i]! =0|| Indegree[i]! =0) && Pre[i] = = i) cnt++; if(CNT = =1)return true; return false;}BOOLIseulur ()//whether there is Euler pathway {intCNT =0; intFlag =0;  for(inti =1; I <= -; i++)        if((outdegree[i]! =0|| Indegree[i]! =0) && (indegree[i]! =Outdegree[i]))        Judging the singularity, the method is not unique. {CNT++; Flag+ = (Indegree[i]-Outdegree[i]); if(Flag >1|| Flag <-1)return false; }     if(CNT = =0|| CNT = =2&& flag = =0)return true; return false;}intMain () {intT; scanf ("%d", &T);  while(t--) {scanf ("%d", &m);        Initpre (); memset (Indegree,0,sizeof(Indegree)); memset (Outdegree,0,sizeof(Outdegree));  for(inti =1; I <= m; i++)        {            Charword[ ++7]; scanf ("%s", Word); intU = word[0] -'a'+1; intLen =strlen (word); intv = word[len-1] -'a'+1;            Mix (U, v); ++Outdegree[u]; ++Indegree[v]; }        if(Iseulur () && isconnct ()) printf ("Ordering is possible.\n"); Elseprintf"The door cannot be opened.\n"); }    return 0;}

UVA 10129 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.