UVA-10129 Play on Words (Euler loop + and check set)

Source: Internet
Author: User

2. Problem-Solving ideas: The problem of the use of Euler circuit conditions to solve. All the words can be regarded as the edge, 26 letters as the end point, then the question is actually asked if there is a path, you can reach all occurrences of the word connector points. Since the subject also requires two words to be spelled together, the condition is the same as the right end of the previous word and the left end of the word. So this is a forward graph. According to the conclusion: the Basemap of the directed graph (which ignores the direction of the edge) must be connected, the in-line graph can only have a maximum of two endpoints of the degree is not equal to the degree, and must be one of the points in the degree of 1 less than the degree of the other point in the degree of 1 greater than the degree. Therefore, first determine whether the endpoints are connected, and then determine whether the degree of each endpoint to meet the conclusion.

So, how to determine connectivity? The first approach is to use DFS, and the second method can take advantage of and look at the set. The subject uses and checks the set to determine whether connectivity.

1 #pragmaComment (linker, "/stack:1024000000,1024000000")2#include <iostream>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <math.h>7#include <algorithm>8#include <queue>9#include <Set>Ten#include <bitset> One#include <map> A#include <vector> -#include <stdlib.h> -#include <stack> the using namespacestd; - intdirx[]={0,0,-1,1}; - intdiry[]={-1,1,0,0}; - #definePI ACOs (-1.0) + #defineMax (a) (a) > (b)? (a): (b) - #defineMin (a) (a) < (b)? (a): (b) + #definell Long Long A #defineEPS 1e-10 at #defineMOD 1000000007 - #defineN 100006 - #defineINF 1e12 - intN; - intU[n],v[n]; - int inch[N], out[N]; in intVis[n]; - intFa[n]; to intnum; + voidinit () { -Memsetinch,0,sizeof(inch)); theMemset out,0,sizeof( out)); *memset (Vis,0,sizeof(Vis)); $     for(intI=0; i<n;i++){Panax Notoginsengfa[i]=i; -    } thenum=0; + } A ///////////////////////////////////////////////////////////////////// the intFindintx) { +    returnfa[x]==x?x:fa[x]=find (Fa[x]); - } $ voidMergeintXinty) { $    introot1=find (x); -    intRoot2=find (y); -    if(ROOT1==ROOT2)return; thefa[root1]=Root2; - }Wuyi ////////////////////////////////////////////////////////////////////// the voidsolve () { -  Wu      for(intI=0; i<n;i++){ - merge (U[i],v[i]); About     } $     intI=0; -      for(i;! vis[i];i++); -  -     intX=find (i);//determine if connectivity is possible A      for(intj=i+1;j< -; j + +){ +         if(Vis[j]) { the            inty=find (j); -            if(x!=y) { $printf"The door cannot be opened.\n"); the                return; the            } the         } the     } -  in     intflag=1; the     intCnt=0; the      for(intI=0;i< -; i++){ About         if(Vis[i]) { the            if(inch[i]!= out[i]) { the               if(inch[i]== out[i]-1) cnt++; the               Else if(inch[i]== out[i]+1) cnt++; +               Else{ -flag=0; the                    Break;Bayi               } the            } the            if(cnt>2){ -flag=0; -                 Break; the            } the         } the     } the     if(flag) { -printf"Ordering is possible.\n"); the     } the     Else{ theprintf"The door cannot be opened.\n");94     } the  the } the intMain ()98 { About    intT; -scanf"%d",&t);101     while(t--){102 103 init ();104  the          Chars[1006];106scanf"%d",&n);107           for(intI=0; i<n;i++){108scanf"%s", s);109              intlen=strlen (s); the              inta=s[0]-'a';111              intb=s[len-1]-'a'; theu[i]=a,v[i]=b;113              inch[A]++, out[b]++; thevis[a]=1; thevis[b]=1; the          }117 solve ();118    }119     return 0; -}
View Code

UVA-10129 Play on Words (Euler loop + set)

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.