1. Test instructions: give n words and ask if you can concatenate all the words in the same way as the idiom solitaire.
2. Idea: The word is viewed from the initial letter to the tail letter of the Forward Edge (ARC) can be. As long as the graph is Eulerian graph, then it can be connected. Otherwise, you cannot do so. when writing and checking the Union, be careful to note that it is a=find (a); Don't repeat the mistake.
3. Code:
#include <cstdio> #include <cstring>using namespace Std;int father[30];char s[1005];int in[30],out[30];int Vis[30];int deta[30];int mat[30][30];int F (int a) {return a>0?a:-a;} int Find (int a) {int r=a; while (Father[a]!=a) {a=father[a]; } father[r]=a; return A;} void Union (int a,int b) {a=find (a); B=find (b); if (a!=b) {father[b]=a; }}int Main () {int t; scanf ("%d", &t); while (t--) {int n; memset (Mat,0,sizeof (MAT)); scanf ("%d", &n); for (int i=1; i<=26; i++) {father[i]=i; in[i]=out[i]=0; vis[i]=0; } for (int i=1; i<=n; i++) {scanf ("%s", s); int Len=strlen (s); int A, B; a=s[0]-' a ' +1; b=s[len-1]-' a ' +1; in[a]++; out[b]++; Vis[a]=vis[b]=1; if (mat[a][b]==0&&mat[b][a]==0) {Union (A, b); Mat[a][b]=mat[b][a]=1; }} int cnt=0; int cnt1=0; for (int i=1; i<=26; i++) {if (Find (i) ==i&&vis[i]==1) cnt++; if (Vis[i]==1&&in[i]!=out[i]) {cnt1++; Deta[cnt1]=in[i]-out[i]; }} if (cnt==1&& (cnt1==0| | (Cnt1==2&&f (deta[1]) ==1&&f (deta[2]) ==1))) {printf ("Ordering is possible.\n"); } else printf ("The door cannot be opened.\n"); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1116 Play on Words (Eulerian graph)