题目链接

题意 : 有很多门,每个门上有很多磁盘,每个盘上一个单词,必须重新排列磁盘使得每个单词的第一个字母与前一个单词的最后一个字母相同。给你一组单词问能不能排成上述形式。

思路 :把每个单词看成有首字母指向尾字母的有向边,每个字母看成一个点,题中要求等效于判断图中是否存在一条路径经过每一条一次且仅一次,就是有向欧拉通路。统计个顶点的出入度,如果每个点的出入度都相同,那就是欧拉回路,如果有两个奇数度,那就是欧拉通路,除此之外,都不能满足要求。还有别忘了判断是否连通,此时用到并查集,图中所有的边(u,v),如果u!=v且属于不同的连通分量,就合并。欧拉回路的基本定理及概念。

 #include <cstdio>
#include <cstring>
#include <iostream> using namespace std ; char word[] ;
int father[],out[],in[] ,vis[]; int find_(int x)
{
if(father[x] != x)
father[x] = find_(father[x]) ;
return father[x] ;
} void mergee(int a,int b)
{
if(find_(a) != find_(b))
father[find_(a)] = find_(b) ;
} void Init()
{
memset(out,,sizeof(out)) ;
memset(in,,sizeof(in)) ;
for(int i = ; i < ; i++)
father[i] = i ;
memset(vis,,sizeof(vis)) ;
} int main()
{
int T ;
cin >> T ;
int n ;
while(T--)
{
cin >> n ;
Init() ;
while(n -- )
{
scanf("%s",word) ;
int u = word[]-'a' ;
int v = word[strlen(word)-]-'a' ;
mergee(u,v) ;
out[u] ++ ;
in[v] ++ ;
vis[u] = vis[v] = ;
}
int cnt = ,cnt1 = ,cnt2 = ;
for(int i = ; i < ; i++)
{
if(vis[i] && father[i] == i)
{
cnt ++ ;
}
}
if(cnt > )
{
puts("The door cannot be opened.") ;
continue ;
}
bool flag = true ;
for(int i = ; i < ; i++)
{
if(vis[i] && out[i] != in[i])
{
if(out[i]-in[i] == )
{
cnt1 ++ ;
if(cnt1 > )
{
flag = false ;
break ;
}
}
else if(in[i]-out[i] == )
{
cnt2 ++ ;
if(cnt2 > )
{
flag = false ;
break ;
}
}
else
{
flag = false ;
break ;
}
}
}
if(!flag) puts("The door cannot be opened.") ;
else puts("Ordering is possible.") ;
}
return ;
}

HDU 1116 || POJ 1386 || ZOJ 2016 Play on Words (欧拉回路+并查集)的更多相关文章

  1. poj 1386 Play on Words 有向欧拉回路

    题目链接:http://poj.org/problem?id=1386 Some of the secret doors contain a very interesting word puzzle. ...

  2. hdu 4424 & zoj 3659 Conquer a New Region (并查集 + 贪心)

    Conquer a New Region Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...

  3. poj 1386 Play on Words门上的单词【欧拉回路&&并查集】

    题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...

  4. hdu 1116 欧拉回路+并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...

  5. HDU 1116 Play on Words(欧拉回路+并查集)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1116 Play on Words Time Limit: 10000/5000 MS (Java/Ot ...

  6. 【POJ 1988】 Cube Stacking (带权并查集)

    Cube Stacking Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)id ...

  7. 【POJ 1984】Navigation Nightmare(带权并查集)

    Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40 ...

  8. POJ 1703 Find them, Catch them (数据结构-并查集)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31102   Accepted: ...

  9. HDU 3038 - How Many Answers Are Wrong - [经典带权并查集]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

随机推荐

  1. [原创]从Oracle和Microsoft Sql Server迁移到PostgreSQL Plus Advanced Server

    一.了解PPAS的迁移方式1.在线迁移和离线迁移使用Migration Studio或Migration Toolkit直接向PPAS数据库进行对象定义和数据表中数据的迁移称为在线迁移,生成要迁移对象 ...

  2. poj 3641 Pseudoprime numbers

    题目连接 http://poj.org/problem?id=3641 Pseudoprime numbers Description Fermat's theorem states that for ...

  3. 常见css的兼容问题

    链接的虚线框问题 <!-- html --> <a class="noDashedBox" href="#"><img src=& ...

  4. iOS学习之Object-C语言集合

    一.数组类      1.C语言数组的特点:数组是一个有序的集合,用来存储相同数据类型的元素,通过下标访问数组中的元素,下标从0开始.      2.OC中的数组只能存储对象类型(必须是NSObjec ...

  5. IOS开发的国际化

    一 app中内容的国际化 1 添加简体中文支持(默认只有英文)         在xcode的PROJECT->Info->Localizations下添加简体中文的支持.   2新建St ...

  6. P1572: [Usaco2009 Open]工作安排Job

    做这道题走了不少弯路,其实本身也是很简单的,类似单调队列的东西.刚开始以为双关键字排序就行了,结果连WA两遍,忽然意识到可以在截止之前做这件事!!于是就规规矩矩的打队列,然而忘记找队列里的最小P做,当 ...

  7. 基于.net mvc的校友录(三、实体模型实现)

    实体模型设计 由于是实际开发,而且是时间比较紧的,所以,在开发实现过程中,总有一些对原计划的改动: AlumniBookModel数据库实体模型 这是主数据实体类,EF会根据此实体生成数据库,它的每一 ...

  8. Careercup - Microsoft面试题 - 5718181884723200

    2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...

  9. OS X 使用技巧——不用鼠标就能打开应用程序

    如果要打开的应用程序没有保留在Dock栏里,一种快速启动它的办法是按住Control+Space键后再输入应用程序的名称.按Control+Space键会开启聚光灯(Spotlight)搜索工具,它会 ...

  10. 【Java】Eclipse导出jar包与javadoc

    1.导出jar包 2.导出javadoc 3.jar包添加javadoc 4.出错解决 参考资料: http://www.cnblogs.com/cyh123/p/3345889.html http: ...