Play on Words(欧拉回路+并查集)

Play on Words
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 11628 Accepted: 3980
Description

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 wordmotorola”. 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.
Input

The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing a single integer number Nthat indicates the number of plates (1 <= N <= 100000). Then exactly Nlines follow, each containing a single word. Each word contains at least two and at most 1000 lowercase characters, that means only letters ‘a’ through ‘z’ will appear in the word. The same word may appear several times in the list.
Output

Your program has to determine whether it is possible to arrange all the plates in a sequence such that the first letter of each word is equal to the last letter of the previous word. All the plates from the list must be used, each exactly once. The words mentioned several times must be used that number of times.
If there exists such an ordering of plates, your program should print the sentence “Ordering is possible.”. Otherwise, output the sentence “The door cannot be opened.”.
Sample Input

3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok
Sample Output

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

Source
Central Europe 1999

题意:
给你n个单词,请问能不能将这些单词重新排序后,使所有的单词满足前一个单词的最后一个字母和最后一个单词的第一个字母相同。能则输出:Ordering is possible.,否则输出:The door cannot be opened.

分析:
这题很明显是一个图论的题目。主要是怎么建图,起初我想的是在可以相连的两个单词之间建一条边,每个单词按照出现的先后顺序做上标记。然后再利用判断是否存在欧拉通路的思想去做(关于欧拉回路的讲解可看另一篇博客)。后来看了题解之后,立刻就觉得自己蠢了。。
我们在每个单词内部建边,从单词的首字母指向最后一个字母。又由于所有单词都是由小写字母组成,所以我们可以很方便的计入每个字母的入度和出度。
(1)读入每个单词时,单词首字母代表的顶点出度加1,尾字母的入度加1.
(2)统计所有的入度出度时,还要用一个v[i]数组来记录每个字母是否出现。
(3)在判断满足欧拉通路的条件后,还要判断这个出现的字母是否连通。而判断是否连通并查集是很好的方法。

一个连通图存在欧拉通路的条件是:所有顶点的入度和出度相等(欧拉回路),或者处两个顶点外,所有顶点的入度和出度相等,并且这两个点,一个出度减入读位1,一个出度减入度位-1
在下面的代码中用o_id[i]表示i点出度与入度的差值。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int o_id[30],p[30];
int v[30];

int find_(int x)
{
    return x==p[x]?x:p[x]=find_(p[x]);
}

int main()
{
    int T,n,len;
    char word[1010];
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        memset(v,0,sizeof(v));
        memset(o_id,0,sizeof(o_id));
        for(int i=0;i<26;i++)
            p[i]=i;
        int x,y,xx,yy;
        for(int i=0;i<n;i++)
        {
            scanf("%s",word);
            len=strlen(word);
            x=word[0]-'a';
            y=word[len-1]-'a';
            o_id[x]++;o_id[y]--;
            v[x]=1;v[y]=1;
            xx=find_(x);yy=find_(y);
            if(xx!=yy)
                p[xx]=yy;
        }
        bool flag = true;
        int num1=0,num2=0;
        for(int i=0;i<26;i++)
        {
            if(!v[i])continue;
            if(!o_id[i]) continue;
            if(o_id[i]>=2 || o_id[i]<=-2)
            {
                flag=false;
                break;
            }
            if(num1>1 || num2>1)
            {
                flag=false;
                break;
            }
            if(o_id[i]==1)
                num1++;
            if(o_id[i]==-1)
                num2++;
        }
        if(flag)
        {
            if((num1==0 && num2==0) || (num1==1 && num2==1))
            {
                int j;
                for(j=0;j<26;j++)
                {
                    if(!v[j]) continue;
                    xx = find_(j);
                    break;
                }
                for(j;j<26;j++)
                {
                    if(!v[j]) continue;
                    if(find_(j)!=xx)
                    {
                        flag=false;
                        break;
                    }
                }
            }
            else
                flag=false;
        }
        if(flag)
            printf("Ordering is possible.\n");
        else
            printf("The door cannot be opened.\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值