1. 程式人生 > >Play on Words——字母首尾相連

Play on Words——字母首尾相連

Play on Words
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 10138 Accepted: 3468

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.

題意:一扇門裝了密碼鎖,給出一些單詞,將這些單詞首尾相連,連線的部分兩個字母相同,即一個單詞的最後一個字母和一個單詞的第一個字母相同才能連在一起,這樣,如果把這些單詞都能夠連線起來,那麼就可以把門開啟,問能否把門開啟。

解析:首先,我們要知道這些單詞能否首尾相連,如果不能首尾相連,那麼自然也就不能開啟門。注意到,題目中給出的單詞只有第一個字母和最後一個字母有用,是這兩個字母之間的對接,那麼剩下的字母就像邊一樣連線了兩端的字母這兩個頂點,這樣我們就成功的把這個問題轉化成了圖。能不能把門開啟,就是看能不能將這些單詞首尾連成一條線,實際上就是求存不存在有向尤拉連通路徑。

有向尤拉連通路徑:設D是有向圖,D的基圖連通,並且D的每條邊經過且僅經過一次的有向路徑。

那麼我們就要判斷基圖連不連通,判斷每條邊經過且僅經過一次。

判斷每條邊經過且僅經過一次:看每個頂點的入度和出度,具體根據有向尤拉通路的定義來判斷。

判斷基圖連不連通,用並查集看每個頂點是不是都連通在一棵樹上,具體看並查集的相關知識點

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

int od[26],id[26];
int parent[26],bused[26];
int n,t,u,v;
char word[1006];

int Find(int x)
{
    int s = x;
    while(parent[s] >= 0)
        s = parent[s];
        //並查集的路徑壓縮
    while(s != x)
    {
        int tmp = parent[x];
        parent[x] = s;
        x = tmp;
    }
    return s;
}

void Union(int x,int y)
{
    int fx = Find(x);
    int fy = Find(y);
    if(fx != fy)
    {
        //並查集的加權法則,儘量使樹高降低
        int sum = parent[fx] + parent[fy];
        if(parent[fx] < parent[fy])
        {
            parent[fy] = fx;
            parent[fx] = sum;
        }
        else
        {
            parent[fx] = fy;
            parent[fy] = sum;
        }
    }
}

int main()
{
    int i,j,k,l;
    scanf("%d",&t);
    while(t--)
    {
        memset(od,0,sizeof(od));
        memset(id,0,sizeof(id));
        memset(parent,-1,sizeof(parent));
        memset(bused,0,sizeof(bused));
        scanf("%d",&n);
        bool falg = true;
        for(i = 0; i < n; i++)
        {
            scanf("%s",word);
            l = strlen(word);
            u = word[0] - 'a';
            v = word[l-1] - 'a';
            od[u]++;
            id[v]++;
            //說明該頂點出現過
            bused[u] = bused[v] = 1;
            //在輸入邊的時候進行並查集建樹
            Union(u,v);
        }
        int first = -1,num;
        //看是否連通
        for(i = 0; i < 26; i++)
        {
            if(bused[i] == 0)
                continue;
            if(first == -1)
            {
                first = i;
                num = Find(first);
            }
            if(num != Find(i))
            {
                falg = false;
                break;
            }
        }
        if(falg == false)
        {
            printf("The door cannot be opened.\n");
            continue;
        }
        //在連通的基礎上判斷每條邊是不是都經過一次並且僅經過一次
        int a = 0,b = 0;
        for(i = 0; i < 26; i++)
        {
            if(bused[i] == 0)
                continue;
            if(od[i] - id[i] == 1)
            {
                a++;
                if(a > 1)
                {
                    falg = false;
                    break;
                }
            }
            if(id[i] - od[i] == 1)
            {
                b++;
                if(b > 1)
                {
                    falg = false;
                    break;
                }
            }
            if(id[i]-od[i] > 1 || od[i]-id[i]>1)
            {
                falg = false;
                break;
            }
        }
        if(a != b)
            falg = false;
        if(falg == false)
        {
            printf("The door cannot be opened.\n");
        }
        else
            printf("Ordering is possible.\n");
    }
    return 0;
}