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. SpringMVC---其它常用注解

    常用注解 PathVariable @RequestMapping注解中使用占位符的情况下,需要使用@PathVariable注解指定占位符参数.即指定占位符中的值与方法中哪一个参数进行匹配.如果方法 ...

  2. Android学习笔记(四)之碎片化Fragment实现仿人人客户端的侧边栏

    其实一种好的UI布局,可以使用户感到更加的亲切与方便.最近非常流行的莫过于侧边栏了,其实我也做过很多侧边栏的应用,但是那些侧边栏的使用我 都不是很满意,现在重新整理,重新写了一个相对来说我比较满意的侧 ...

  3. Entity Framework(二)

    1. ORM :Object Relation Mapping ,通俗说:用操作对象的方式来操作数据库. 2. 插入数据不再是执行Insert,而是类似于 Person p=new Person() ...

  4. 孤荷凌寒自学python第四十七天通用跨数据库同一数据库中复制数据表函数

    孤荷凌寒自学python第四十七天通用跨数据库同一数据库中复制数据表函数 (完整学习过程屏幕记录视频地址在文末) 今天继续建构自感觉用起来顺手些的自定义模块和类的代码. 今天打算完成的是通用的(至少目 ...

  5. 团队项目-第八次scrum 会议

    时间:11.4 时长:30分钟 地点:F楼2层沙发休息处 工作情况 团队成员 已完成任务 待完成任务 解小锐 修复员工招聘时bug 完成员工commit函数的数值函数编写 陈鑫 实现雇佣与解雇功能的界 ...

  6. [常识]Windows系统里休眠和睡眠的区别?

    睡眠和休眠都是笔记本电脑的节能方式,但有细微的差别: 睡眠还保持着开机状态的,休眠是关机了,但是再次开机之后和关闭时的系统状态是一样的. 睡眠还是保持着系统运行数据在内存中,而休眠则将内存中的数据保存 ...

  7. Oracle 数据库导出时 EXP-00008;ORA-00904

    问题是客户端和服务器端版本问题,我本地是11g,而服务器端是10g. 规则1:低版本的exp/imp可以连接到高版本(或同版本)的数据库服务器,但高版本的exp/imp不能连接到低版本的数据库服务器. ...

  8. CodeForces A. Many Equal Substrings

    http://codeforces.com/contest/1029/problem/A You are given a string tt consisting of nn lowercase La ...

  9. 三、vue依赖收集

    Vue 会把普通对象变成响应式对象,响应式对象 getter 相关的逻辑就是做依赖收集,这一节我们来详细分析这个过程 Dep Dep 是整个 getter 依赖收集的核心,它的定义在 src/core ...

  10. 替换localhost:8080(假域名,本地使用)

    1. 更改servers 的端口号为 80 (默认 8080),此时就不用再输入 8080了. 2. 找到 C:\Windows\System32\drivers\etc 下的 hosts 文件,用记 ...