hdu oj 1116 Play on Words(并查集+欧拉路径)

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.

题目大意:给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思。但是如果有多个重复的单词时,也必须满足这样的条件才能算YES。否则都是不可能的情况。 

解题思路: 欧拉路的基本题。只要知道就可以做出来了。 

关于欧拉回路和欧拉路径 定义:

 欧拉回路:每条边恰好只走一次,并能回到出发点的路径 

欧拉路径:经过每一条边一次,但是不要求回到起始点

 ①首先看欧拉回路存在性的判定: 

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

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

 三.混合图欧拉回路  (做这道题不用了解这一条) 

 ②.欧拉路径存在性的判定 

一。无向图 一个无向图存在欧拉路径,当且仅当   该图所有顶点的度数为偶数   或者  除了两个度数为奇数外其余的全是偶数。

 二。有向图 一个有向图存在欧拉路径,当且仅当 该图所有顶点的度数为零     或者 一个顶点的度数为1,另一个度数为-1,其他顶点的度数为0。

  

所以这道题的大题思路就是:

 1.并查集判断连通

 2.将每个单词取出首字母和尾字母,转换为一条边,然后加入对应的连通分量中。如果这个字母出现过,visit数组标记为true。同时起点出度加1,终点入度加1. 

3.判断一下:

 1)这个图必须是连通的,即根结点只有一个。如果不是,直接结束本次算法。

 2)如果这个图是连通的,判断每个结点的入度和出度情况。         

如果这个图是欧拉路,则每个顶点的出度等于入度。即out[i] = in[i] 如果这个图是半欧拉图,则起点的出度比入度大1,终点的入度比出度大1.其余顶点的出度等于入度。 如果满足上述条件,就可以将所有单词链接起来,否则不能。 当然,在判断出度入度的时候还有一点需要注意,那就是除了起点终点以外的顶点,出度必须等于入度(出度入度可以同时为2,即环),但是起点和终点必须保证出度和入度之差为1。

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#define MAX 26
using namespace std;
int out[MAX],in[MAX],flag[MAX],p[MAX],pre[MAX];


int Find(int x)
{
    int r=x;
    while (pre[r]!=r)
        r=pre[r];
    return r;
}


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


int main ()
{
    int n,i,a,b,k,num,t;
    char str[1002];
    scanf("%d",&t);
    while (t--)
    {
        scanf("%d",&n);
        for(i=0;i<26;i++)//初始化26个字母
            pre[i]=i;
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        memset(flag,0,sizeof(flag));
        for(i=1;i<=n;i++)
        {
            cin>>str;
            a=str[0]-'a';     //用0,1,2...26分别表示26个英文字母,a表示各个单词首字母
            b=str[strlen(str)-1]-'a';//b表示每个单词的尾字母
            join(a,b);     //将a,b捆绑
            in[b]++;        //入度加1
            out[a]++;       //出度加1
            flag[a]=flag[b]=1;  //标记该字母已存在
        }
        num=0;
        for(i=0;i<26;i++)
        {
            pre[i]=Find(i);
            if(flag[i]==1 && pre[i]==i)  //如果不同源,则非同根
                num++;  //看看连起来了几个串
        }
        if(num>1)  //num大于1   表示连不起来1个
        {
            cout<<"The door cannot be opened."<<endl;
            continue;
        }
        k=0;
        for(i=0;i<26;i++)
        {
            if(flag[i]==1 && in[i]!=out[i])  //计算存在字母的入度及出度不相等的节点数
                p[k++]=i;
        }
        if(k==0)  //不存在出度不等于入度的节点,是欧拉回路
        {
            cout<<"Ordering is possible."<<endl;
            continue;
        }
        if(k==2 && ((out[p[0]]-in[p[0]]==1 && in[p[1]]-out[p[1]]==1)      //入度与出度不相等的节点个数为2
        || (out[p[1]]-in[p[1]]==1 && in[p[0]]-out[p[0]]==1)) )            //且起点出度减入度等于1或者终点入度减出度等于1,则是欧拉回路
        {
            cout<<"Ordering is possible."<<endl;
            continue;
        }
        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、付费专栏及课程。

余额充值