HDU - 1116 Play on Words(建立模型+欧拉路径)

题目链接https://vjudge.net/contest/365311#problem/G
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.

翻译
n个仅有小写字母组成的英文单词。是否能有一个合适的顺序,使得相邻两个单词,前一个单词的末字母等于后一个单词的首字母
若能达到要求输出Ordering is possible.
否则输出The door cannot be opened.

模型
以26个字母作为顶点,对于每一个单词,如果它的首字母为c1,末字母为c2,那么从c1向c2连一条有向边
问题转化为在图中寻找一条不重复地经过每一条边的路径,即欧拉路径
在这里插入图片描述

欧拉回路

图G中经过每条边一次并且仅一次的回路。

欧拉路径

图G中经过每条边一次并且仅一次的路径。

欧拉图:存在欧拉回路
半欧拉图:存在欧拉路径,但不存在欧拉回路

判断欧拉图或半欧拉图的方法

无向图

欧拉图:G为连通图,且所有的顶点的度为偶数
半欧拉图:G为连通图,除了2个顶点的度为奇数,其它所有顶点的度为偶数所有顶点的度都为偶数

有向图

欧拉图:G的基图连通,且所有顶点的入度等于出度
半欧拉图:G的基图连通,且存在u的入度比出度大1,v的入度比出度小1,其它所有顶点的入度等于出度所有顶点的入度等于出度


基图:忽略有向图所有边的方向,得到的无向图称为该有向图的基图

算法
判断图是否连通=并查集(是否只有一个祖先)

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=30;
bool book[N];
int in[N],out[N],f[N];
void init()
{
    memset(book,false,sizeof(book));
    memset(in,0,sizeof(in));
    memset(out,0,sizeof(out));
    for(int i=0; i<26; i++)
        f[i]=i;
}
int getf(int u)
{
    if(u!=f[u])
        f[u]=getf(f[u]);
    return f[u];
}
void gether(int u,int v)/*并查集判断连通性*/
{
    int t1=getf(u);
    int t2=getf(v);
    if(t1!=t2)
        f[t2]=t1;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        init();
        int n;
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            char mp[1010];
            scanf("%s",mp);
            int len=strlen(mp);
            int fir=mp[0]-'a',sec=mp[len-1]-'a';
            book[fir]=true;
            book[sec]=true;/*标记这个字母出现过*/
            out[fir]++;/*出度++*/
            in[sec]++;/*入度++*/
            gether(fir,sec);
        }
        int root=0;
        for(int i=0; i<26; i++)
        {
            if(book[i]&&f[i]==i)/*判断祖先的个数*/
                root++;
        }
        if(root>1)
            printf("The door cannot be opened.\n");
        else
        {
            int sign[5],tot=0;
            int in1,in2,out1,out2;
            for(int i=0; i<26; i++)
            {
                if(book[i]&&in[i]!=out[i])
                    sign[tot++]=i;/*tot记录入度不等于出度的点的个数*/
            }
            if(tot==0)/*所有点的入度等于出度*/
                printf("Ordering is possible.\n");
            else if(tot==2)
            {
                if(in[sign[0]]-out[sign[0]]==1&&in[sign[1]]-out[sign[1]]==-1||in[sign[0]]-out[sign[0]]==-1&&in[sign[1]]-out[sign[1]]==1)
                    printf("Ordering is possible.\n");
                else
                    printf("The door cannot be opened.\n");
            }
            else
                printf("The door cannot be opened.\n");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zaiyang遇见

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值