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.

每个门上会有大量磁盘且每个磁盘会有一个单词刻于其上。磁盘必须以此为序进行安排:每个单词的首字母必须与上一个单词的尾字母相同。例如“acm”后面可接“motorola”。你的任务就是写个程序来读入这些单词并判断是否有可能把它们安排明白以开门。

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 N that indicates the number of plates (1 ≤ N ≤ 100000). Then exactly N lines 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.

输入:第一行输入T代表会有T组测试数据。接下来每组会以一个整数N开始,暗示了会有N个磁盘(1 ≤ N ≤ 10万)。接下来的N行每行一个单词,单词包含2~1000个小写字母。相同的单词是可以多次出现的。

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.’

输出:对于每组数据,所有的单词必须都要用上,某单词出现了几次就要用几次。如果可以穿成一串,就是存在一种可能的顺序,打印语句“Ordering is possible.”;否则打印“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.

介绍:欧拉回路

如果图G中的一个路径包括每个边恰好一次,则该路径称为欧拉路径(Euler path)。
如果一个回路是欧拉路径,则称为欧拉回路(Euler circuit)。
 
欧拉回路是数学家欧拉在研究著名的德国哥尼斯堡(Koenigsberg)七桥问题时发现的。如图所示,流经哥尼斯堡的普雷格尔河(Pregel)中有两个岛,两个岛与两岸共4处陆地通过7座桥彼此相联。7桥问题就是:是否存在一条路线,可以不重复地走遍7座桥。

欧拉大佬把七桥问题改写成了图。则问题变成了:能否从无向图中的一个结点出发走出一条道路,每条边恰好经过一次。这样的路线就叫欧拉路径,也可以形象地称为“一笔画”。

不难发现,在欧拉道路中,进和出是对应的——除了起点和终点外,其他点的进出次数应该相等。换句话说,其他点的度数(degree)应该是偶数(我们把进入称为入度,出去称为出度)。在七桥问题中,所有4个点的度数均是奇数(这样的点称为奇点),因此不可能存在欧拉道路。

在此不加证明地给出欧拉道路和回路的存在条件,请结合生活实际验证:

那么介绍到这里,应该就可以自己动手编写这道欧拉路径的裸题了,先判断度数,再判断连通性即可。建议自己实现,有问题再参考别人程序。

此处给出两种判断连通性的代码,由于DFS和并查集都是基础算法,暂时不做冗余介绍~

DFS:

 #include <bits/stdc++.h>
using namespace std; string str;
int test, n;
int ru[], chu[];//入度和出度
bool table[][], vis[]; bool dfs(int cur)
{
vis[cur] = true;
for (int i = ; i < ; i++)
if (cur != i && table[cur][i] && !vis[i])
dfs(i);
} bool ok()
{
for (int i = ; i < ; i++)
if (vis[i])
n--;
return n == ;
} int main()
{
cin >> test;
while (test--)
{
//初始化
memset(table, false, sizeof(table));
memset(vis, false, sizeof(vis));
memset(ru, , sizeof(ru));
memset(chu, , sizeof(chu));
//输入
cin >> n;
for (int i = ; i <= n; i++)
{
cin >> str;
int x = str[] - 'a', y = str[str.length()-] - 'a';
table[x][y] = table[y][x] = true;//邻接矩阵制作无向图
chu[x]++;
ru[y]++;
}
//计算度数是否符合条件
int a = , b = , c = -, d = ;
n = ;
for (int i = ; i < ; i++)
{
if (ru[i] == chu[i])
{
a++;
if (!ru[i])//用来判定最后出现了几个字母
n--;
}
else if (ru[i] == chu[i]-) b++, c = i;
else if (ru[i] == chu[i]+) d++;
} if ((b == && d == && a == ) || a == )
{
if (c == -)//即a==26时
for (int i = ; i < ; i++)
if (chu[i])
{
c = i;
break;
}
dfs(c);
if (ok())
{
puts("Ordering is possible.");
continue;
}
}
puts("The door cannot be opened.");
}
return ;
}

并查集:

 #include <cstdio>
#include <cstring>
#include <vector> int n;
int f[], degree[];
char str[];
bool vis[];//标记某字母是否出现过
std::vector <int> v;//储存奇点 void init()
{
memset(degree, , sizeof(degree));
memset(vis, false, sizeof(vis));
v.clear();
for (int i = ; i < ; i++) f[i] = i;//并查集的预处理
} int getf(int v)
{
return v == f[v] ? v : f[v] = getf(f[v]);
} void input()
{
scanf("%d", &n);
while (n--)
{
scanf("%s", str);
int x = str[] - 'a', y = str[strlen(str)-] - 'a'; vis[x] = vis[y] = true;
degree[x]++, degree[y]--;
int t = getf(x), p = getf(y);
if (t != p)
f[p] = t;
}
} bool cal()
{
int cc = ;//有多少个连通块
for (int i = ; i < ; ++i)
{
if (degree[i] != )
v.push_back(degree[i]);
if (vis[i] && f[i] == i)
cc++;
}
return cc == && (v.empty() || (v.size() == && (v[] == || v[] == )));
} int main()
{
int test;
scanf("%d", &test);
while (test--)
{
init();//初始化
input();//输入 + 并查集处理
printf("%s\n", cal() ? "Ordering is possible." : "The door cannot be opened.");
}
}

UVA10129:Play on Words(欧拉回路)的更多相关文章

  1. UVA10129 Play on Words —— 欧拉回路

    题目链接:https://vjudge.net/problem/UVA-10129 代码如下: // UVa10129 Play on Words // Rujia Liu // 题意:输入n个单词, ...

  2. uva10129 PlayOnWords(并查集,欧拉回路)

    判断无向图是否存在欧拉回路,就是看度数为奇数的点有多少个,如果有两个,那么以那分别两个点为起点和终点,可以构造出一条欧拉回路,如果没有,就任意来,否则,欧拉回路不存在. 首先用并查集判断连通,然后统计 ...

  3. 6_16 单词(UVa10129)<欧拉回路>

    考古学家有时候遇到一些神秘的门,这些门需要解开特定的谜题才能打开.因为没有其他方法可以打开门,这谜题对我们来说非常重要.在门上有许多磁盘,每个盘子上有一个英文单字在上面.这些盘子必须被安排,使得盘子上 ...

  4. Play on Words UVA - 10129 (欧拉回路)

    题目链接:https://vjudge.net/problem/UVA-10129 题目大意:输入N  代表有n个字符串  每个字符串最长1000  要求你把所有的字符串连成一个序列  每个字符串的第 ...

  5. ACM/ICPC 之 混合图的欧拉回路判定-网络流(POJ1637)

    //网络流判定混合图欧拉回路 //通过网络流使得各点的出入度相同则possible,否则impossible //残留网络的权值为可改变方向的次数,即n个双向边则有n次 //Time:157Ms Me ...

  6. [poj2337]求字典序最小欧拉回路

    注意:找出一条欧拉回路,与判定这个图能不能一笔联通...是不同的概念 c++奇怪的编译规则...生不如死啊... string怎么用啊...cincout来救? 可以直接.length()我也是长见识 ...

  7. ACM: FZU 2112 Tickets - 欧拉回路 - 并查集

     FZU 2112 Tickets Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u P ...

  8. UVA 10054 the necklace 欧拉回路

    有n个珠子,每颗珠子有左右两边两种颜色,颜色有1~50种,问你能不能把这些珠子按照相接的地方颜色相同串成一个环. 可以认为有50个点,用n条边它们相连,问你能不能找出包含所有边的欧拉回路 首先判断是否 ...

  9. POJ 1637 混合图的欧拉回路判定

    题意:一张混合图,判断是否存在欧拉回路. 分析参考: 混合图(既有有向边又有无向边的图)中欧拉环.欧拉路径的判定需要借助网络流! (1)欧拉环的判定:一开始当然是判断原图的基图是否连通,若不连通则一定 ...

随机推荐

  1. NOIP2010_T4_引水入城 bfs+贪心

    在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个 N 行 M 列的矩形,如上图所示,其中每个格子都代表一座城 市,每座城市都有一个海拔高度.为了使 ...

  2. CI 模型公用查询函数

    /** * 多字段条件查询数据 * @param array $val array("name" => $value).name为要操作的字段,value为要操作的值 * @ ...

  3. JAVA基础细谈

    JAVA基础细谈 一. 源文件和编译后的类文件     源文件的本质就是程序文件,是程序员编写,是人看的.而编译后的类文件是给电脑看的文件.一个类就是一个文件,无论这个类写在哪里,编译以后都是一个文件 ...

  4. MAC 安装phantomjs

    step1:下载压缩包http://phantomjs.org/ step2:解压缩,我是解压缩到/Users/gxy/software step3:写入配置路径,vi ~/.bash_profile ...

  5. WinDbg 调试工具的使用

    概述 项目接近尾声了,可是在运行时会有memory leak(内存泄露) bug.产品在运行一天后,内存增长致1.4G,而我们产品的初始内存才有70M,问题很严重,决定采用WinDbg工具来分析代码问 ...

  6. 增加,删除GMS包

    1. device/hiteq/vtab_1050_standard/httek.mk BUILD_GMS:=yes GMS_VARIANT:=mini 2. rm out/target/produc ...

  7. PHP多种序列化/反序列化的方法 json_encode json_decode

    序列化是将变量转换为可保存或传输的字符串的过程:反序列化就是在适当的时候把这个字符串再转化成原来的变量使用.这两个过程结合起来,可以轻松地存储和传输数据,使程序更具维护性. 1. serialize和 ...

  8. CentOS 6 命令行下安装 VirtualBox 虚拟机步骤

    CentOS 6 命令行下安装 VirtualBox 虚拟机步骤 1. 准备工作 安装内核更新 yum install kernel-develyum update kernel*如果内核有更新,则需 ...

  9. storyBoard学习教程一(页面跳转)

    今天为了给伙伴作一篇storyBoard快速编程的教程,所以才写下了这篇博客. 有过storyBoard 编程经验的伙伴还是不要阅读本篇博客了,我自己认为,太基础太简单了,为了方便别人学习使用,我还是 ...

  10. java:calendar类及一些比较实用的utils(一)

    在java编程中经常会用到时间日期的计算.比较.格式化等等操作,刚开始接触Calendar类时,还是在初学习期间,小小白一枚,看着这个好复杂,懒惰心理作祟也就没有怎么去学习,后来在项目中经常用到,索性 ...