UVA Play On Words 10129

输入n(n <= 100000)个单词, 是否可以吧所有这些单词排成一个序列, 使得每个单词的第一个字母和上一个单词的最后一个字母相同。每个单词最多包含1000个小写字母

。输入中可以有重复单词。

即使有重边  也完全可以按照欧拉道路的判定思想来做。

对于有向图:如果有两个点的入度和出度不相等。而且同时保证其中一个点的出度恰好比入度大1, 另一个点的入度比出度大1。且保证图是连通的。那么此时满足欧拉道路

                        如果所有点的入度和出度相同,且保证图是连通的。那么此时满足欧拉回路。

对于无向图:所有点的入度和出度相同,且保证图是连通的,那么此时满足欧拉回路(欧拉图)。

                       如果出现两个奇点,从一个奇点出发,另一个奇点终止,可以走一条欧拉道路。,那么此时满足欧拉回路。

代码:


/*=============================================================================  
#  
#      Author: liangshu - cbam   
#  
#      QQ : 756029571   
#  
#      School : 哈尔滨理工大学   
#  
#      Last modified: 2015-11-17 20:11 
#  
#     Filename: H.cpp  
#  
#     Description:   
#        The people who are crazy enough to think they can change the world, are the ones who do !   
=============================================================================*/ 

#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
const int INF = 30;
int F[INF];
int r[INF];
void init()
{
    for(int i=0; i<=30; i++)
    {
        F[i]=i;
        r[i]=1;
    }
}


//int Find(int x)
//{
//    if(x!=F[x])
//       F[x]=Find(F[x]);
//       return F[x];
//}
int Find(int x)
{
    int r=x;
    while(r!=F[r])
    {
        r=F[r];
    }
    int k=x;
    while(k!=r)
    {
        int t=F[k];
        F[k]=r;
        k=t;
    }
    return r;
}
void union_set(int u,int v)
{
    int tx=Find(u);
    int ty=Find(v);
    if(r[tx]>r[ty])
        F[ty]=tx;
    else if(r[tx]<r[ty])
        F[tx]=ty;
    else
    {
        F[tx]=ty;
        r[ty]++;
    }
}

int main()
{
    int t;cin>>t;
    while(t--){
        init();
        int flag = 1;
        int in[100], out[100];
        memset(in, 0, sizeof(in));
        memset(out, 0, sizeof(out));
        int n;cin>>n;
        for(int i = 0; i < n; i++){
            char cnt[10008];scanf("%s", cnt);
            int k = cnt[0] - 'a';
            in[k]++;
            int t = cnt[strlen(cnt) - 1] - 'a';
            out[t]++;
            union_set(k, t);
        }
        int num = 0;
        for(int i = 0; i < 27; i++){
            if(in[i] && F[i] == i){
                num++;
            }
            else if(out[i] && F[i] == i){
                num++;
            }
        }
        if(num != 1){
            flag = 0;
        }
        int co1 = 0, co2 = 0;
        for(int i = 0; i < 27; i++){
            if(in[i] != out[i]){
                if(in[i] == out[i] + 1){
                    co1++;
                }
                else if(in[i] == out[i] - 1){
                    co2++;
                }
                else{
                    flag = 0;
                    break;
                }
            }
        }
       // cout<<co1<<" "<<co2<<" "<<flag<<endl;
       if(co1 + co2 != 2 && co1 + co2 != 0){
        flag  = 0;
       }
        if(flag){
            cout<<"Ordering is possible.\n";
        }
        else{
            cout<<"The door cannot be opened.\n";
        }

    }
    return 0;
}
/*
34
2
acm
ibm
3
acm
malform
mouse
2
ok
ok
*/


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值