poj 1386

Play on Words
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 11312   Accepted: 3862

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, 另一个的入度比出度大。

如果奇点数不存在的话, 则可以从任意点出发,最终一定会回到该点(成为欧拉回路)。

题目给的单词量比较大,但是有用的只有首和尾的字母,所以只需要存首尾字母就可以了。 

欧拉道路还有关键的一部是判断这一个图是连通的, 并且只有一个一个连通分支。

代码:

#include<cstdio>
#include<cstring>
using namespace std;
#define N 27
struct node{
    int in,out;
}degree[N];
int fa[N],rank[N],mem[N],vis[N],top,t,n; 
char str[1001];
int find(int x){
    return fa[x]==x?x:fa[x]=find(fa[x]);
}
int main(){
    scanf("%d",&t);
    while(t--){
        for(int i=1;i<=26;i++){
            fa[i]=i,rank[i]=0;
        }
        memset(degree,0,sizeof degree);
        memset(vis,0,sizeof vis);
        top=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            memset(str,0,sizeof str);
            scanf("%s",str);
            int a=str[0]-'a'+1,b=str[strlen(str)-1]-'a'+1;
            if(!vis[a]){
                vis[a]=1;
                mem[++top]=a;
            }
            if(!vis[b]){
                vis[b]=1;
                mem[++top]=b;
            }
            degree[a].out++;degree[b].in++;
            a=find(a);b=find(b);
            if(a!=b){
                if(rank[a]<rank[b]) fa[a]=b;
                else{
                    fa[b]=a;
                    if(rank[a]==rank[b]) rank[a]++;
                }
            }    
        }
        int tmp=find(mem[1]),flag=0;
        for(int i=2;i<=top;i++){
            if(find(mem[i])!=tmp){
                flag=1;break;
            }
        }
        if(flag){
            printf("The door cannot be opened.
");
            continue;
        }
        int sum=0,flag1=0,flag2=0,ok=1;
        for(int i=1;i<=top&&sum<=2&&ok;i++){
            if(degree[mem[i]].in!=degree[mem[i]].out){
                sum++;
                if(degree[mem[i]].in==degree[mem[i]].out+1) flag1++;
                else if(degree[mem[i]].in==degree[mem[i]].out-1) flag2++;
                else ok=0;
            }    
        }
         if(ok){
            if(flag1==1&&flag2==1 || flag1==0&&flag2==0) 
               printf("Ordering is possible.
");
            else 
               printf("The door cannot be opened.
");
         }
         else 
            printf("The door cannot be opened.
");       
    }
    return 0;
}
原文地址:https://www.cnblogs.com/shenben/p/5564663.html