HDU 1116 || POJ 1386 || ZOJ 2016 Play on Words (欧拉回路+并查集)
题意 : 有很多门,每个门上有很多磁盘,每个盘上一个单词,必须重新排列磁盘使得每个单词的第一个字母与前一个单词的最后一个字母相同。给你一组单词问能不能排成上述形式。
思路 :把每个单词看成有首字母指向尾字母的有向边,每个字母看成一个点,题中要求等效于判断图中是否存在一条路径经过每一条一次且仅一次,就是有向欧拉通路。统计个顶点的出入度,如果每个点的出入度都相同,那就是欧拉回路,如果有两个奇数度,那就是欧拉通路,除此之外,都不能满足要求。还有别忘了判断是否连通,此时用到并查集,图中所有的边(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 (欧拉回路+并查集)的更多相关文章
- poj 1386 Play on Words 有向欧拉回路
题目链接:http://poj.org/problem?id=1386 Some of the secret doors contain a very interesting word puzzle. ...
- 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 ...
- poj 1386 Play on Words门上的单词【欧拉回路&&并查集】
题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...
- hdu 1116 欧拉回路+并查集
http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...
- HDU 1116 Play on Words(欧拉回路+并查集)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1116 Play on Words Time Limit: 10000/5000 MS (Java/Ot ...
- 【POJ 1988】 Cube Stacking (带权并查集)
Cube Stacking Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)id ...
- 【POJ 1984】Navigation Nightmare(带权并查集)
Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40 ...
- POJ 1703 Find them, Catch them (数据结构-并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31102 Accepted: ...
- 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 ...
随机推荐
- oracle 修改表空间存储路径
[root@yoon ~]# more /etc/oracle-releaseOracle Linux Server release 5.7 Oracle Database 11g Enterpris ...
- jquery 简单弹出层
预定义html代码:没有 所有代码通过js生成和移除. 预定义css .z-popup-overlay{ width:100%; min-height: 100%; height:800px; pos ...
- golang的{}初始化
之前说到Golang中某些类型可以赋值nil, 某些类型不能赋值nil. 不能赋值nil都是initialized value不为nil的类型, 例如: bool int, intN uint, ui ...
- sharepoint 2010 误删除AD组用户不能访问
不小心误操作把ad中的组删除了,在sharepoint中是通过组给的权限,在ad中新建了一个同样名的组给了权限组下面的用户还是不能访问. 解决方法: 在sharepoint中把这组从网站集中删除,重新 ...
- [收藏]win8安装弹出输入的产品密钥与用于安装任何可用windows映像都不匹配
问题描述: 帮朋友装win8(第一次装大神不要喷我啊)结果到 现在安装 这一步的时候 点击 现在安装 弹出个窗口 说输入的产品密钥与用于安装任何可用windows映像都不匹配.请输入其他产品密钥 解决 ...
- css3 forwards、backwards、both
animation-fill-mode 属性规定动画在播放之前或之后,其动画效果是否可见. none 不改变默认行为. forwards 当动画完成后,保持最后一个属性值(在最后一个关键帧中定义). ...
- storm集成kafka
kafkautil: import java.util.Properties; import kafka.javaapi.producer.Producer; import kafka.produce ...
- android 自动化压力测试-monkey 2 获取程序包名
monkey 1 中我们写到: C:\Users\chenshan>adb shell shell@hwG750-T20:/ $ monkey -p cn.emoney.acg -v 500 说 ...
- Codeforces Round #164 (Div. 2) E. Playlist 贪心+概率dp
题目链接: http://codeforces.com/problemset/problem/268/E E. Playlist time limit per test 1 secondmemory ...
- 请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。 但是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是。
// test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...