题目链接:

https://cn.vjudge.net/problem/UVA-10129

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 Specification

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 Specification

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

Output for the Sample Input

The door cannot be opened.Ordering is possible.The door cannot be opened.
 /*
题意描述:
输入给出n个单词,问是否能够构成一个序列
解题思路:
使用并查集判断图是否连通,再判断是否满足有向图存在欧拉道路的条件 直观的想法是把每个单词看成一个节点,如果两个符合首尾相连的条件就建立一种关系,然后判断是否能够构成一个序列,
但是把单词成一个节点,使用并查集时,时间复杂度是n方,而数据范围是10万,肯定会超时的。
不妨直接将每个单词看成一种关系,也就是首字母和尾字母是节点,每个单词是一种关系。
这样使用并查集和并的时候就是n的复杂度了。
易错分析:
注意判断是否满足欧拉道路的条件时,要分两种情况
*/
#include<bits/stdc++.h> int rd[],cd[],f[],bk[];
int n;
void merge(int u,int v);
int getf(int v);
int check(); int main()
{
//freopen("E:\\testin.txt","r",stdin);//记得写路径
int T;
char word[];
int fa,la;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=;i<;i++){
bk[i]=;
f[i]=i;
cd[i]=rd[i]=;
}
for(int i=;i<n;i++){
scanf("%s",word);
fa=word[]-'a';
bk[fa]=;
cd[fa]++;
la=word[strlen(word)-]-'a';
rd[la]++;
bk[la]=; merge(fa,la);
} if(check())
printf("Ordering is possible.\n");
else
printf("The door cannot be opened.\n");
}
return ;
} int check(){
int sum=;
for(int i=;i<;i++){
if(f[i] == i && bk[i])
sum++;
}
if(sum > )
return ; sum=;
int q=,z=;
for(int i=;i<;i++){
if(bk[i]){
if(cd[i] != rd[i]){
sum++;
if(cd[i] - == rd[i])
q++;
if(rd[i] - == cd[i])
z++;
}
}
}
//存在欧拉道路的两种情况
if((sum == && q == && z == ) || (sum == && q == && z == )) return ;
else return ;
}
void merge(int u,int v){
int t1=getf(u);
int t2=getf(v);
if(t1 != t2){
f[t2]=t1;
}
}
int getf(int v){
return f[v]==v ? v : f[v]=getf(f[v]);
}

UVa 10129 Play on Words(并查集+欧拉路径)的更多相关文章

  1. UVa 10129 (并查集 + 欧拉路径) Play on Words

    题意: 有n个由小写字母的单词,要求判断是否存在某种排列使得相邻的两个单词,前一个单词末字母与后一个单词首字母相同. 分析: 将单词的两个字母看做节点,则一个单词可以看做一条有向边.那么题中所求的排列 ...

  2. UVA 572 油田连通块-并查集解决

    题意:8个方向如果能够连成一块就算是一个连通块,求一共有几个连通块. 分析:网上的题解一般都是dfs,但是今天发现并查集也可以解决,为了方便我自己理解大神的模板,便尝试解这道题目,没想到过了... # ...

  3. UVA 12232 - Exclusive-OR(带权并查集)

    UVA 12232 - Exclusive-OR 题目链接 题意:有n个数字.一開始值都不知道,每次给定一个操作,I a v表示确认a值为v,I a b v,表示确认a^b = v,Q k a1 a2 ...

  4. UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环

    X-Plosives A secret service developed a new kind ofexplosive that attain its volatile property only ...

  5. uva 1493 - Draw a Mess(并查集)

    题目链接:uva 1493 - Draw a Mess 题目大意:给定一个矩形范围,有四种上色方式,后面上色回将前面的颜色覆盖,最后问9种颜色各占多少的区域. 解题思路:用并查集维护每一个位置相应下一 ...

  6. UVA 11987 Almost Union-Find (并查集+删边)

    开始给你n个集合,m种操作,初始集合:{1}, {2}, {3}, … , {n} 操作有三种: 1 xx1 yy1 : 合并xx1与yy1两个集合 2 xx1 yy1 :将xx1元素分离出来合到yy ...

  7. UVA - 1160(简单建模+并查集)

    A secret service developed a new kind of explosive that attain its volatile property only when a spe ...

  8. UVA 1493 Draw a Mess(并查集+set)

    这题我一直觉得使用了set这个大杀器就可以很快的过了,但是网上居然有更好的解法,orz... 题意:给你一个最大200行50000列的墙,初始化上面没有颜色,接着在上面可能涂四种类型的形状(填充):  ...

  9. UVa 1455 Kingdom 线段树 并查集

    题意: 平面上有\(n\)个点,有一种操作和一种查询: \(road \, A \, B\):在\(a\),\(b\)两点之间加一条边 \(line C\):询问直线\(y=C\)经过的连通分量的个数 ...

随机推荐

  1. java 图片压缩 缩放

    废话不多说,直接上代码,静态方法可直接调用,中间用流来处理的 /** * 图片缩放(未考虑多种图片格式和等比例缩放) * @param filePath 图片路径 * @param height 高度 ...

  2. java 调用c++程序实例

    1.java程序: package com.zhangshitong; import java.io.File; public class Java2cpp { static{ System.load ...

  3. 使用 ipmitool 实现远程管理Dell 系列服务器

    IBM 文档:       http://www.ibm.com/developerworks/cn/linux/l-ipmi/index.html ipmi命令收集: http://hi.baidu ...

  4. ThreadLocal实现线程级上下文

    一.ThreadLocal测试 package com.junge.threadlocal.context; /** * @author Administrator * */ public class ...

  5. 分形之列维(levy)曲线

    莱维C形曲线(Lévy C curve)是个自我相似的分形,最先由保罗·皮埃尔·莱维在1938年的论文Plane or Space Curves and Surfaces Consisting of ...

  6. 基于emoji 国际通用表情在web上的输入与显示的记录

    定义: emoji 即国际通用表情 场景: 1,ios,android,wp上emoji表情输入与显示 2,web也需作为支撑平台对emoji表情就行输入与显示(解析) 问题: 1,app端输入的表情 ...

  7. 使用 DotNetty 实现 Redis 的一个控制台应用程序

    零:Demo 跑出来的结果如图 上图说明 图中左边蓝色的命令行界面,是用windows powershell 命令行链接的. 1.打开powershell命令行界面,输入命令[telnet   127 ...

  8. 背水一战 Windows 10 (54) - 控件(集合类): ItemsControl 的布局控件 - OrientedVirtualizingPanel, VirtualizingStackPanel, WrapGrid

    [源码下载] 背水一战 Windows 10 (54) - 控件(集合类): ItemsControl 的布局控件 - OrientedVirtualizingPanel, VirtualizingS ...

  9. nodejs学习(imooc课程笔记, 主讲人Scott)

    课程地址: 进击Node.js基础(一) 进击Node.js基础(二) 1. nodejs创建服务器 var http = require('http'); //加载http模块 //请求进来时, 告 ...

  10. time clock getrusage clock_gettime gettimeofday timespec_get 对比

    http://stackoverflow.com/questions/12392278/measure-time-in-linux-time-vs-clock-vs-getrusage-vs-cloc ...