紫书uva10129欧拉回路


题目类型: 欧拉道路


题目:

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.


题目大意翻译:


有一些秘密的门包含着非常有趣的单词迷题, 考古学家队伍必须解决它们才能够打开大门。 因为没有其他方法能偶打开这些门, 所以解决那些迷题对我们非常重要。


在每个门上有很多个有磁力的盘子,盘子上面写着单词。 必须重新移动放置这些盘子,让它们形成一个队列:队列中,除了第一个单词,每个单词的开头和上一个单词的结尾字母

一样。例如, motorola的后面可以接上acm。 


你的任务是写一个程序, 读入一系列单词,然后计算确定它们是否有可能被排成这样的队列。


样例输入:

3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok

样例输出:

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

第一次遇到写欧拉回路的问题,欧拉回路好像也是叫一笔划问题,能否一笔划取决于奇点不超过两个(出度入度相差1),其余的点都是出入度相等的点,我看别人博客的时候,别人写写错了弄得我看了很久,dfs判断是否联通的时候,先把有向图当成无向图去处理,然后再从任意有出度的点开始dfs,这个就是基本的数据结构图的dfs,如果找到一个点(有入度但没有搜索到)||(有出度但没搜索到),这证明这个点是不连通的a-m m-a b-b,这样来看,b是不可能同时和a,m搜索到的,所以这是不能构成欧拉回路的,出度多1的是起始点,入度少1的是终止点,还有一点就是,这道题和一开始输入的n完全没有关系,我们要遍历的是26个字母是否被完全遍历,所以和n没有任何关系,题目大概意思就是单词能不能全部首尾相连,那么我们把第一个字母和最后一个字母当成节点,这个单词当成路径如,rude ,就是r->e,我们分别记录下每个字母的出入度,然后根据上面的定义,找出奇点和完全不符合的点,那么这道题就完成了。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
//欧拉回路
using namespace std;
int n;
int map[109][109];
int vis[109];
char str[1009];
int in[109],out[109];
void dfs(int x)
{
    int i;
    for(i=0; i<=100; i++)
    {
        if(!vis[i]&&map[x][i])
        {
            vis[i]=1;
            dfs(i);
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(vis,0,sizeof(vis));
        memset(map,0,sizeof(map));
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        int i,j,k,n1=0,n2=0;
        scanf("%d",&n);
        for(i=0; i<=n-1; i++)
        {
            scanf("%s",str);
            int j=str[0]-'a';
            int k=str[strlen(str)-1]-'a';
            out[j]++;
            in[k]++;
            map[j][k]=1;
            map[k][j]=1;
        }
        int flag=0;
        for(i=0; i<=100; i++)
        {
            if(out[i]!=in[i])//出度和入度不相等
            {
                if(out[i]==in[i]+1)//这是起始点
                    n1++;
                else if(in[i]==out[i]+1)//这是终结点
                    n2++;
                else
                {
                    flag=1;
                    break;
                }
            }
        }
        if(n1&&n2&&n1+n2>2)
        {
            flag=1;
        }
        //找到不符合出度等于入度的点
        //或者奇点个数太多,欧拉回路中奇点个数最多两个奇点
        if(flag) printf("The door cannot be opened.\n");
        else
        {
            int flag2=0;
            for(i=0; i<=100; i++)
            {
                if(out[i])
                {
                    dfs(i);//只要符合欧拉回路,那么最后一定可以找到点
                    //如 c-a-a-b-b-d
                    break;
                }
            }
            for(i=0; i<=100; i++)
            {
                if(!vis[i]&&in[i])
                {
                    flag2=1;
                    break;
                }
               else if(!vis[i]&&out[i])
                {
                    flag2=1;
                    break;
                }
            }
            if(flag2) printf("The door cannot be opened.\n");
            else printf("Ordering is possible.\n");
        }
    }
    return 0;

}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值