hdu 1116 Play on Words
简单欧拉回路判定:
当是单向欧拉回路时,必须保证每个结点入度等于出度。
当是单向欧拉路时,必须保证除了两个结点外,每个结点的入度等于出度,但这两个结点中,一个结点的入度比出度大1,另一个结点的入度比出度小1。
#include<stdio.h> #include<string.h> int IN[30],OUT[30]; int F[30],D[30]; char as[1005]; int f(int x){if(x!=F[x]) x = f(F[x]); return x;} int main() { int i,j,k,t,n,x,y; scanf("%d",&t); while(t--) { scanf("%d",&n); memset(IN,0,sizeof(IN)); memset(OUT,0,sizeof(OUT)); memset(D,0,sizeof(D)); for(i = 0; i < 30; ++ i) F[i] = i; while(n--) { scanf("%s",as); x = as[0] - 'a' + 1; y = as[strlen(as)-1] - 'a' + 1; IN[x]++; OUT[y]++; D[x] = 1; D[y] = 1; x = f(x); y = f(y); if(x!=y) F[y] = x; } k = 0; for(i = 1; i <= 26; ++ i)if(D[i]&&F[i]==i) k++; if(k>1) puts("The door cannot be opened."); else{ for(k=0,i=1; i<=26;++i)if(IN[i]!=OUT[i]) D[k++] = i; if(!k)puts("Ordering is possible."); else if((k==2)&&((IN[D[0]]==OUT[D[0]]+1&&IN[D[1]]==OUT[D[1]]-1)||(IN[D[0]]==OUT[D[0]]-1&&IN[D[1]]==OUT[D[1]]+1))) puts("Ordering is possible."); else puts("The door cannot be opened."); } }return 0; }