hdu1116欧拉道路

题目链接Play on Words

将给出的所有单词首尾相连,前一个单词的尾部必须和下一个单词字首相同

acm->motorola....


欧拉通路:图中存在一条通过各边一次且仅一次的通路

欧拉回路:图中存在一条通过各边一次且仅一次的回路


如果一个无向图是连通的,且最多只有两个奇点(度数为奇数),则一定存在欧拉道路。

如果有两个奇点,则必须从一个奇点出发,另一个奇点终止;如果奇点不存在,则可以从任意点出发,最终一定会回到该点(称为欧拉回路)


对于有向图:

1、在忽略边的方向后,图必须连通

2、最多只能有两个点的入度不等于出度,而且必须是其中一个点的出度恰好比入度大1;另一个的入度比出度大1(把他作为终点)

#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <string.h>
using namespace std;

const int maxl=1005;
int F[26];
int Find(int x)
{
    if(F[x]==-1)return x;
    else return F[x]=Find(F[x]);
}

int Merge(int x,int y)
{
    int t1=Find(x);
    int t2=Find(y);
    if(t1!=t2)F[t1]=t2;
}

int main()
{
    //freopen("in.txt","r",stdin);
    int T,n;
    scanf("%d",&T);
    char str[maxl];
    int in[26],out[26];
    bool vis[26];
    while(T--){
        scanf("%d",&n);
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        memset(vis,false,sizeof(vis));
        memset(F,-1,sizeof(F));
        while(n--){
            scanf("%s",str);
            int len=strlen(str);
            int a=str[0]-'a';int b=str[len-1]-'a';
            in[a]++;out[b]++;
            vis[a]=true;vis[b]=true;
            Merge(a,b);
        }
        int cnt=0;
        for(int i=0;i<26;i++){
            if(vis[i]&&F[i]==-1){
                cnt++;
            }
        }
        if(cnt>1){
            printf("The door cannot be opened.\n");
            continue;
        }
        int st=0,en=0;
        bool tag=true;;
        for(int i=0;i<26;i++){
            if(out[i]-in[i]==1)st++;
            else if(in[i]-out[i]==1)en++;
            else if(in[i]!=out[i]){
                tag=false;
                break;
            }
        }
        if(tag&&((st==1&&en==1)||(st==0&&en==0)))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、付费专栏及课程。

余额充值