HDU 1116 Play on Words (Eulerian graph)

Source: Internet
Author: User

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)

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.