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\)经过的连通分量的个数 ...
随机推荐
- AngularJS 控制器 Live Dom
控制器的作用是在$scope对象上创建属性和方法,控制器的作用域是$scope,所以作用域是针对控制器来讲的.另外,控制器实例不是单例,每次都会重新实例化,不像服务是单例的. 其是注册在模块上的,如 ...
- 关于cxGrid的排序问题
当然,这个 dcoAnsiSort也很重要,否者不按拼音排序 1.首先需要开启 Views的 OptionsCustomize.ColumnSorting 2.再设置每列的这个 Sorting 3. ...
- 2015年 10月最新苹果IOS上架App Store商店步骤
1.1.前期工作 首先你需要有一个苹果的开发者帐号,一个Mac系统. 如果没有帐号可以在打开http://developer.apple.com/申请加入苹果的开发者计划.支付99美元每年,怎么申请网 ...
- ASP.NET写的一个博客系统
由于域名闲置,正好也有服务器空间,短期内开发了一个博客系统. 大家都来谈 http://www.djdlt.com 目前是开放注册,免费发布.(限于空间有限,图片还是尽量少传些) 网站架构: ASP ...
- ASP.NET Web API 中使用 swagger 来管理 API 文档
本文以 ASP.NET Web API 为后台框架,利用 EF6 连接 postgreSQL 数据库,使用 swagger 来生成 REST APIs文档.文章分二个部分,第一部分主要讲如何用 EF6 ...
- JS中同步显示并分割输入的数字字符串
题目比较晦涩,来张图来说明要表达的效果: 第一张图的效果就是,用户输入一个数字,上面就显示一个大层,然后显示输入的数字,并把数字用空格按照每四位分割出来.好像在建行的网上银行上面就有这种效果.第二个图 ...
- WPF 内存泄漏优化经历
最近公司有个CS客户端程序,有个登录界面,有个程序的主界面,程序支持注销功能,但是在注销后,客户端的内存一直以40M-50M的速度递增,因此猜测,应该是WPF程序出现了内存泄漏.下面主要记录优化内存泄 ...
- UCore-Lab0
日期:2019/3/31 内容:UCore-Lab0 一.UCore实验 实验 说明 关键词 Lab1 bootloader的实现 中断 Lab2 物理内存管理 x86分段/分页模式 Lab3 虚拟内 ...
- [SDOI2011]消耗战(虚树+树形动规)
虚树dp 虚树的主要思想: 不遍历没用的的节点以及没用的子树,从而使复杂度降低到\(\sum\limits k\)(k为询问的节点的总数). 所以怎么办: 只把询问节点和其LCA放入询问的数组中. 1 ...
- 36_并发编程-multiprocess模块
仔细说来,multiprocess不是一个模块而是python中一个操作.管理进程的包. 之所以叫multi是取自multiple的多功能的意思,在这个包中几乎包含了和进程有关的所有子模块.由于提供的 ...