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. [SCOI2010] 连续攻击游戏

    题目 Description lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示.当他使用某种装备时,他只能使用该装备的某一 ...

  2. Shell 示例:将指定的文件内容转换为大写

    程序代码如下: #!/bin/bash # 将一个指定的输入文件内容转换为大写 E_FILE_ACCESS=70 E_WRONG_ARGS=71 if [ ! -r "$1" ] ...

  3. [转]RabbitMQ学习之:(十二)在Node.js环境下使用RabbitMQ

    本文转自:https://blog.csdn.net/puncha/article/details/8452017 学,以致用.找了半天Node.js下RabbitMQ的库,看上去都不太趁手,直到最后 ...

  4. linq使用Take和Skip实现分页

    ;//第1页 ;//页大小 var list = list.Skip((pageIndex-1) * pageSize).Take(pageSize).ToList();

  5. design mode(php)

    前段时间看了下设计模式 参考,以及head first设计模式,简要如下 ## OO原则 * 封装变化* 多用组合,少用继承* 针对接口编程,不针对实现编程* 为交互对象之间的松耦合设计而努力* 开闭 ...

  6. netty 基础

    Netty是一个高性能.异步事件驱动的NIO框架,提供了对TCP.UDP和文件传输的支持,作为一个异步NIO框架,Netty的所有IO操作都是异步非阻塞的,通过Future-Listener机制,用户 ...

  7. css引入页面的三种方法

    1.内联式:直接在标签上写样式 <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

  8. sql: Query to Display Foreign Key Relationships and Name of the Constraint for Each Table in Database

    --20170505 --塗聚文 Geovin Du CREATE DATABASE DuMailSystem GO USE DuMailSystem GO --1查詢表的及备注说明 SELECT S ...

  9. extract-text-webpack-plugin 作用、安装、使用

    作用:该插件的主要是为了抽离css样式,防止将样式打包在js中引起页面样式加载错乱的现象 安装:插件安装命令如下: npm install extract-text-webpack-plugin -- ...

  10. ionic3打包不能prod的问题

    在最近的项目中,我ionic3采用了懒加载,来提高性能.但是当我普通打包的时候,正常成功了,但是加上--prod的时候,就报错了. 报错如下: 大概意思就是page是声明的一部分,然后请在更高级声明之 ...