Problem UVA10129-Play on Words

Accept: 2534  Submit: 19477

Time Limit: 3000 mSec

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

 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.

题解:欧拉路径模板题,这里面运用并查集判断联通,对于欧拉路径的判断,lrj的代码给了一个很好的模板,简单严密

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
using namespace std; const int maxl = +;
const int kind = ;
char str[maxl];
int n,pre[kind],deg[kind];
bool used[kind]; int findn(int x){
return x == pre[x] ? x : pre[x] = findn(pre[x]);
} int main()
{
//freopen("input.txt","r",stdin);
int iCase;
scanf("%d",&iCase);
while(iCase--){
scanf("%d",&n);
memset(used,false,sizeof(used));
memset(deg,,sizeof(deg));
for(int i = ;i < ;i++) pre[i] = i;
int cnt = ;
for(int i = ;i <= n;i++){
scanf("%s",str);
int x1 = str[]-'a',x2 = str[strlen(str)-]-'a';
used[x1] = used[x2] = true;
deg[x1]++,deg[x2]--;
int fx1 = findn(x1),fx2 = findn(x2);
if(fx1 != fx2){
pre[fx1] = fx2;
cnt--;
}
}
vector<int> ans;
for(int i = ;i < kind;i++){
if(!used[i]) cnt--;
else if(deg[i] != ){
ans.push_back(deg[i]);
}
}
bool ok = false;
if(cnt== && (ans.empty() || (ans.size()==) && (ans[]== || ans[]==-))) ok = true;
if(ok) printf("Ordering is possible.\n");
else printf("The door cannot be opened.\n");
}
return ;
}

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

  1. [欧拉路径]Play on Words UVA10129

    传送门:   UVA - 10129 题目大意: 给定一些单词(可能会重复出现),判断单词是否能排成一个序列,前提是单词的最后一个字母与下一个单词的第一个字母相同.输出"The door c ...

  2. Play on Words UVA - 10129 欧拉路径

    关于欧拉回路和欧拉路径 定义:欧拉回路:每条边恰好只走一次,并能回到出发点的路径欧拉路径:经过每一条边一次,但是不要求回到起始点 ①首先看欧拉回路存在性的判定: 一.无向图每个顶点的度数都是偶数,则存 ...

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

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

  4. 【USACO 3.3】Riding The Fences(欧拉路径)

    题意: 给你每个fence连接的两个点的编号,输出编号序列的字典序最小的路径,满足每个fence必须走且最多走一次. 题解: 本题就是输出欧拉路径. 题目保证给出的图是一定存在欧拉路径,因此找到最小的 ...

  5. hdu 3472 HS BDC(混合路的欧拉路径)

    这题是混合路的欧拉路径问题. 1.判断图的连通性,若不连通,无解. 2.给无向边任意定向,计算每个结点入度和出度之差deg[i].deg[i]为奇数的结点个数只能是0个或2个,否则肯定无解. 3.(若 ...

  6. poj 2337 有向图输出欧拉路径

    Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10186   Accepted: 2650 Descrip ...

  7. nyoj 42 一笔画问题 欧拉路径

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=42 欧拉回路,欧拉路径水题~ 代码: #include "stdio.h&quo ...

  8. UESTC 917 方老师的分身IV --求欧拉路径

    判断欧拉路径是否存在及求出字典序最小的欧拉路径问题(如果存在). 将字符串的第一个字母和最后一个字母间连边,将字母看成点,最多可能有26个点(a-z),如果有欧拉路径,还要判断是否有欧拉回路,如果有, ...

  9. POJ1780 Code(欧拉路径)

    n位密码,要用尽可能短的序列将n位密码的10n种状态的子串都包括,那么要尽量地重合. 题目已经说最短的是10n + n - 1,即每一个状态的后n-1位都和序列中后一个状态的前n-1位重合. 这题是经 ...

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

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

随机推荐

  1. 正则表达式之javascript

    1.正则表达式的定义 描述字符模式的对象,JavaScript的RepExp类表示正则表达式 var pattern = new RegExp("s\("); <=> ...

  2. WPF Application 类介绍以及怎样修改启动方式

    因为想要修改wpf的启动方式,所以研究了下Application类,现把一些有用的属性与大家分享下: 属性: Current                  获取当前 AppDomain的 Appl ...

  3. html 颜色选择器 亲测,很好用

    @*以下 是测试html 颜色选择器的*@ @*<a href="#" mce_href="#" onclick="initColorPicke ...

  4. JavaScript 变量作用域 详解

    变量作用域要点 - 在JavaScript中没有块级作用域,只有函数作用域 - 在函数体内,局部变量的优先级高于同名的全局变量 - 在全局作用域编写代码时可以不写var语句,但声明局部变量时必须使用v ...

  5. 140 - The 12th Zhejiang Provincial Collegiate Programming Contest(浙江省赛2015)

      Ace of Aces Time Limit: 2 Seconds      Memory Limit: 65536 KB There is a mysterious organization c ...

  6. TestOps宣言

    TestOps   TestOps离不开敏捷 TestOps是测试驱动的一种延伸,它强调测试人员与运维人员沟通协作规范化的实践模式. DevOps的持续集成与持续交付,实现了从代码到服务的快速落地.而 ...

  7. tomcat版本号隐藏或修改

    tomcat版本号隐藏或修改 找到tomcat\lib\catalina.jar\org\apache\catalina\util\ServerInfo.properties文件 修改 server. ...

  8. sql server:alter database name

    --step 1 : 修改数据库名称 USE master GO ALTER DATABASE GeovinDuCms SET SINGLE_USER WITH ROLLBACK IMMEDIATE ...

  9. vue2.X的路由

    以 / 开头的嵌套路径会被当作根路径. <router-link> 在vue-router1.X中是以<a v-link=""></a>存在的 ...

  10. React 入门学习笔记整理(九)——路由

    (1)安装路由 React-router React-router提供了一些router的核心api,包括Router, Route, Switch等,但是它没有提供dom操作进行跳转的api. Re ...