HDU 1116 &&POJ 1386 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 wordmotorola”. 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.

题意:就不多说了,就是欧拉回路;

不过本题还是有点区别,因为这个题如果拿节点做,你会发现节点最多只有26个,爽!

所以本题可以这样:

欧拉回路和欧拉通路的判定可以总结为如下:

1)所有的点联通

2)欧拉回路中所有点的入度和出度一样。

3)欧拉通路中起点的入度 - 出度 = 1,终点的 初度 - 入度 = 1, 其他的所有点入度 = 出度;

无向图存在欧拉回路条件

  一个无向图存在欧拉回路,当且仅当该图所有顶点度数都是偶数。

有向图存在欧拉回路条件

  一个有向图存在欧拉回路,且所有顶点的入度等于出度

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn=30;

char str[1005];
int out[maxn],in[maxn],f[maxn];
int vis[maxn],p[maxn];

int find(int x)
{
    if(f[x]!=x)
        f[x]=find(f[x]);
    return f[x];
}

void merger(int x,int y)
{
    int a=find(x);
    int b=find(y);
    if(a!=b)f[a]=b;
}

int main()
{
    int t,i,j;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        getchar();
        memset(vis,0,sizeof(vis));
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        for(i=0;i<26;i++)
        {
            f[i]=i;
        }
        while(n--)
        {
            scanf("%s",str);
            int len=strlen(str);
            int u=str[0]-‘a‘;
            int v=str[len-1]-‘a‘;
            merger(u,v);
            in[v]++;
            out[u]++;
            vis[u]=vis[v]=1;
        }
        int cnt=0;
        for(i=0;i<26;i++)
            f[i]=find(i);
        for(i=0;i<26;i++)
        {
            if(vis[i]&&f[i]==i)
            {
                cnt++;
            }
        }
        if(cnt>1)   //图是否连通
        {
            printf("The door cannot be opened.\n");
           // printf("hehe\n");
            continue;
        }
        cnt=0;
        for(i=0;i<26;i++)
            if(vis[i]&&out[i]!=in[i])
            {
                p[cnt++]=i;
            }
        if(cnt==0)
        {
            printf("Ordering is possible.\n");
            continue;
        }
        if(cnt==2)
        {
            if(out[p[0]]-in[p[0]]==1&&in[p[1]]-out[p[1]]==1||in[p[0]]-out[p[0]]==1&&out[p[1]]-in[p[1]]==1)
            {
                printf("Ordering is possible.\n");
                continue;
            }
        }
        printf("The door cannot be opened.\n");

    }
    return 0;
}
时间: 2024-10-29 06:38:36

HDU 1116 &&POJ 1386 Play on Words(欧拉回路)的相关文章

HDU 1116 || POJ 1386 || ZOJ 2016 Play on Words (欧拉回路+并查集)

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

HDU 1116 &amp;&amp; POJ 1386 Play on Words(欧拉路径)

按字母来建边,每个单词的首字母和尾字母加边.先判断是否连通,然后判断每个字母的入度和出度不能出现差的绝对值大于2,然后入度和出度差的绝对值为1的不能超过两个.就可以形成欧拉路径 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include

POj 1386 Play on words 欧拉回路/通路,图的连通性判断

题目链接: #include<iostream> #include<cstdio> #include<cstring> #define M 28 using namespace std; int fa[M]; int Find(int x) { return x==fa[x]?x:fa[x]=Find(fa[x]); } int main() { int T,n; char str[1005]; int in_degree[M],out_degree[M]; int v

poj 1386 Play on Words(有向图欧拉回路)

1 /* 2 题意:单词拼接,前一个单词的末尾字母和后一个单词的开头字母相同 3 思路:将一个单词的开头和末尾单词分别做两个点并建一条有向边!然后判断是否存在欧拉回路或者欧拉路 4 5 再次强调有向图欧拉路或欧拉回路的判定方法: 6 (1)有向图G为欧拉图(存在欧拉回路),当且仅当G的基图连通,且所有顶点的入度等于出度. 7 (2)有向图G为半欧拉图(存在欧拉道路),当且仅当G的基图连通,且存在顶点u的入度比出度大1.v的入度比出度小1, 8 其它所有顶点的入度等于出度(顶点u,v的个数必须都是

POJ 1386 Play on Words(欧拉图的判断)

Play on Words Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11838   Accepted: 4048 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 ther

poj 1386 欧拉路径

1 /* 2 题意:给出N个单词,一个单词的头字母和另一个单词的尾字母相同则可以相连,问这N个单词是否能完全相连成一行 3 4 题解:求欧拉路径 5 首先以每个单词的首字母和尾字母为点并且连边,然后用DFS求该图是否连通,然后根据点的入度和出度判断是否存在 6 欧拉路径或者欧拉回路(存在回路也是符合要求的) 7 */ 8 #include <cstdio> 9 #include <cstring> 10 11 int map[30][30]; 12 int in[30],out[3

POJ 1386 Play on Words

欧拉回路问题. 题意是说给你一些字符串,类似于成语接龙,上一个字符串尾字母必须和下一个字符串首字母相同. 把所有字符串连成一串. 根据定理判断欧拉通路,然后DFS判连通(并查集也可) 没注意题意 字符串开了str[100] 结果RE.结果字符串最长有1000.改了就AC了. #include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm>

HDU 1087 &amp;&amp; POJ 2533(DP,最长上升子序列).

~~~~ 两道题的意思差不多,HDU上是求最长上升子序列的和,而POJ上就的是其长度. 貌似还有用二分写的nlogn的算法,不过这俩题n^2就可以过嘛.. ~~~~ 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1087 http://poj.org/problem?id=2533 ~~~~ HDU1087: #include<cstdio> #include<cstring> #include<algorithm> #

Eight hdu 1043 poj 1077

Description The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all packed into a 4 by 4 frame with one tile mis