HDOJ 1116 Play on Words(欧拉回路)

来源:互联网 发布:cc2541中文数据手册 编辑:程序博客网 时间:2024/05/01 02:52
 Play on Words
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu
Submit Status

Description

Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzzle is very important for us. 

There is a large number of magnetic plates on every door. Every plate has one word written on it. The plates must be arranged into a sequence in such a way that every word begins with the same letter as the previous word ends. For example, the word ``acm'' can be followed by the word ``motorola''. Your task is to write a computer program that will read the list of words and determine whether it is possible to arrange all of the plates in a sequence (according to the given rule) and consequently to open the door. 

Input

The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing a single integer number Nthat indicates the number of plates (1 <= N <= 100000). Then exactly Nlines follow, each containing a single word. Each word contains at least two and at most 1000 lowercase characters, that means only letters 'a' through 'z' will appear in the word. The same word may appear several times in the list.

Output

Your program has to determine whether it is possible to arrange all the plates in a sequence such that the first letter of each word is equal to the last letter of the previous word. All the plates from the list must be used, each exactly once. The words mentioned several times must be used that number of times. 
If there exists such an ordering of plates, your program should print the sentence "Ordering is possible.". Otherwise, output the sentence "The door cannot be opened.". 

看案例还是比较好懂的,可以把每个字符串保留头和尾,建立由头-->尾的边,最后判断这个有向图是否存在半欧拉回路,既是是否能够走到从起点走到终点,先判断图的连通性,这里有并查集和BFS都可以做,并查集要快一点,在这个图连通的情况下判读入度和出读,因为是有向图,所以只要两个点的出度不等与入度,且做为起点的出度比入度大一,终点的出度比入度小一,判断以上条件是否满足就行了,注意细节和测试数据。

#include<stdio.h>     //BFS#include<queue>#include<algorithm>#include<string.h>using namespace std;int a[30][30];int rudu[30];int chudu[30];int x[3];void BFS(int now){    queue<int>que;    que.push(now);    while(!que.empty())    {        now=que.front();        que.pop();        for(int i=1;i<=26;i++)        {            if(a[now][i]!=0)            {                a[now][i]--;                que.push(i);            }        }    }}int main(){    int t,n;    char s[2000];    scanf("%d",&t);    while(t--)    {        memset(a,0,sizeof(a));        memset(rudu,0,sizeof(rudu));        memset(chudu,0,sizeof(chudu));        scanf("%d",&n);        int A,B;        for(int i=0;i<n;i++)        {            scanf("%s",s);            A=s[0]-'a'+1;            B=s[strlen(s)-1]-'a'+1;            a[A][B]++;            chudu[A]++;            rudu[B]++;        }        int sum=0;        int lenx=0;        int s=0;        int tempx;        for(int i=1;i<=26;i++)        {            if(rudu[i]!=chudu[i])            {                sum++;                x[lenx++]=i;            }        }        if(sum==2||sum==0)        {            if(sum==2)            {                int a1=x[0];                int a2=x[1];                int flag=0;                if((chudu[a1]-rudu[a1])==1)                {                    if((chudu[a2]-rudu[a2])==-1)                    {                        flag=1;                        tempx=a1;                    }                }                else if((chudu[a1]-rudu[a1])==-1)                {                    if((chudu[a2]-rudu[a2])==1)                    {                        flag=1;                        tempx=a2;                    }                }                if(flag==0)                {                    printf("The door cannot be opened.\n");                    continue;                }            }            else            {                for(int i=1;i<=n;i++)                {                    if(rudu[i]!=0||chudu[i]!=0)                    {                        tempx=i;                        break;                    }                }            }            BFS(tempx);            int f=0;            for(int i=1;i<=26;i++)            {                if(f==1)                    break;                for(int j=1;j<=26;j++)                {                    if(a[i][j]>=1)                    {                        f=1;                        break;                    }                }            }            if(f==1)                printf("The door cannot be opened.\n");            else                printf("Ordering is possible.\n");        }        else            printf("The door cannot be opened.\n");    }return 0;}

#include<stdio.h>     //并查集#include<string.h>#include<queue>#include<vector>using namespace std;int F[300],n;int ru[300],chu[300];int use[300];int findfa(int x)   //查找维护并查集{    if(F[x]==x)        return F[x];    F[x]=findfa(F[x]);    return F[x];}int main(){    int t;    char s[2000];    scanf("%d",&t);    while(t--)    {        for(int i=1;i<=26;i++)            F[i]=i;        memset(ru,0,sizeof(ru));        memset(chu,0,sizeof(chu));        memset(use,0,sizeof(use));        scanf("%d",&n);        int x,y;        for(int i=0;i<n;i++)        {            scanf("%s",s);            x=s[0]-'a'+1;            y=s[strlen(s)-1]-'a'+1;            chu[x]++;            ru[y]++;            use[x]=1;            use[y]=1;            int dx=findfa(x);  //头结点的最远父节点            int dy=findfa(y);   //尾结点的最远父节点            if(dx!=dy)      //两个父节点不一样,说明在分别在两个子树上面,此时有这条边的情况下,可以合并两课子树                F[dx]=dy;        }        int sum=0;        for(int i=1;i<=26;i++)        {            if(use[i]==1&&F[i]==i)    //如果这个字母出现过,且一棵树只有一个跟结点既是F[i]=i;                sum++;        }        if(sum>1)         //判断子树的多少        {            //printf("YES1\n");            printf("The door cannot be opened.\n");            continue;        }        sum=0;        int b[1000];        for(int i=1;i<=26;i++)        {            if(ru[i]!=chu[i]&&use[i]==1)            {                //printf("%d\n",i);                b[sum++]=i;            }        }        if(sum==2)        {            int flag=0;            x=b[0];            y=b[1];           // printf("%d %d    \n",ru[y],chu[y]);            if((chu[x]-ru[x])==1)            {                if((chu[y]-ru[y])==-1)                    flag=1;            }            else if((chu[y]-ru[y])==1)            {                 if((chu[x]-ru[x])==-1)                    flag=1;            }            if(flag==1)                printf("Ordering is possible.\n");            else                printf("The door cannot be opened.\n");        }        else if(sum==0)            printf("Ordering is possible.\n");        else        {            printf("The door cannot be opened.\n");            //printf("YES2\n");        }    }return 0;}


0 0