UVa 10129 Play on Words(并查集+欧拉路径)
题目链接:
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(并查集+欧拉路径)的更多相关文章
- UVa 10129 (并查集 + 欧拉路径) Play on Words
题意: 有n个由小写字母的单词,要求判断是否存在某种排列使得相邻的两个单词,前一个单词末字母与后一个单词首字母相同. 分析: 将单词的两个字母看做节点,则一个单词可以看做一条有向边.那么题中所求的排列 ...
- UVA 572 油田连通块-并查集解决
题意:8个方向如果能够连成一块就算是一个连通块,求一共有几个连通块. 分析:网上的题解一般都是dfs,但是今天发现并查集也可以解决,为了方便我自己理解大神的模板,便尝试解这道题目,没想到过了... # ...
- 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 ...
- UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环
X-Plosives A secret service developed a new kind ofexplosive that attain its volatile property only ...
- uva 1493 - Draw a Mess(并查集)
题目链接:uva 1493 - Draw a Mess 题目大意:给定一个矩形范围,有四种上色方式,后面上色回将前面的颜色覆盖,最后问9种颜色各占多少的区域. 解题思路:用并查集维护每一个位置相应下一 ...
- UVA 11987 Almost Union-Find (并查集+删边)
开始给你n个集合,m种操作,初始集合:{1}, {2}, {3}, … , {n} 操作有三种: 1 xx1 yy1 : 合并xx1与yy1两个集合 2 xx1 yy1 :将xx1元素分离出来合到yy ...
- UVA - 1160(简单建模+并查集)
A secret service developed a new kind of explosive that attain its volatile property only when a spe ...
- UVA 1493 Draw a Mess(并查集+set)
这题我一直觉得使用了set这个大杀器就可以很快的过了,但是网上居然有更好的解法,orz... 题意:给你一个最大200行50000列的墙,初始化上面没有颜色,接着在上面可能涂四种类型的形状(填充): ...
- UVa 1455 Kingdom 线段树 并查集
题意: 平面上有\(n\)个点,有一种操作和一种查询: \(road \, A \, B\):在\(a\),\(b\)两点之间加一条边 \(line C\):询问直线\(y=C\)经过的连通分量的个数 ...
随机推荐
- PCI总线目标接口状态机设计
module state_machine (devsel_l, trdy_l, stop_l, pci_ad_oe, dts_oe, par_oe, bk_oe, pci_ad_en, hi ...
- hdu 1.2.5
#include<cstdio> #include<cstring> int main() { //freopen("input.txt","r& ...
- java并发的处理方式
1 什么是并发问题. 多个进程或线程同时(或着说在同一段时间内)访问同一资源会产生并发问题. 银行两操作员同时操作同一账户就是典型的例子.比如A.B操作员同时读取一余额为1000元的账户,A操作员为该 ...
- python 操作mysql数据库之模拟购物系统登录及购物
python 操作mysql数据库之模拟购物系统登录及购物,功能包含普通用户.管理员登录,查看商品.购买商品.添加商品,用户充值等. mysql 数据库shop 表结构创建如下: create TAB ...
- 验证FluentValidation
FluentValidation https://www.xcode.me/code/fluentvalidation-dot-net-library 这里写得很详细了
- Button去除边框方法
<Button Content="Button" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey} ...
- sam(后缀自动机)
后缀自动机ins解释 void ins(int c){ int p=last;//将当前节点的parent节点变为last int np=++cnt;//建立新节点 last=np;//将last设为 ...
- Redis---基础数据结构
1.String(字符串) 1.1 概述 字符串 string 是 Redis 最简单的数据结构.Redis 所有的数据结构都是以唯一的 key 字符串作为名称,然后通过这个唯一 key 值来获取相应 ...
- solr初识
参考资料http://blog.csdn.net/l1028386804/article/details/70199983
- 人工智能-机器学习之numpy方法
机器学习 最重要的东西就是算法 这里面的水很深 所以呢我就简单的整理了一下 基础的操作 #导入numpy库 as别名 为了怕重名 import numpy as np # 打印版本号 p ...