Play on Words uva DFS+欧拉回路

Play on Words

Some of the secret doorscontain a very interesting word puzzle. The team of archaeologists has tosolve 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 largenumber of magnetic plates on every door. Every plate has one word written onit. The plates must be arranged into a sequence in such a way that every wordbegins with the same letter as the previous word ends. For example, the word``acm'' can be followed by the word ``motorola''. Your task is towrite a computer program that will read the list of words and determine whetherit is possible to arrange all of the plates in a sequence (according tothe given rule) and consequently to open the door.

Input Specification

The inputconsists of T test cases. The number of them (T) is givenon the first line of the input file. Each test case begins with a linecontaining a single integer number Nthat indicates the numberof plates (1 <= N <= 100000). Then exactly Nlinesfollow, each containing a single word. Each word contains at least two andat most 1000 lowercase characters, that means only letters 'a'through 'z' will appear in the word. The same word mayappear several times in the list.

Output Specification

Yourprogram has to determine whether it is possible to arrange all the plates ina sequence such that the first letter of each word is equal to the lastletter of the previous word. All the plates from the list must be used, eachexactly once. The words mentioned several times must be used that number oftimes.

If there exists such anordering of plates, your program should print the sentence "Ordering is possible.". Otherwise, output the sentence "The door cannot be opened.".

Sample Input

32acmibm3acmmalformmouse2okok

Output for the SampleInput

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

解决方案:一看就知道用欧拉回路。

欧拉回路:

1) 当为无向图时,dfs可以解决问题;

2) 当是有向图时,首先应该是判断每个点入度和出度是否相等,若全部的点的都相等,则无论从那个点开始都能连成欧拉回路。若不是,则只能有两个点的入度和出度不等,且a 起始点:入度+1=出度; b 末点:出度+1=入度;最后判断该图是否连通即可。

代码:

#include<iostream>
#include<cstring>
#include<vector>
#include<cstdio>
using namespace std;
int Map[26][26];
int in[26],out[26];
bool vis[26];
int T,N;
void dfs(int k)
{
    vis[k]=true;
    for(int i=0;i<26;i++)
    {
        if(Map[k][i]&&!vis[i])
            dfs(i);
    }
}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&N);
        char C[1100];
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        memset(Map,0,sizeof(Map));
        for(int i=0;i<N;i++)
        {
            scanf("%s",C);
            int len=strlen(C);
            ++Map[C[0]-'a'][C[len-1]-'a'];
            ++out[C[0]-'a'];
            ++in[C[len-1]-'a'];
        }
        int a=0,b=0;
        bool flag=true;
        for(int i=0;i<26;i++)
        {
            if(in[i]!=out[i])
            {
                if(in[i]==out[i]+1) a++;
                else if(in[i]+1==out[i]) b++;
                else {flag=false;break;}
            }
        }
        if(a&&b&&a+b>2)  flag=false;
        if(flag)
        {
            memset(vis,false,sizeof(vis));
            for(int i=0;i<26;i++)
            if(out[i]) {dfs(i);break;}
            bool flag1=true;
            for(int i=0;i<26;i++)
            {
                if(out[i]&&!vis[i]) {flag1=false;break;}
                if(in[i]&&!vis[i]) {flag1=false;break;}
            }
            if(flag1)
            {
                cout<<"Ordering is possible.\n";
            }
            else {cout<<"The door cannot be opened.\n";}
        }
        else {
            cout<<"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、付费专栏及课程。

余额充值