HDU1116 欧拉回路 解题报告

来源:互联网 发布:广州网络女装批发市场 编辑:程序博客网 时间:2024/05/17 06:36

Play on Words

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.

【解题报告】
题意 : 有很多门,每个门上有很多磁盘,每个盘上一个单词,必须重新排列磁盘使得每个单词的第一个字母与前一个单词的最后一个字母相同。给你一组单词问能不能排成上述形式。
思路 :把每个单词看成有首字母指向尾字母的有向边,每个字母看成一个点,题中要求等效于判断图中是否存在一条路径经过每一条一次且仅一次,就是有向欧拉通路。统计个顶点的出入度,如果每个点的出入度都相同,那就是欧拉回路,如果有两个奇数度,那就是欧拉通路,除此之外,都不能满足要求。还有别忘了判断是否连通,此时用到并查集,图中所有的边(u,v),如果u!=v且属于不同的连通分量,就合并。

代码如下:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std ;int n,t;char word[1001] ;int fa[27],out[27],in[27],vis[30];int find(int x){    return (fa[x]!=x)?find(fa[x]):fa[x];}void mergee(int a,int b){    if(find(a)!=find(b)) fa[find(a)]=find(b);}int main(){    for(scanf("%d",&t);t;--t)    {        scanf("%d",&n);        memset(out,0,sizeof(out));        memset(in,0,sizeof(in));        memset(vis,0,sizeof(vis));        for(int i=0;i<26;i++) fa[i]=i;        while(n--)        {            scanf("%s",word);            int u=word[0]-'a';            int v=word[strlen(word)-1]-'a';            mergee(u,v);            out[u]++;            in[v]++;            vis[u]=vis[v]=1;        }        int cnt=0,cnt1=0,cnt2=0;        for(int i=0;i<26;i++)        if(vis[i]&&fa[i]==i) cnt++;        if(cnt>1)        {            puts("The door cannot be opened.");            continue;        }        bool flag=true;        for(int i=0;i<26;i++)        {            if(vis[i]&&out[i]!=in[i])            {                if(out[i]-in[i]==1)                {                    cnt1++;                    if(cnt1>1)                    {                        flag=false;                        break;                    }                }                else if(in[i]-out[i]==1)                {                    cnt2++;                    if(cnt2>1)                    {                        flag=false;                        break;                    }                }                else                {                    flag=false;                    break;                }            }        }        if(!flag) puts("The door cannot be opened.");        else puts("Ordering is possible.");    }    return 0;}