Play on Words

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

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.
 
 

题目解析:

输入一些英文单词,根据该单词的首尾字母,判断所有单词能不能连成一串,类似于成语接龙的意思。同样如果有多个重复的单词时,也必须满足这样的条件才能通过,否则都是不可能的情况。输入包括若干个案例,每个案例中最多有100000个单词。

思路解析:

该题所涉及的知识点主要是并查集和欧拉回路,那么什么是并查集呢?所谓的并查集就是指“并”和“查”的集合,具体大家可以结合HDU畅通工程来理解,当然,在那里会有相关的解析。(http://blog.csdn.net/caizi1991/article/details/8995793)现在我们来着重看一下欧拉回路的知识及其应用背景,那么,欧拉回路又是什么呢?它跟我们今天所做的“成语接龙”有什么联系呢?学过离散的同学都知道,欧拉回路是指,在图中,经过所有的边一次且仅一次的回路就是欧拉回路。而经过所有的顶点一次且仅一次的回路叫做哈密顿回路。现在我们考虑每个单词有用的成分只有首尾字母,那么我们把单词的首尾字母提取出来,抽象成两个顶点,而恰恰这两个顶点又是有了联系的,我们把它们放入一个集合。实际上,无论输入多少个单词,首尾字母中所出现的字母无外乎a,b,c……y,z等26个英文字母。所以反客为主,我们不用被动的根据输入的单词来变换,首先建立26个集合,将有联系的集合合并(merge)则矣。当做完这个工作后,我们再来看看欧拉回路存在性的判定定理:

一、无向图
每个顶点的度数都是偶数,则存在欧拉回路。

二、有向图(所有边都是单向的)
每个节顶点的入度都等于出度,则存在欧拉回路。

或者有两个点(起点或终点)入度不等于出度,满足(

((out[p[0]] - in[p[0]] == 1 && in[p[1]] - out[p[1]] == 1) // 
|| (out[p[1]] - in[p[1]] == 1 && in[p[0]] - out[p[0]] == 1))

),也叫存在欧拉回路。

入度:指向改点的边数

出度:离开该点的边数

度:与该点连接的边数

三.混合图欧拉回路
  混合图欧拉回路用的是网络流。
  把该图的无向边随便定向,计算每个点的入度和出度。如果有某个点出入度之差为奇数,那么肯定不存在欧拉回路。因为欧拉回路要求每点入度 = 出度,也就是总度数为偶数,存在奇数度点必不能有欧拉回路。

好了,到这里,这道题也就解决了,相信大家对并查集及欧拉回路又有了更深刻的理解。

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#define MAX 27
using namespace std;
int out[MAX], in[MAX], f[MAX], p[MAX],per[MAX];
int find(int x) //查找当前节点的祖先(根)
{
if (per[x] == x) return x;
else return per[x] = find(per[x]);
}
void unite(int xx, int yy)////如果这两个节点不同根,则令a的祖先指向b,从而建立同源树
{
int x = find(xx);
int y = find(yy);
if (x == y) return;
else per[x] = y;
}
int main()
{
int i, k, count, a, b,t,n;
char str[];
cin >> t; //输入案例个数
while (t--)
{
for (i = ; i <= ; i++)
per[i] = i;////对26个字母进行初始化
memset(in, , sizeof(in));
memset(out, , sizeof(out));
memset(f, , sizeof(f));
cin >> n;
for (i = ; i <= n; i++)
{//用0,1,2...26分别表示26个英文字母,a,b分别表示各个单词首尾字母
cin >> str;
a = str[] - 'a' + ;
b = str[strlen(str) - ] - 'a' + ;
unite(a, b);//将a,b合并
out[a]++; //入度和出度分别加1
in[b]++;
f[a] = f[b] = ;//标记该字母已存在
}
count = ;
for (i = ; i <= ; i++)
{
per[i] = find(i);
if (f[i] && per[i] == i)
count++;//计算连通分支个数
}
if (count > )//非连通图,直接输出
{
cout << "The door cannot be opened." << endl;
continue;
}
k = ;
for (i = ; i <= ; i++)
{
if (f[i] && in[i] != out[i])//计算存在字母的入度及出度不相等的节点数
p[k++] = i;
}
if (k == )//不存在出度不等于入度的节点,是欧拉回路
{
cout << "Ordering is possible." << endl;
continue;
}
if (k == && //入度与出度不相等的节点个数为2
((out[p[]] - in[p[]] == && in[p[]] - out[p[]] == ) //入度与出度不相等的节点个数为2
|| (out[p[]] - in[p[]] == && in[p[]] - out[p[]] == )))
{//且起点出度减入度等于1或者终点入度减出度等于1,则是欧拉回路
cout << "Ordering is possible." << endl;
continue;
}
cout << "The door cannot be opened." << endl;
}
return ;
}

HDU 1116 Play on Words(并查集和欧拉回路)(有向图的欧拉回路)的更多相关文章

  1. Play on Words HDU - 1116 (并查集 + 欧拉通路)

    Play on Words HDU - 1116 Some of the secret doors contain a very interesting word puzzle. The team o ...

  2. HDU 1116 Play on Words(欧拉路径(回路))

    http://acm.hdu.edu.cn/showproblem.php?pid=1116 题意:判断n个单词是否可以相连成一条链或一个环,两个单词可以相连的条件是 前一个单词的最后一个字母和后一个 ...

  3. LeetCode刷题总结-排序、并查集和图篇

    本文介绍LeetCode上有关排序.并查集和图的算法题,推荐刷题总数为15道.具体考点分析如下图: 一.排序 1.数组问题 题号:164. 最大间距,难度困难 题号:324. 摆动排序 II,难度中等 ...

  4. 22.1.22 并查集和KMP算法

    22.1.22 并查集和KMP算法 1.并查集结构 1)实现: 并查集有多种实现方式,例如向上指的图的方式,数组的方式等等.其根本思想就在于准确记录某个节点的根节点,这个这种记录就能够很快的实现并查集 ...

  5. hdu 1116 并查集和欧拉路径

    ---恢复内容开始--- 把它看成是一个图 只是需要欧拉路径就可以了 首尾能连成一条线即可 如果要判断这个图是否连通 得用并查集 在hrbust oj里面看答案学到的方法 不用各种for循环套着判断能 ...

  6. hdu 1116 欧拉回路+并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...

  7. HDU 1116 Play on Words(欧拉回路+并查集)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1116 Play on Words Time Limit: 10000/5000 MS (Java/Ot ...

  8. <hdu - 1863> 畅通工程 并查集和最小生成树问题

    本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1863  Problem Description: 省政府“畅通工程”的目标是使全省任何两个村庄间都可以 ...

  9. hdu 1116 Play on Words 欧拉路径+并查集

    Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

随机推荐

  1. mysql 到postgresql

    1 import pandas as pd 2 import psycopg2 3 from io import StringIO 4 import pymysql 5 conf={"mys ...

  2. iOS使用UIWebView遇到Error Domain=WebKitErrorDomain Code=101 “The operation couldn’t be completed. (WebKitErrorDomain error 101

    现在在接触iOS开发,今天在调试一个界面加载web页面的问题,发现死活无法加载,浏览器里能正常打开,加上相应代码之后得到了错误信息为: 2013-04-18 15:05:06.446 Client_D ...

  3. ant 打 jar 包添加 manifest.mf 文件

    经查询 ant 有 <manifest> 任务可以创建 manifest文件(https://ant.apache.org/manual/Tasks/manifest.html) 但尝试在 ...

  4. Zend Studio导致PHP插入数据库中文乱码【坑了个爹】

    用PHP往数据库里面插入数据,在执行INSERT语句前已经执行过 SET NAMES UTF8命令,MySql数据库的编码也确定是UTF8,然而插入中文的结果还是乱码. 找来找去,最后发现原来是用的I ...

  5. iOS-----推送机制(上)

    推 送 机 制 使用NSNotificationCenter通信 NSNotificationCenter实现了观察者模式,允许应用的不同对象之间以松耦合的方式进行通信. NSNotification ...

  6. [LeetCode&Python] Problem 883. Projection Area of 3D Shapes

    On a N * N grid, we place some 1 * 1 * 1 cubes that are axis-aligned with the x, y, and z axes. Each ...

  7. 你在AutoHotKey面前居然敢比调音量 - imsoft.cnblogs

    当你正在电脑游戏中酣战之际.或者正沉浸在动作大片紧张激烈的情节中.或者正在全神贯注的聆听优美动听音乐……,在这些场景中,如果你需要迅速对音量进行调节(例如增大减小音量,或者静音)怎么办?难道返回Win ...

  8. Codeforces 208A:Dubstep(字符串)

    题目链接:http://codeforces.com/problemset/problem/208/A 题意 给出一个字符串,将字符串中的WUB给删去,如果两个字母间有WUB,则这两个字母用空格隔开 ...

  9. matplotlib-------标记特殊点

    import matplotlib.pyplot as plt import numpy as np def demo_test(): a=np.array([0.15,0.16,0.14,0.17, ...

  10. leetcode:Count and Say【Python版】

    一次AC 字符串就是:count+char class Solution: # @return a string def countAndSay(self, n): str = " for ...