Test instructions: Judging whether some words can be joined together
#include <iostream>#include<algorithm>#include<cstring>#include<cmath>#include<cstdio>using namespacestd;intn,father[ -],range[ -],save[100010],inch[ -], out[ -];BOOLuse[ -];Chars[1010];voidAddintx) {Use[x]=true; FATHER[X]=x; RANGE[X]=0;}intFind (intx) { intk=0; while(x!=Father[x]) {Save[k++]=x; X=Father[x]; } for(intj=0; j<k; J + +) Father[save[j]]=x; returnx;}voidUniintXinty) { intA=find (x), b=Find (y); if(a!=b) {if(Range[a]<range[b]) father[a]=b; Else{Father[b]=A; if(Range[a]==range[b]) range[a]++; } }}intMain () {Freopen ("In.txt","R", stdin); intT; scanf ("%d",&t); while(t--) {scanf ("%d",&N); memset (use,false,sizeof(use)); memset (father,0,sizeof(father)); memset (range,0,sizeof(range)); memset (inch,0,sizeof(inch)); memset ( out,0,sizeof( out)); for(intI=1; i<=n; i++) {scanf ("%s", s); intL=strlen (s), x=s[0]-'a', y=s[l-1]-'a'; if(use[x]==false) Add (x); if(use[y]==false) Add (y); inch[x]++; out[y]++; Uni (x, y); } intflag=0; for(intI=0; i< -; i++) { if(use[i]==true&&find (i) = =i) {flag++; } } if(flag>1) printf ("The door cannot be opened.\n"); Else { intA=0, b=0, I; for(i=0;i< -; i++) { if(use[i]&&inch[i]!= out[i]) {if(inch[i]- out[i]==1) a++; Else if( out[i]-inch[i]==1) b++; Else Break; } } if(i< -) printf ("The door cannot be opened.\n"); Else if(a+b==0|| a==1&&b==1) printf ("Ordering is possible.\n"); Elseprintf"The door cannot be opened.\n"); } } return 0;}
POJ1386 Play on Words