Play on Words HDU - 1116 (欧拉通路)

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.". 

Sample Input

3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok

Sample Output

The door cannot be opened.
Ordering is possible.
The door cannot be opened.

题意与思路:

         读入一组单词,然后判断是否可以经过重组是得每个单词第一个字母跟前一个单词最后一个单词字母相同,这样们才可以打开。

        关键在于单词的首尾字母,并且每个单词可看成首尾相连两个字母的一条有向边(即首尾字母的匹配),每组顶点为26个小写字母,每个单词为图中的一条边,所以需要构造有向图,重组判定每个单词第一个字母跟前一个单词最后一个字母相同,等效于判断图中是否存在一条路径经过每条边一次且仅一次,就是欧拉通路。所以这个过程我们要判断每个顶点出度入度的关系进行处理。

代码如下:

#include<cstdio>
#include<cstring>
#define MAXN 100001//边数最大值
#define INF 0x3f3f3f3f
int T;
int N;
char word[10001];//读入的每个单词
int od[26],id[26];//每个字母的入度出度
int bused[26];//表示第i个字母在这组单词中是否作为首尾字母
int parent[26];//为顶点i所在集合对应的树中的根结点
struct edge//边
{
    int u,v;//边的顶点
}edges[MAXN];//边的数组
void UFset()//初始化
{
    for(int i=0;i<26;i++)
        parent[i]=-1;
}
int Find(int x)//查找并返回节点x所属集合的根结点
{
    int s;//查找位置
    for(s=x;parent[s]>=0;s=parent[s])
        ;
    while(s!=x)//优化方案——压缩路径,使后续的查找操作加速
    {
        int tmp=parent[x];
        parent[x]=s;
        x=tmp;
    }
    return s;
}
//将两个集合的元素进行合并,使两个集合中任两个元素都连通
void Union(int R1,int R2)
{
    int r1=Find(R1);//r1为R1的根结点
    int r2=Find(R2);//r2为R2的根结点
    int tmp=parent[r1]+parent[r2];//练个级和节点个数之和(负数)
    //如果R2所在树结点个数>R1所在树结点个数(注意parent[r1]是负数)
    if(parent[r1]>parent[r2])//优化方案--加权法则
    {
        parent[r1]=r2;
        parent[r2]=tmp;
    }
    else
    {
        parent[r2]=r1;
        parent[r1]=tmp;
    }
}
bool bconnect()//判断有向图的基图是否连通
{
    int u,v,i;//每条边的两个顶点、循环变量
    UFset();  //初始化
    for(i=0;i<N;i++)//对每条边(u,v),如果u和v不属于同一个连通分量,则合并
    {
        u=edges[i].u;
        v=edges[i].v;
        if(u!=v&&Find(u)!=Find(v))
            Union(u,v);
    }
    int first=-1;//第一个bused[i]不为0的顶点
    for(i=0;i<26;i++)
    {
        if(bused[i]==0)
            continue;
        if(first==-1)
            first=i;
        else if(Find(i)!=Find(first))
            break;//不连通
    }
    if(i<26)
        return false;//不连通
    else
        return true;//连通
}
int main()
{
    int u,v;//每个单词首尾字母所对应的序号
    int i,j;//循环变量
    scanf("%d",&T);
    for(i=0;i<T;i++)
    {
        memset(od,0,sizeof(od));
        memset(id,0,sizeof(id));
        memset(bused,0,sizeof(bused));
        scanf("%d",&N);
        for(j=0;j<N;j++)
        {
            scanf("%s",word);
            u=word[0]-'a';
            v=word[strlen(word)-1]-'a';
            od[u]++;
            id[v]++;
            bused[u]=bused[v]=1;
            edges[j].u=u;
            edges[j].v=v;
        }
        bool Euler=true;//是否存在欧拉通路
        int one=0;  //出度比入度多1的顶点个数
        int none=0; //出度比入度少1的顶点个数
        for(j=0;j<26;j++)
        {
            if(bused[j]==0)
                continue;
            if(od[j]-id[j]>=2||id[j]-od[j]>=2)
            {
                Euler=false;
                break;
            }
            if(od[j]==0&&id[j]==0)
            {
                Euler=false;
                break;
            }
            if(od[j]-id[j]==1)
            {
                one++;
                if(one>1)
                {
                    Euler=false;
                    break;
                }
            }
            if(id[j]-od[j]==1)
            {
                none++;
                if(none>1)
                {
                    Euler=false;
                    break;
                }
            }
        }
        if(one!=none)
            Euler=false;
        if(!bconnect())
            Euler=false;//不连通
        if(Euler)
            printf("Ordering is possible.\n");
        else
            printf("The door cannot be opened.\n");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值