Play on Words HDU - 1116 (并查集 + 欧拉通路)
Play on Words HDU - 1116
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.
InputThe 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.
OutputYour 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. 题意:给你一些英文字母,不同的英文字母之间可以首位连接,只要字母一样,问可不可以构成一个完整的一条链
题解:一开始想把每一个单词都看作是一个点来跑,但是很难实现(有点像哈密顿图),之后我就把英文的26个字母当作是点,然后按照单词来建边,当作欧拉通路来跑。
有向的欧拉通路:只有两个节点的出入度不一样(一个出度比入度大一,一个入度比出度大一),其余所有的点的出入度都是一样的。同时还需要判断是不是一个连通图,那就是看是不是在同一个集合(根节点是不是同一个就可以了)
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<map>
#include<cstdlib>
#include<vector>
#include<string>
#include<queue>
using namespace std; #define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
const double PI = acos(-1.0);
const int maxn = 1e3+;
const int mod = 1e9+;
int par[];
void init()
{
for(int i=;i<;i++)
par[i] = i;
}
int find(int x)
{
return par[x]!=x ? find(par[x]) : x;
}
void combine(int a,int b)
{
a = find(a);
b = find(b);
if(a != b)
par[a] = b;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int st[],en[];
memset(st,,sizeof st);
memset(en,,sizeof en);
int n;
scanf("%d",&n);
init();
for(int i=;i<n;i++)
{
char str[];
scanf("%s",str);
int a=str[]-'a';
int b=str[strlen(str)-]-'a';
st[a]++;
en[b]++;
combine(a,b);
}
int start;
for(int i=;i<;i++)
{
if((st[i] || en[i]) && i == find(i))
{
//cout<<i<<endl;
start = i;
break;
}
}
int ans = ;
bool connect = ;
for(int i=;i<;i++)
{
ans += abs(st[i]-en[i]);
if((st[i] || en[i]) && start != find(i))
connect = ;
}
if(ans > || connect == )
puts("The door cannot be opened.");
else
puts("Ordering is possible.");
}
}
Play on Words HDU - 1116 (并查集 + 欧拉通路)的更多相关文章
- Colored Sticks POJ - 2513 并查集+欧拉通路+字典树hash
题意:给出很多很多很多很多个棒子 左右各有颜色(给出的是单词) 相同颜色的可以接在一起,问是否存在一种 方法可以使得所以棒子连在一起 思路:就是一个判欧拉通路的题目,欧拉通路存在:没奇度顶点 或者 ...
- NYOJ--42--dfs水过||并查集+欧拉通路--一笔画问题
dfs水过: /* Name: NYOJ--42--一笔画问题 Author: shen_渊 Date: 18/04/17 15:22 Description: 这个题用并查集做,更好.在练搜索,试试 ...
- hdu 1116 并查集判断欧拉回路通路
判断一些字符串能首尾相连连在一起 并查集求欧拉回路和通路 Sample Input 3 2 acm ibm 3 acm malform mouse 2 ok ok Sample Output The ...
- hdu 1116(并查集+欧拉路径)
Play on Words Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- hdu 4514 并查集+树形dp
湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- HDU 3926 并查集 图同构简单判断 STL
给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...
- HDU 4496 并查集 逆向思维
给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...
- POJ 2513 无向欧拉通路+字典树+并查集
题目大意: 有一堆头尾均有颜色的木条,要让它们拼接在一起,拼接处颜色要保证相同,问是否能够实现 这道题我一开始利用map<string,int>来对颜色进行赋值,好进行后面的并查操作以及欧 ...
- HDU 1232 并查集/dfs
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...
随机推荐
- usb-host一步一步学(一)安卓在usb-host模式下列出当前连接的usb设备
在本次尝试中,我的安卓手机(HTC One X) 通过OTG线作为usb主机模式列出当前插入的usb设备,版本要求minSDKVersion="12". 没有外设的情况下,结果如下 ...
- 用python计算直角三角形斜边长
直接上代码 import math def hypotenuse(a,b): return(math.sqrt(a**2+b**2)) side1 = int(input("第一条直角边:& ...
- Javascript Functions
Javascript 全局对象 全局属性和函数可用于所有内建的Javascript对象 顶层函数(全局函数) decodeURI()解码某个编码的URI. decodeURIComponent()解码 ...
- 独立安装Oracle Hyperion Enterprise Performance Management 验证过程
在安装EPM的过程中,都是安装既定的操作手册进行,只是一个过程的重复,对自己安装不会留下深刻的印象.根据自己学习体会,制定安装步骤,去验证自己学习过程中的体会,加深学习印象,解决安装中遇到的问题,模仿 ...
- 父类和子类以及super关键字
super和this关键字的特点类似:super代表的是父类对象的引用. 当子父类的成员出现同名时,可以通过super来进行区分. 子类的构造方法中,通过super关键字调用父类的构造方法. publ ...
- Shape详解
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...
- 如何修改HDFS上文件
如果只想append操作: . echo "<Text to append>" | hdfs dfs -appendToFile - yourHdfsPath/test ...
- Wince 6.0获取设备的分辨率 自动设置窗体位置
调用微软提供给wince的API “coredll.dll” [DllImport("coredll.dll")] public static extern int GetSys ...
- 不得不承认pretty-midi很好用,以及一些简单的上手
官方文档在此: http://craffel.github.io/pretty-midi/ 首先我们演示如何将midi文件转变为piano-roll格式(matrix). 现在我们手中有了一个数据集, ...
- 手把手教你用Docker部署一个MongoDB集群
MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中最像关系数据库的.支持类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引 ...