题目链接:

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. PCI总线目标接口状态机设计

    module state_machine (devsel_l, trdy_l, stop_l, pci_ad_oe,      dts_oe, par_oe, bk_oe, pci_ad_en, hi ...

  2. shell 命令 rz sz

    尝试了几个版本,下面的是可用的 https://segmentfault.com/a/1190000012166969

  3. 查找对端mac地址

    1.ping对端mac: 2.arp命令查找:

  4. poj 3083 Children of th

    #include <iostream> #include<stdio.h> #include<string.h> using namespace std; int ...

  5. PICT用户手册 [转]

    PICT 3.3 User's Guide Jacek Czerwonka, Test Lead, Microsoft Corporation Overview Using PICT to Combi ...

  6. EntityFramework Core 学习扫盲

    0. 写在前面 1. 建立运行环境 2. 添加实体和映射数据库 1. 准备工作 2. Data Annotations 3. Fluent Api 3. 包含和排除实体类型 1. Data Annot ...

  7. MVC+Nhibernate+spring.net(一)

    所用数据库是我之前所写的Nhibernate入门篇的数据库https://www.cnblogs.com/pandorabox/p/PandoraBox.html 第一步:创建一个mvc项目 第二步: ...

  8. 【转】OAuth2.0的refresh token

    转载自http://www.html-js.com/?p=1297 最近看人人网的OAuth认证,发现他是OAuth2.0,之前一直看的是新浪的OAuth,是OAuth1.0. 二者还是有很多不同的, ...

  9. 【译】准备好你求职时候用的 GitHub 账号

    我目前正在招聘,很多人分享了他们的GitHubs个人资料和项目,但是维护得很差,所以我决定为活跃的求职者写一个小指南. 无论是否合理,技术招聘人员倾向于从您的GitHub个人资料中推断出很多关于您的信 ...

  10. 【BZOJ3238】 [Ahoi2013]差异(SAM)

    传送门 BZOJ 洛谷 Solution SA版本的 考虑可以建一个SAM? 那么接下来我们就考虑每一对点对之间的贡献了. 把这个式子化简一下就是无序点对之间的那啥(自己意会一下) 然后我们定义边权为 ...