hdu1116Play on Words(并查集+欧拉函数)

29 篇文章 4 订阅

Play on Words

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10098    Accepted Submission(s): 3463

http://acm.hdu.edu.cn/showproblem.php?pid=1116
 

Problem 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 word ``motorola''. 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.

 

题意:t组数据,每组n个单词。若单词a的最后一个字母和单词b的第一个字母相同,则可以连接。若这n个单词能连接在一起,输出“Ordering is possible”,否则,输出“The door cannot be opened.”。

 

思路:并查集+欧拉回路。

欧拉回路:在图中,经过所有的边一次且仅一次的回路就是欧拉回路。而经过所有的顶点一次且仅一次的回路叫做哈密顿回路。

现在我们考虑每个单词有用的成分只有首尾字母,那么我们把单词的首尾字母提取出来,抽象成两个顶点,而恰恰这两个顶点又是有了联系的,我们把它们放入一个集合。实际上,无论输入多少个单词,首尾字母中所出现的字母无外乎a,b,c……y,z等26个英文字母。所以反客为主,我们不用被动的根据输入的单词来变换,首先建立26个集合,将有联系的集合合并(join)则矣。当做完这个工作后,我们再来看看欧拉回路存在性的判定定理:

一、无向图
每个顶点的度数都是偶数,则存在欧拉回路。

二、有向图(所有边都是单向的)
每个节顶点的入度都等于出度,则存在欧拉回路。

有两个点入度出度相等,一个点入度比出度大一,另一个点出度比入度大一,其他点的入度与出度相等。

 三.混合图欧拉回路
  混合图欧拉回路用的是网络流。
  把该图的无向边随便定向,计算每个点的入度和出度。如果有某个点出入度之差为奇数,那么肯定不存在欧拉回路。因为欧拉回路要求每点入度 = 出度,也就是总度数为偶数,存在奇数度点必不能有欧拉回路。

 

代码:

#include<iostream>
#include<cstring>
#include<cmath>
#include<iomanip>
#include<algorithm>
#include<cstdio>
#define inf 0x3f3f3f3f
using namespace std;

//对于有向图来说就是所有点的入度与出度都相等或者
//存在一个点入度比出度大一存在另一个点出度比入度大一,其他点的入度与出度相等。

char s[1005];
int f[30];
int flag[30];  //标记字符
int in[30];  //入度
int out[30];  //出度
int l[30];  //记录入度出度不相等的点

int find(int x)
{
    if(f[x]!=x)
		f[x]=find(f[x]);
	return f[x];
	//return x==f[x]?x:f[x]=find(f[x]);  会超内存
}

void join(int x,int y)
{
    int fx=find(x);
    int fy=find(y);
    if(fx!=fy)
        f[fy]=fx;
}

int main()
{
    int t,n;
    cin>>t;
    while(t--)
    {
        memset(flag,0,sizeof(f));
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        for(int i=0;i<26;i++)
            f[i]=i;
        cin>>n;
        while(n--)
        {
            cin>>s;
            int len=strlen(s);
            int a=s[0]-'a';
            int b=s[len-1]-'a';
            join(a,b);
            in[b]++;
            out[a]++;
            flag[a]=flag[b]=1;
        }
        int num=0;
        for(int i=0;i<26;i++)
        {
            f[i]=find(i);
            if(flag[i]&&f[i]==i)  //连通分支个数
                num++;
        }
        if(num!=1)  //有多个连通分支,一定不行
            cout<<"The door cannot be opened."<<endl;
        else
        {
            int k=0;
            for(int i=0;i<26;i++)
            {
                if(flag[i]&&in[i]!=out[i])
                    l[k++]=i;
            }
            if(k==0)
                cout<<"Ordering is possible."<<endl;
            else if(k==2&&((in[l[0]]-out[l[0]]==1&&out[l[1]]-in[l[1]]==1)||
                    (in[l[1]]-out[l[1]]==1&&out[l[0]]-in[l[0]]==1)))
                cout<<"Ordering is possible."<<endl;
            else
                cout<<"The door cannot be opened."<<endl;
        }
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值