[UVA10129]Play On Wordsios
算法入門經典第6章6-16(P169)git
題目大意:有一些單詞,問能不能將它們串成字符串(只有前綴和後綴相同才能連)算法
試題分析:很巧妙的一道題,將每一個單詞的首尾字符相連,先判斷出現過的單詞的連通性(並查集),再利用歐拉路有向圖的斷定方法斷定是否構成歐拉路。spa
#include<iostream> #include<cstring> #include<cstdio> #include<vector> #include<queue> #include<stack> #include<algorithm> using namespace std; inline int read(){ int x=0,f=1;char c=getchar(); for(;!isdigit(c);c=getchar()) if(c=='-') f=-1; for(;isdigit(c);c=getchar()) x=x*10+c-'0'; return x*f; } const int MAXN=100001; const int INF=999999; int N,M; int T; int fa[27]; int Hash[27]; int root; int find(int x){ if(x!=fa[x]) return fa[x]=find(fa[x]); return x; } void merge(int a,int b){ int x=find(a); int y=find(b); if(x==y) return ; fa[y]=x; root=x; } char str[1001]; int od[27],id[27]; int main(){ T=read(); while(T--){ N=read(); bool flag=false; memset(od,0,sizeof(od)); memset(id,0,sizeof(id)); memset(Hash,false,sizeof(Hash)); for(int i=0;i<=26;i++) fa[i]=i; for(int i=1;i<=N;i++){ scanf("%s",str); int len=strlen(str); merge(str[0]-'a',str[len-1]-'a'); od[str[0]-'a']++; id[str[len-1]-'a']++; Hash[str[len-1]-'a']=Hash[str[0]-'a']=true; } for(int i=0;i<26;i++){ if(Hash[i]&&find(i)!=root) {printf("The door cannot be opened.\n");flag=true;break;} } if(flag) continue; int a=0,b=0; for(int i=0;i<26;i++){ if(abs(od[i]-id[i])>1){ printf("The door cannot be opened.\n"); flag=true; break; } else{ if(od[i]-id[i]==1) a++; else if(id[i]-od[i]==1) b++; } } if(flag) continue; if((!a&&!b)||(a==1&&b==1)){ printf("Ordering is possible.\n"); } else{ printf("The door cannot be opened.\n"); } } }