首页 > 代码库 > HDU 1116 Play on Words (歐拉迴路 + 并查集)

HDU 1116 Play on Words (歐拉迴路 + 并查集)

鏈接: 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.


解題思路:

>>>1.并查集判断连通

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

>>>3.判断一下:

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


代碼:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define MAXN 35
#define RST(N)memset(N, 0, sizeof(N))
using namespace std;

char str[1010];
int cas, n, len; // 樣例個數、單詞個數、單詞長度
int In[MAXN], Out[MAXN]; //計算每個節點的入度和出度
int father[MAXN], vis[MAXN];
int In_Num, Out_Num, root;  //记录入度出度不相等顶点个数, 根结点个数
bool flag, flag1;  //判断连通性、判断入度和出度是否是1或者0
int front, rear;   //转化为边

void Init()
{
    for(int i=1; i<MAXN; i++) father[i] = i;
    RST(vis), RST(In), RST(Out);
    root = In_Num = Out_Num = 0;
    flag = true, flag1 = true;
}

int Find_Set(int x)
{
    return x == father[x] ? x : Find_Set(father[x]);
}

void Union_Set(int x, int y)
{
    int Fx = Find_Set(x);
    int Fy = Find_Set(y);
    if(Fx != Fy) father[Fy] = Fx;
}

void read_data()
{
    scanf("%d", &n);
    for(int i=0; i<n; i++) {
        scanf("%s", str);
        len = strlen(str);
        front = str[0] - 'a' + 1, rear = str[len-1] - 'a' + 1;
        vis[front] = 1, vis[rear] = 1;
        In[rear]++, Out[front]++;
        Union_Set(front, rear);
    }
}

void solve()
{
    for(int i=1; i<MAXN; i++) {
        if(vis[i]) {
            if(father[i] == i) root++;
            if(In[i] != Out[i]) {
                if(In[i] - Out[i] == 1) In_Num++;
                else if(Out[i] - In[i] == 1) Out_Num++;
                else flag1 = false;
            }
        }
        if(root > 1) { flag = false; break; }
    }
    if(flag && flag1 && ((In_Num==0 && Out_Num==0) || (In_Num==1 && Out_Num==1))) {
        puts("Ordering is possible.");
    }else puts("The door cannot be opened.");
}

int main()
{
    scanf("%d", &cas);
    while(cas--) {
        Init(), read_data(), solve();
    }
    return 0;
}



HDU 1116 Play on Words (歐拉迴路 + 并查集)