hdu 1116【欧拉通路+并查集】

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
 
  
32acmibm3acmmalformmouse2okok
 
Sample Output
 
  
The door cannot be opened.Ordering is possible.The door cannot be opened.

分析:每个单词只有首尾两个字母很关键,并且每个单词可以看成连接首尾两个字母的一条有向边(由首字母指向尾字母)。这

样每个测试数据中的一组单词可以构造成一个图:图中的顶点为26 个小写字母,每个单词为图中的一条边。构造好有向图后,题

目要判定是否可以经过重组使得每个单词第一个字母跟前一个单词最后一个字母相同,等效于判断图中是否存在一条路径经过每

条边一次且仅一次,这就是有向欧拉通路。

题解:

(1) 读入每个单词时,因为每个单词相当于一条从首字母指向尾字母的边,所以对单词首字母对应的顶点,出度加1;尾字母对应

的顶点,入度加1;

(2) 26 个顶点的入度和出度都统计完毕后,根据各顶点的出度、入度关系来判断是否存在欧拉通路,但要注意排除每个单词的首

尾字母中没有出现过的字母。在下面的代码中,用vis[ ]数组来表示每个字母是否在单词的首尾中出现。

(3) 判断完以后,还得判断整个有向图的基图(即不考虑边的方向)是否连通,判断连通最好的方法是使用并查集。。。

代码实现:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#define MAXN 100001
#define INF 99999999
using namespace std;
int t,n;
int od[26],id[26];
int vis[26];//表示每个字母是否在单词的首尾中出现
int pre[26];
char word[1005];
struct edge{
    int u,v;//边的顶点
}edges[MAXN];
void init(){
    for(int i=0;i<26;i++)
        pre[i]=-1;
}
int Find(int x){//查找并返回节点x所属集合的根节点
    int r;
    for(r=x;pre[r]>=0;r=pre[r]);
    while(r!=x){//路径压缩
        int tmp=pre[x];
        pre[x]=r;
        x=tmp;
    }
    return r;
}
void join(int x,int y){//将两个不同集合的元素合并,使两个集合中任两个元素都连通
    int fx=Find(x);
    int fy=Find(y);
    if(fx!=fy){
        pre[fx]=fy;
    }
}
bool bconnect(){//判断有向图的基图是否连通
    int u,v,i;
    init();
    for(i=0;i<n;i++){
        u=edges[i].u;
        v=edges[i].v;
        if(u!=v&&Find(u)!=Find(v)){
            join(u,v);
        }
    }
    int first=-1;//第一个vis[i]不为0的顶点
    for(i=0;i<26;i++){
        if(vis[i]==0) continue;
        if(first==-1) first=i;
        else if(Find(i)!=Find(first)) break;//不连通
    }
    if(i<26) return false;//不连通
    else return true;//连通
}
int main()
{
    int u,v;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        memset(vis,0,sizeof vis);
        memset(od,0,sizeof od);
        memset(id,0,sizeof id);
        for(int i=0;i<n;i++){
            scanf("%s",word);
            u=word[0]-'a';
            edges[i].u=u;
            v=word[strlen(word)-1]-'a';
            edges[i].v=v;
            od[u]++;
            id[v]++;
            vis[u]=vis[v]=1;
        }
        bool flag=true;//是否存在欧拉通路
        int one=0;//出度比入度多1的顶点个数
        int none=0;//出度比入度少1的顶点个数
        for(int i=0;i<26;i++){
            if(!vis[i]) continue;
            if(od[i]-id[i]>=2||id[i]-od[i]>=2){
                flag=false;
                break;
            }
            if(od[i]==0&&id[i]==0){
                flag=false;
                break;
            }
            if(od[i]-id[i]==1){
                one++;
                if(one>1){
                    flag=false;
                    break;
                }
            }
            if(id[i]-od[i]==1){
                none++;
                if(none>1){
                    flag=false;
                    break;
                }
            }
        }
        if(one!=none) flag=false;
        if(bconnect()==false) flag=false;
        if(flag) printf("Ordering is possible.\n");
        else printf("The door cannot be opened.\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37867156/article/details/80587589