HDU - 1116 Play on Words(建立模型+歐拉路徑)

題目鏈接https://vjudge.net/contest/365311#problem/G
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.

翻譯
n個僅有小寫字母組成的英文單詞。是否能有一個合適的順序,使得相鄰兩個單詞,前一個單詞的末字母等於後一個單詞的首字母
若能達到要求輸出Ordering is possible.
否則輸出The door cannot be opened.

模型
以26個字母作爲頂點,對於每一個單詞,如果它的首字母爲c1,末字母爲c2,那麼從c1向c2連一條有向邊
問題轉化爲在圖中尋找一條不重複地經過每一條邊的路徑,即歐拉路徑
在這裏插入圖片描述

歐拉回路

圖G中經過每條邊一次並且僅一次的迴路。

歐拉路徑

圖G中經過每條邊一次並且僅一次的路徑。

歐拉圖:存在歐拉回路
半歐拉圖:存在歐拉路徑,但不存在歐拉回路

判斷歐拉圖或半歐拉圖的方法

無向圖

歐拉圖:G爲連通圖,且所有的頂點的度爲偶數
半歐拉圖:G爲連通圖,除了2個頂點的度爲奇數,其它所有頂點的度爲偶數所有頂點的度都爲偶數

有向圖

歐拉圖:G的基圖連通,且所有頂點的入度等於出度
半歐拉圖:G的基圖連通,且存在u的入度比出度大1,v的入度比出度小1,其它所有頂點的入度等於出度所有頂點的入度等於出度


基圖:忽略有向圖所有邊的方向,得到的無向圖稱爲該有向圖的基圖

算法
判斷圖是否連通=並查集(是否只有一個祖先)

代碼:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=30;
bool book[N];
int in[N],out[N],f[N];
void init()
{
    memset(book,false,sizeof(book));
    memset(in,0,sizeof(in));
    memset(out,0,sizeof(out));
    for(int i=0; i<26; i++)
        f[i]=i;
}
int getf(int u)
{
    if(u!=f[u])
        f[u]=getf(f[u]);
    return f[u];
}
void gether(int u,int v)/*並查集判斷連通性*/
{
    int t1=getf(u);
    int t2=getf(v);
    if(t1!=t2)
        f[t2]=t1;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        init();
        int n;
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            char mp[1010];
            scanf("%s",mp);
            int len=strlen(mp);
            int fir=mp[0]-'a',sec=mp[len-1]-'a';
            book[fir]=true;
            book[sec]=true;/*標記這個字母出現過*/
            out[fir]++;/*出度++*/
            in[sec]++;/*入度++*/
            gether(fir,sec);
        }
        int root=0;
        for(int i=0; i<26; i++)
        {
            if(book[i]&&f[i]==i)/*判斷祖先的個數*/
                root++;
        }
        if(root>1)
            printf("The door cannot be opened.\n");
        else
        {
            int sign[5],tot=0;
            int in1,in2,out1,out2;
            for(int i=0; i<26; i++)
            {
                if(book[i]&&in[i]!=out[i])
                    sign[tot++]=i;/*tot記錄入度不等於出度的點的個數*/
            }
            if(tot==0)/*所有點的入度等於出度*/
                printf("Ordering is possible.\n");
            else if(tot==2)
            {
                if(in[sign[0]]-out[sign[0]]==1&&in[sign[1]]-out[sign[1]]==-1||in[sign[0]]-out[sign[0]]==-1&&in[sign[1]]-out[sign[1]]==1)
                    printf("Ordering is possible.\n");
                else
                    printf("The door cannot be opened.\n");
            }
            else
                printf("The door cannot be opened.\n");
        }
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章