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\)经过的连通分量的个数 ...
随机推荐
- myeclipse6.6+maven跑springside4.1的demo
1.安装myeclipse6.6 2.myeclipse6.6安装maven 2.1 删除原有maven. 关闭Eclipse程序, 进入MyEclipse插件目录/eclipse/features ...
- how can I make the login form transparent?
This is how you can make the Login Form transparent: 1. Add this css to Server Module-> Custom cs ...
- Ubuntu下SVN安装和配置
一.SVN安装 1.安装包 1.$ sudo apt-get install subversion 2.创建项目目录 $ sudo mkdir /home/xiaozhe/svn $ cd /home ...
- asp.net web api 跨域问题
缘起 以前在asp.net mvc时代,很少出现跨域问题 自从使用了asp.net web api + angular (1/2)之后,开始有跨域问题了. 简单普及下跨域: 我的理解是只要是前台页面与 ...
- 聊聊如何设计千万级吞吐量的.Net Core网络通信!
聊聊如何设计千万级吞吐量的.Net Core网络通信! 作者:大石头 时间:2018-10-26 晚上 20:00 地点:QQ群-1600800 内容:网络通信, 网络库使用方式 网络库设计理念,高性 ...
- MVC 5使用ViewData(对象)显示数据
控制器协调处理好数据之后,是交由视图来显示数据.在控制器与视图交互有一个是ViewData.这次练习,Insus.NET就以它来做实例. 前些时间,Insus.NET实现的练习中,也有从控制器传数据给 ...
- JVM活学活用——类加载机制
类的实例化过程 有父类的情况 1. 加载父类静态 1.1 为静态属性分配存储空间并赋初始值 1.2 执行静态初始化块和静态初始化语句(从上至下) 2. 加载子类静态 2.1 为静态 ...
- jspm 简介
借鉴:http://www.jianshu.com/p/4aba847b3e8c 功能 1. 支持加载JavaScript各种模块化的写法:AMD.CommonJS.标准化的ES6模块... 2. 包 ...
- 版本控制工具git
公司要求用git,感觉不如svn好使,还是命令行的,暂时记录一下. 服务器是在linux上可以直接安装.我是虚拟机centos6.9版本.yum install -y git 查看版本号是git -- ...
- 将Python项目打包成EXE可执行文件(单文件,多文件,包含图片)
解决 将Python项目打包成EXE可执行文件(单文件,多文件,包含图片) 1.当我们写了一个Python的项目时,特别是一个GUI项目,我们特备希望它能成为一个在Windows系统可执行的EXE文件 ...