1. 程式人生 > >POJ 1386 Play on Words

POJ 1386 Play on Words

can all europe bubuko post .com mos task only

                      Play on Words
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 13281 Accepted: 4461

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

Central Europe 1999

題意:給出n個單詞,如果一個單詞的尾和另一個單詞的頭字符相等,那麽可以相連,問這n個單詞是否可以排成一列。

思路:有向圖判斷是否存在歐拉通路

吐槽:zz的我竟然應為把這句話忘了:技術分享圖片而調了一個小時。。。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
char s[10000];
int fa[30];
int t,n,tot,sum,sum1,sum2;
int vis[30],num[30],into[30],out[30];
int find(int x){
    if(fa[x]==x)    return fa[x];
    return fa[x]=find(fa[x]);
}
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        tot=0;sum=0;sum1=0;sum2=0;
        memset(vis,0,sizeof(vis));
        memset(num,0,sizeof(num));
        memset(out,0,sizeof(out));
        memset(into,0,sizeof(into));
        for(int i=1;i<=26;i++)    fa[i]=i;
        for(int i=1;i<=n;i++){
            scanf("%s",s);
            int x=s[0]-a+1;
            int y=s[strlen(s)-1]-a+1;
            out[x]++;   into[y]++;
            if(!vis[x])    vis[x]=1,num[++tot]=x;
            if(!vis[y])    vis[y]=1,num[++tot]=y;
            int dx=find(x); int dy=find(y);
            if(dx!=dy)    fa[dy]=dx;
        }
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=tot;i++){
            int now=find(num[i]);
            if(!vis[now]){ vis[now]=1;sum++; }
        }
        if(sum>1){ printf("The door cannot be opened.\n");continue; } sum=0;
        for(int i=1;i<=tot;i++)
            if(into[num[i]]!=out[num[i]])    sum++;
        if(sum==0){ printf("Ordering is possible.\n");continue; }
        for(int i=1;i<=tot;i++)
            if(into[num[i]]!=out[num[i]]){
                if(into[num[i]]==out[num[i]]-1)    sum1=1;
                else if(into[num[i]]-1==out[num[i]])    sum2=1;
            }
        if(sum==2&&sum1==1&&sum2==1){ printf("Ordering is possible.\n");continue; }    
        else printf("The door cannot be opened.\n");
    }
    return 0; 
}

POJ 1386 Play on Words