Play on Words

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7080    Accepted Submission(s): 2398

Problem Description
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
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
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
 
Sample Output
The door cannot be opened.
Ordering is possible.
The door cannot be opened.
 
题意:判断所给的单词能否首尾相连成一串。
题解:1.判断连通分量
   2.判断欧拉路径
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int N = ;
bool vis[N]; ///判断当前字母是否出现过
int father[N];
int indegree[N],outdegree[N]; ///入度和出度
int _find(int x){
if(x==father[x]) return x;
return _find(father[x]);
}
int Union(int a,int b){
int x = _find(a);
int y = _find(b);
if(x==y) return ;
father[x] = y;
return ;
}
void init(){
memset(vis,false,sizeof(vis));
memset(indegree,,sizeof(indegree));
memset(outdegree,,sizeof(outdegree));
for(int i=;i<N;i++) father[i]=i;
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--){
int n;
char str[];
scanf("%d",&n);
init();
while(n--){
scanf("%s",str);
int len = strlen(str);
int s = str[]-'a';
int e = str[len-]-'a';
Union(s,e);
vis[s] = vis[e] = true;
indegree[e]++;
outdegree[s]++;
}
int ans = ;
bool flag=false,flag1=false;
int in=,out=;
for(int i=;i<N;i++){
if(vis[i]){
if(father[i]==i) ans++;
if(indegree[i]!=outdegree[i]){
if(indegree[i]-outdegree[i]==) in++;
else if(outdegree[i]-indegree[i]==) out++;
else flag1 = true;
}
}
if(ans>){
flag = true;
}
}
if(!flag&&!flag1&&in==&&out==) printf("Ordering is possible.\n");
else if(!flag&&!flag1&&in==&&out==) printf("Ordering is possible.\n");
else printf("The door cannot be opened.\n");
}
}

hdu 1116(并查集+欧拉路径)的更多相关文章

  1. hdu 1116 并查集和欧拉路径

    ---恢复内容开始--- 把它看成是一个图 只是需要欧拉路径就可以了 首尾能连成一条线即可 如果要判断这个图是否连通 得用并查集 在hrbust oj里面看答案学到的方法 不用各种for循环套着判断能 ...

  2. hdu 1116 并查集判断欧拉回路通路

    判断一些字符串能首尾相连连在一起 并查集求欧拉回路和通路 Sample Input 3 2 acm ibm 3 acm malform mouse 2 ok ok Sample Output The ...

  3. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  4. HDU 3926 并查集 图同构简单判断 STL

    给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...

  5. HDU 4496 并查集 逆向思维

    给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...

  6. hdu 1116 Play on Words 欧拉路径+并查集

    Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  7. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...

  8. HDU 2860 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...

  9. UVa 10129 (并查集 + 欧拉路径) Play on Words

    题意: 有n个由小写字母的单词,要求判断是否存在某种排列使得相邻的两个单词,前一个单词末字母与后一个单词首字母相同. 分析: 将单词的两个字母看做节点,则一个单词可以看做一条有向边.那么题中所求的排列 ...

随机推荐

  1. vue-cli 引入axios

    写文章注册登录     首页 下载App × vue-cli 引入axios及跨域使用 星球小霸王 关注 2017.10.04 16:40* 字数 504 阅读 13038评论 2喜欢 18 使用 c ...

  2. TCP close seq问题

    测试mt_hls一条流时,发现会话的时长总是对应不上. 仔细观察发现: 注意 1.包1735 (客户端) 发送FIN 请求,seq = 2435582428 2.包1736,1737,1738 (服务 ...

  3. jpg、png、gif图片格式的浅析

    原文地址:图片格式与设计那点事儿 之前面试时被面试官问到了jpg.gif.png格式的区别,当时就扯了一些,感觉都是扯淡,上网搜了下,分享一篇文章 第一次写技术博客,有不尽如人意的地方,还请见谅和指正 ...

  4. 19、AngularJs知识点总结 part-1

    1.AngularJs AngularJs是一款JavaScript开源库,由Google维护,用来协助单一页面应用程序: AngularJs的目标是通过MVC模式增强基于浏览器的应用,使开发和测试变 ...

  5. 4.实现简单的shell sed替换功能

    # -*- coding:utf-8 -*- # Author: JACK ZHAO # 程序1: 实现简单的shell sed替换功能 import sys #判断参数个数 if len(sys.a ...

  6. django QuerySet 的常用API

    为了加深对queryset对象api的了解,我们建立了以下示例模型: from django.db import models class Author(models.Model): "&q ...

  7. 解决使用vim-go插件时候保存go代码导致设置好的折叠消失的问题

    我之前在用vim编辑python代码的时候,折叠的功能都没啥问题 后来在编辑go代码的时候,我发现我一保存,折叠全都消失了,我很费解,就推断跟我使用的插件有关系,因为我保存的时候会触发gofmt插件格 ...

  8. 团队冲刺Alpha(十)

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...

  9. DPDK的代码规范

    每个公司都会有自己代码风格或者编程规范,都旨在防范编程语言的一些陷阱或者提高代码效率,还有就是保持一致编码风格来提高代码可读性,方便code review: 或者说代码的一种美学,比如python也就 ...

  10. SQL视频总结

    SQL是英文Structured Query Language的缩写,意思为结构化查询语言. SQL语言的主要功能就是同各种数据库建立联系,进行沟通.SQL被作为关系型数据库管理系统的标准语言. SQ ...