天天看點

POJ 1386 Play on Words(有向圖歐拉路徑并查集判定)

Play on Words

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 10068 Accepted: 3452

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.      

Source

跟成語接龍很相似,問這些單詞是否能夠接起來

有向圖判定歐拉回路 需要(圖是連通的) &&(所有點入度=出度 或 隻有兩個點 入度-出度=1 出度-入度=1) 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
using namespace std;
const int N = 100010;
int a[30],b[30],v[30];
int bin[30];
int findx(int x)
{
    int r=x;
    while(bin[r]!=r)
        r=bin[r];
    return r;
}
void merge(int x,int y)
{
    int fx,fy;
    fx=findx(x);
    fy=findx(y);
    if(fx!=fy)
        bin[fx]=fy;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(v,0,sizeof(v));
        for(int i=0; i<26; i++)
            bin[i]=i;
        for(int i=0; i<n; i++)
        {
            char s[1010];
            scanf("%s",s);
            int len=strlen(s);
            a[s[0]-'a']++;                 //出度
            b[s[len-1]-'a']++;             //入度
            v[s[0]-'a']=1;
            v[s[len-1]-'a']=1;
            merge((int)(s[0]-'a'),(int)(s[len-1]-'a'));
        }
        int flag=0,f;
        for(int i=0; i<26; i++)                  //判斷是否聯通圖
        {
            f=0;
            if(v[i])
            {
                for(int j=i+1; j<26; j++)
                {
                    if(v[j])
                    {
                        if(findx(i) != findx(j))
                        {
                            f=1;
                            break;
                        }
                    }
                }
                if(f) break;
            }
        }

        if(f)
        {
            printf("The door cannot be opened.\n");
            continue;
        }
        int s1=0,s2=0;
        for(int i=0; i<26; i++)              //判斷是否有歐拉回路
        {
            if(v[i])
            {
                if(a[i]!=b[i])
                {
                    s1++;
                    if(s1>2)
                    {
                        flag=1;
                        break;
                    }
                }
                else continue;
                if(a[i]-b[i]==1||b[i]-a[i]==1)
                {
                    s2++;
                    if(s2>2)
                    {
                        flag=1;
                        break;
                    }
                }
                else
                {
                    flag=1;
                    break;
                }
            }
        }
        if(flag) printf("The door cannot be opened.\n");
        else printf("Ordering is possible.\n");
    }
    return 0;
}