HDU 1116&UVA10129 Play on Words 欧拉路径

Play on Words

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7004    Accepted Submission(s): 2366


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.


题意:给一堆单词,一个单词可以连接另一个单词,要求上一个单词的最后一个字母和下一个单词点的第一个字母相同。问是否能构成一条链。

暴力DFS是不科学的,复杂度太高,会TLE,看到别人T的。然后这道题具体写法是把图转换成欧拉路径。即由一个单词和一个单词的连接转换成每个单词头和尾相连,看是否满足欧拉路径的条件,能满足就能构成一条链,不能就不能了。

为什么可以行,感觉不好说,可以画图模拟一下。

这是由单词之间关系构成的图,每个单词是一个点,然后找一条经过每个点且每个点只能经过一次,这是哈密顿路径,是非常难求解的。

然后可以转换一下,

每个单词由头字母和尾字母构成,把它们练成一条边,这样每条边就是一个单词,这样问题就可以转换成在不重复访问同一条边的前提下是否遍历图中每一条边。

这样就由哈密顿路径转换成欧拉路径,然后不用输出路径,简单判断下连通性和点的奇偶性就可以得出答案。



首先是几个概念:

欧拉回路 图中经过每条边一次并且仅一次的回路称作欧拉回路。

欧拉路径 图中经过每条边一次并且仅一次的路径称作欧拉路径。

欧拉图 存在欧拉回路的图称为欧拉图。

半欧拉图 存在欧拉路径但不存在欧拉回路的图称为半欧拉图。


然后就是判定

不管是这四种的哪几种,大前提是这个图必须是一个 连通图,否则什么都不是
满足前提过后;
(无向图)所有点的度数为偶数就是欧拉图
如果有2个点的度数为奇数就是半欧拉图


(有向图)所有点的入度==出度就是欧拉图

且存在顶点的入度比出度大1(终点)、的入度比出度小1(起点),其它所有顶点的入度等于出度。



这道题就是有向图的,就利用那个判定进行判定就行了,连通图的判定就随便并查集dfs bfs都行


输出路径就是另外写DFS了,好像有坑,还没写过,下一篇写。

CODE:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>

using namespace std;

const int N = 30;

int n;
int vis[N];    ///标记哪些字母的点会被访问
char s[1010];  ///输入用
int fa[N];     ///并查集用
int in[N];     ///入度
int out[N];    ///出度

int Find(int x)
{
    if(fa[x] != x)
        return fa[x] = Find(fa[x]);
    return x;
}

void Union(int x,int y)
{
    int tx = Find(x);
    int ty = Find(y);
    fa[ty] = tx;
}

void Init()       ///初始化
{
    for(int i = 0;i < N;i++)
    {
        fa[i] = i;
        vis[i] = in[i] = out[i] = 0;
    }
}

int main(void)
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        Init();
        scanf("%d",&n);
        while(n--)
        {
            scanf("%s",s);
            int len = strlen(s);
            vis[s[0]-'a'] = 1;
            vis[s[len-1]-'a'] = 1;
            in[s[0]-'a']++;
            out[s[len-1]-'a']++;
            fa[s[0]-'a'] = fa[s[len-1]-'a'] = Find(s[0]-'a');
        }
        int cnt_root = 0;     ///根节点个数
        for(int i = 0;i < 26;i++)
        {
            if(vis[i] && fa[i] == i)
                cnt_root++;
        }
        if(cnt_root > 1)      ///根节点个数多于一个则不可能是连通图
        {
            printf("The door cannot be opened.\n");
            continue;
        }
        int st=0,en=0,tmp=0;  ///起点个数,终点个数,其他各种类型的边(入度比出度大2之类的)
        for(int i = 0;i < 26;i++)
        {
            if(vis[i] && in[i] != out[i]){
            if(in[i] == out[i]-1)
                en++;
            else if(in[i]-1 == out[i])
                st++;
            else tmp++;
            }
        }
        if(tmp || st > 1 || en > 1)
        {
            printf("The door cannot be opened.\n");
        }
        else
        {
            printf("Ordering is possible.\n");
        }
    }
    return 0;
}



你可能感兴趣的:(欧拉回路)