hdoj 1116 Play on Words 【并查集】+【欧拉路】
Play on Words
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.
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.
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.".
3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok
The door cannot be opened.
Ordering is possible.
The door cannot be opened.
题意:给出几个字符串。假设一个字符串的首字符(尾子符)等于另外一个字符串的尾子符(首字符),就让他们连接起来。问最后能不能把全部的字符串都连接起来。
分析:非常明显的是要用到并查集的仅仅是。可是处理首尾字符的时候会有点麻烦,我们最好还是将没一个字符的首尾字符都视为一个点,一个字符串就是一条边,那么该题就转化为了求边能不能形成一条连通图,之后就要用欧拉路来推断改图是否连通就好了。
注:欧拉路分为欧拉回路和欧拉通路。
欧拉通路:满足从一点出发经过每一条边且仅仅经过一次,能把全部的边都经过的路
欧拉回路:欧拉通路而且最后回到原点的路;
假设是欧拉回路那么图中每一个点的入读和处度都相等
假设是通路那么起始点的出度减入度为1, 终点处入度减出度为1。
代码:
/*hdoj 1116 并查集+欧拉通/回路*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define M 1005 int out[26], in[26], fat[26];
bool vis[26];
char s[M]; int f(int x){
if(x != fat[x]) fat[x] = f(fat[x]);
return fat[x];
} void merge(int x, int y){
int a = f(x);
int b = f(y);
if(a != b) fat[a] = b;
} int main(){
int n, t, i;
scanf("%d", &t);
while(t --){
memset(vis, 0, sizeof(vis));
memset(out, 0, sizeof(out));
memset(in, 0, sizeof(in));
scanf("%d", &n);
for(i = 0; i < 26; i ++) fat[i] = i;
for(i = 0; i < n; i ++){
scanf("%s", s);
int x = s[0]-'a';
int y = s[strlen(s)-1]-'a';
merge(x, y);
++out[x]; ++in[y];
vis[x] = vis[y] = 1;
}
int flag1 = 0;
for(i = 0; i < 26; i ++){ //推断是否联连通
if(vis[i]&&fat[i] == i) ++flag1;
}
if(flag1 > 1){
printf("The door cannot be opened.\n"); continue;
}
int flag2, flag3; //flag1是推断是否是所有出入度都相等,flag2是判读起始点有几个,flag3是终点有几个
flag1 = flag2 = flag3 = 0;
for(i = 0; i < 26; i ++){
if(vis[i]&&out[i] != in[i]){
++flag1;
if(out[i]-in[i] == 1) ++flag2;
if(in[i] - out[i] == 1) ++flag3;
}
}
if(flag1 == 0) printf("Ordering is possible.\n");
else if(flag1 == 2&&flag2 == 1&&flag3 == 1) printf("Ordering is possible.\n");
else printf("The door cannot be opened.\n");
}
return 0;
}
hdoj 1116 Play on Words 【并查集】+【欧拉路】的更多相关文章
- Colored Sticks (字典树哈希+并查集+欧拉路)
Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27704 Accepted: 7336 Description You ...
- poj2513--并查集+欧拉路+字典树
经典好题,自己不知道哪里错了交上去是RE,可能是数组开的不好吧,字典树老碰到这种问题.. 先马上别人的代码,有空对拍看看 #include <cstdio> #include <cs ...
- poj 2513 Colored Sticks (trie树+并查集+欧拉路)
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 40043 Accepted: 10406 ...
- NYOJ--42--dfs水过||并查集+欧拉通路--一笔画问题
dfs水过: /* Name: NYOJ--42--一笔画问题 Author: shen_渊 Date: 18/04/17 15:22 Description: 这个题用并查集做,更好.在练搜索,试试 ...
- BZOJ 1116 [POI2008]CLO(并查集)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1116 [题目大意] Byteotia城市有n个towns,m条双向roads.每条ro ...
- hdoj 2473 Junk-Mail Filter【并查集节点的删除】
Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- POJ 2513 Colored Sticks (离散化+并查集+欧拉通路)
下面两个写得很清楚了,就不在赘述. http://blog.sina.com.cn/s/blog_5cd4cccf0100apd1.htmlhttp://www.cnblogs.com/lyy2890 ...
- Colored Sticks POJ - 2513 并查集+欧拉通路+字典树hash
题意:给出很多很多很多很多个棒子 左右各有颜色(给出的是单词) 相同颜色的可以接在一起,问是否存在一种 方法可以使得所以棒子连在一起 思路:就是一个判欧拉通路的题目,欧拉通路存在:没奇度顶点 或者 ...
- Play on Words HDU - 1116 (并查集 + 欧拉通路)
Play on Words HDU - 1116 Some of the secret doors contain a very interesting word puzzle. The team o ...
- hdoj 4786 Fibonacci Tree【并查集+最小生成树(kruskal算法)】
Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
随机推荐
- codevs 1450 xth 的旅行
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 毕业了,Xth很高兴,因为他要和他的 ra ...
- (入门SpringBoot)SpringBoot结合定时任务task(十)
SpringBoot整合定时任务task 使用注解EnableScheduling在启动类上. 定义@Component作为组件被容器扫描. 表达式生成地址:http://cron.qqe2.com ...
- api 接口开发理论 在php中调用接口以及编写接口
如: http://localhost/openUser.php?act=get_user_list&type=json 在这里openUser.php相当于一个接口,其中get_user_l ...
- FTP经典常用命令
FTP命令是Internet用户使用最频繁的命令之一,不论是在DOS还是UNIX操作系统下使用FTP,都会遇到大量的FTP内部命令. 熟悉并灵活应用FTP的内部命令,可以大大方便使用者,并收到事半功倍 ...
- 黑苹果+win10双系统折腾笔记
寒假趁机在家折腾一下黑苹果 笔记本配置:神船K610D I7 4600 ,其他配置思路一样,驱动要自己找 镜像和工具:OS X Yosemite 10.10.3 镜像 WIN10 TLSB 2016 ...
- 2017.2.20 activiti实战--第五章--用户与组及部署管理(一)用户与组
学习资料:<Activiti实战> 第五章 用户与组及部署管理(一)用户与组 内容概览:讲解activiti中内置的一套用户.组的关系,以及如何通过API添加.删除.查询. 5.1 用户与 ...
- 给java类加static修饰编译器会说什么?
Illegal modifier for the class XXX;only public abstract & final are permitted.
- 移动端开发者福利-免费收费api收藏
一 .api 1.https://www.juhe.cn/ 跟百度api集市差不多,超级赞,做好认证就行了,我有20+认证能用的免费api 2.http://apistore.baidu.com/as ...
- UNP学习笔记(第二十六章 线程)
线程有时称为轻权进程(lightweight process) 同一进程内的所有线程共享相同的全局内存.这使得线程之间易于共享信息,然后这样也会带来同步的问题 同一进程内的所有线程处理共享全局变量外还 ...
- Activity 事件以及如何得到新打开Activity关闭后返回的数据
1: package com.example.activity_basic; 2: 3: import android.os.Bundle; 4: import android.app.Activ ...