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. 深入理解synchronize

    本文参考引用,本人整理个人理解.地址点击 1.实现原理 synchronized可以保证方法或者代码块在运行时,同一时刻只有一个方法可以进入到临界区,同时它还可以保证共享变量的内存可见性. 下面是一些 ...

  2. eclipse importing maven projects 卡顿

    导入一个maven工程后 一直显示 importing maven projects 9% 解决办法: 找到eclipse安装目录下的eclipse.ini 在最后加入 -vm $JAVA_HOME% ...

  3. 孤荷凌寒自学python第六十二天学习mongoDB的基本操作并进行简单封装1

    孤荷凌寒自学python第六十二天学习mongoDB的基本操作并进行简单封装1 (完整学习过程屏幕记录视频地址在文末) 今天是学习mongoDB数据库的第八天. 今天开始学习mongoDB的简单操作, ...

  4. Actiivity 生命周期

    Actiivity 生命周期,如下图所示: onCreate onStart (onRestarted) onResume onPaused(to onResume(User navigates to ...

  5. Javascript 基础总结

    一.预处理 console.log(global); // undefined var global = 'global'; console.log(global); // global functi ...

  6. qemu中的网络设置

    https://www.cnblogs.com/hukey/p/6436211.html 这个链接里教你怎么操作kvm的各种网络模式,实际操作成

  7. BZOJ 1912:[Apio2010]patrol 巡逻(树直径)

    1912: [Apio2010]patrol 巡逻 Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示村庄a与b之间有一条道路(1 ≤ ...

  8. 洛谷 P3157 [CQOI2011]动态逆序对 | CDQ分治

    题目:https://www.luogu.org/problemnew/show/3157 题解: 1.对于静态的逆序对可以用树状数组做 2.我们为了方便可以把删除当成增加,可以化动为静 3.找到三维 ...

  9. Codeforces 835 F Roads in the Kingdom(树形dp)

    F. Roads in the Kingdom(树形dp) 题意: 给一张n个点n条边的无向带权图 定义不便利度为所有点对最短距离中的最大值 求出删一条边之后,保证图还连通时不便利度的最小值 $n & ...

  10. javascript传递参数如果是object的话,是按值传递还是按引用传递?

    作者:苏墨橘链接:https://www.zhihu.com/question/27114726/answer/35481766来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...