HDU-1116 Play on Words

HDU-1116 Play on Words

并查集、欧拉道路


Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9946 Accepted Submission(s): 3401


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


题意

输入一些英文单词,根据该单词的首尾字母,判断所有单词能不能连成一串,类似于成语接龙的意思。同样如果有多个重复的单词时,也必须满足这样的条件才能通过,否则都是不可能的情况。输入包括若干个案例,每个案例中最多有100000个单词。

思路分析

这题需要运用并查集和欧拉回路的知识。
首先建立所有26个字母的集合,将有联系的字母集合合并。而存在的联系与单词的首位字母有关,于是将每个单词的首尾字母提取,抽象成两个顶点,两个顶点是存在联系的,将这两个顶点合并到一个并查集中。此时需要利用利用欧拉道路的存在性判断。
欧拉道路,即每一条边可以不重复的走一遍就是欧拉道路,如果刚好回到原点,则构成欧拉回路。
欧拉道路需要满足 1、所有点的出度与入度之差的绝对值不能大于1且不为0的个数为2或0;2、所有点是联通的。


代码
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int T,N;
int k;/*连通块个数*/
int par[30],in[30],flag[30];
void Init()
{
    for(int i=0;i<26;i++)
        par[i]=i;
}
int find(int x)
{
    if(par[x]==x)
        return x;
    else
    {
        return par[x]=find(par[x]);
    }
}
void unite(int x,int y)
{
    x=find(x);
    y=find(y);
    if(x==y)
        return;
    k--;
    par[x]=y;
}
int main()
{
    cin>>T;
    while(T--)
    {
        char str[1010];
        Init();
        memset(in,0,sizeof(in));
        memset(flag,0,sizeof(flag));
        cin>>N;
        k=26;
        while(N--)
        {
            scanf("%s",str);
            int a,b;
            a=str[0]-'a';
            b=str[strlen(str)-1]-'a';
            unite(a,b);
            in[a]++;/*入度加一*/
            in[b]--;/*出度减一*/
            flag[a]=flag[b]=1;
        }
        int cnt=0;/*入度出度差不为零的数*/
        bool ok=true;
        for(int i=0;i<26;i++) {
            if(!flag[i])
            k--; /*去掉没出现过的字母*/
            else if(in[i])
            {
                if(in[i]>1||in[i]<-1)
                {
                    ok=false;
                    break;
                }/*欧拉路中每个结点的入度与出度之差只能为0或者
                为1,当超出此范围不构成欧拉路*/
                cnt++;
                if(cnt!=2||cnt!=0)
                {
                    ok=false;
                    break;
                }/*当入度出度差不为零的数不为2,说明不构成欧拉路*/
            }
        }
        if(k!=1)
            ok=false;/*去掉未出现过的字母后,连通图的个数
            如果大于1,即不是所有的点都是连通的,不构成欧拉路*/
        if(ok)
            printf("Ordering is possible.\n");
        else
            printf("The door cannot be opened.\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值