10129 Play on Words-----并查集+欧拉路!!!

15 篇文章 0 订阅
7 篇文章 0 订阅
题意:判断所有字符串是不是可以连在一起,实际上就是判断所构造的有向图是否存在欧拉回路或者欧拉图;


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include <algorithm>
#define N 100010
using namespace std;
char c[N][2];
int al[123];
int in[27],out[27];
int f[27],ccount;
bool yo[26];
int tou(int x)
{
    if(f[x]!=x)
    {
        f[x]=tou(f[x]);
    }
    return f[x];
}
void build(int x,int y)
{
    int toux=tou(x),touy=tou(y);
    if(touy!=toux)
    {
        ++ccount;
        f[toux]=touy;
    }
}

int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        memset(al,0,sizeof(al));
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        memset(yo,0,sizeof(yo));
        memset(f,0,sizeof(f));


        scanf("%d%*c",&n);
        int geshu=0;
        for(int i=0; i<n; i++)
        {
            char cc,ccc;
            scanf("%c",&c[i][0]);
            if(!yo[c[i][0]-'a'])
            {
                yo[c[i][0]-'a']=1;
                ++geshu;
            }
            while(scanf("%c",&cc)&&cc!='\n')
            {
                ccc=cc;
            }
            c[i][1]=ccc;
            if(!yo[c[i][1]-'a'])
            {
                yo[c[i][1]-'a']=1;
                ++geshu;
            }
            in[c[i][1]-'a']++;
            out[c[i][0]-'a']++;
        }
        for(int i=0; i<26; i++)
            f[i]=i;

        ccount=0;
        for(int i=0; i<n; i++)
            build(c[i][0]-'a',c[i][1]-'a');
        int count=0,flag=1;

        if(ccount+1!=geshu)
            flag=0;
        if(flag)
            for(int i=0; i<26; i++)
            {
                if(in[i]!=out[i])
                {
                    count++;
                    if(abs(in[i]-out[i])!=1||count>2)
                    {
                        flag=0;
                        break;
                    }
                }
            }

        if(!flag)
            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、付费专栏及课程。

余额充值