题意:将n个单词排成一个序列,保证相邻单词相邻处字母相同。

分析:每个单词看做一条有向边,字母为点,并查集看图是否连通,因为是有向图,所以最多只能有两个点入度不等于出度,且这两个点一个入度比出度大1,一个出度比入度大1

并查集,单词的首字母是尾字母的祖先。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , -, , -, -, , };
const int dc[] = {-, , , , -, , -, };
const int MOD = 1e9 + ;
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
char s[MAXN];
int fa[];
int in[];
int out[];
int Find(int x){
return fa[x] = (x == fa[x]) ? x : Find(fa[x]);
}
int main(){
int T;
scanf("%d", &T);
while(T--){
for(int i = ; i <= ; ++i){
fa[i] = i;
}
memset(in, , sizeof in);
memset(out, , sizeof out);
int n;
scanf("%d", &n);
for(int i = ; i < n; ++i){
scanf("%s", s);
int len = strlen(s);
int x = s[] - 'a';
int y = s[len - ] - 'a';
++in[y];
++out[x];
int tx = Find(x);
int ty = Find(y);
fa[ty] = tx;//首字母是尾字母的祖先
}
bool ok = true;
int cnt = ;
for(int i = ; i < ; ++i){//统计连通块个数
if((in[i] || out[i]) && fa[i] == i){
++cnt;
}
}
if(cnt > ) ok = false;//图不连通
int num1 = ;//入度比出度大1的结点数
int num2 = ;//出度比入度大1的结点数
for(int i = ; i < ; ++i){
if(!ok) break;
if(in[i] != out[i]){
if(in[i] - out[i] == ) ++num1;
else if(out[i] - in[i] == ) ++num2;
else{
ok = false;
break;
}
}
}
if(ok){
if(!((num1 == && num2 == ) || (num1 == && num2 == ))) ok = false;
}
if(!ok){
printf("The door cannot be opened.\n");
}
else{
printf("Ordering is possible.\n");
}
}
return ;
}

UVA - 10129 Play on Words(欧拉回路)的更多相关文章

  1. Play on Words UVA - 10129 欧拉路径

    关于欧拉回路和欧拉路径 定义:欧拉回路:每条边恰好只走一次,并能回到出发点的路径欧拉路径:经过每一条边一次,但是不要求回到起始点 ①首先看欧拉回路存在性的判定: 一.无向图每个顶点的度数都是偶数,则存 ...

  2. UVa 10129单词(欧拉回路)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  3. UVA - 10129 Play on Words(欧拉回路+并查集)

    2.解题思路:本题利用欧拉回路存在条件解决.可以将所有的单词看做边,26个字母看做端点,那么本题其实就是问是否存在一条路径,可以到达所有出现过的字符端点.由于本题还要求了两个单词拼在一起的条件是前一个 ...

  4. UVA - 10129 Play on Words (欧拉回路+并查集)

    思路: 分别存下每个字符串的首尾字符,以字符为结点,单词看作一条变,就变成了求欧拉回路了,先判断下图是否连通,然后根据欧拉回路的结论:最多只能有两个点的入读不等于初读,而且必须是一个点的出度恰好比入度 ...

  5. 【紫书】Play on Words UVA - 10129 欧拉回路

    题意:给你1e5个字符串,若前一个的末尾字母等于当前的首字母,则可以连在一起(成语接龙一个意思)判断是否可以将他们连在一起 题解:将首位看作点,单词看作边.变成欧拉回路问题. 判断出入度是否相等,再用 ...

  6. Play on Words UVA - 10129 (欧拉回路)

    题目链接:https://vjudge.net/problem/UVA-10129 题目大意:输入N  代表有n个字符串  每个字符串最长1000  要求你把所有的字符串连成一个序列  每个字符串的第 ...

  7. Uva 10129 单词

    题目链接:https://uva.onlinejudge.org/external/101/10129.pdf 把单词的首字母和最后一个字母看做节点,一个单词就是一个有向边.有向图的欧拉定理,就是除了 ...

  8. Uva 10129 - Play on Words 单词接龙 欧拉道路应用

    跟Uva 10054很像,不过这题的单词是不能反向的,所以是有向图,判断欧拉道路. 关于欧拉道路(from Titanium大神): 判断有向图是否有欧拉路 1.判断有向图的基图(即有向图转化为无向图 ...

  9. UVA 10054 The Necklace(欧拉回路,打印路径)

    题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

随机推荐

  1. springboot#下载文件

    膜拜大神 这就是我要的滑板鞋! @RequestMapping(value = "/media", method = RequestMethod.GET) public Respo ...

  2. 128、Java面向对象之对象的比较

    01.代码如下: package TIANPAN; class Book { private String title; private double price; public Book(Strin ...

  3. C/C++网络编程2——socket函数

    本节主要介绍创建套接字的socket函数. #include <sys/socket.h> int socket(int domain, int type, int protocol); ...

  4. 思科 ASA 系列防火墙 官方文档下载指南

    思科 ASA 系列命令参考 思科 ASA 系列命令参考,A 至 H 命令 思科 ASA 系列命令参考, I 至 R 命令 思科 ASA 系列命令参考,S 命令 思科 ASA 系列命令参考, ASASM ...

  5. 远程服务器使用tensorboard

    1 .由于服务器上tensorboard使用的端口是6006,因此,连接ssh时,将服务器的6006端口重定向到自己机器上的16006端口: ssh -L 16006:127.0.0.1:6006 u ...

  6. HashMap1.8之节点删除分析

    HashMap之节点删除 大家一直关注的都是HashMap如何添加节点,当节点数量大于8的时候转化为红黑树,否则使用链表等等,但大家是否有看过删除节点的处理逻辑呢? 今天来看看HashMap删除节点的 ...

  7. Django3.0中向后不兼容的更改

    3.0中向后不兼容的更改 数据库后端API 本节描述了第三方数据库后端中可能需要的更改. 现在的第二个参数DatabaseIntrospection.get_geometry_type()是行描述,而 ...

  8. Day8 - C - Largest Rectangle in a Histogram HDU - 1506

    A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rec ...

  9. python2学习------基础语法5(文件操作)

    1.文件内容读取 a.txt teestttttttttttttt teestttttttttttttt teestttttttttttttt teestttttttttttttt teesttttt ...

  10. shell 脚本终止进程

    参考:https://blog.csdn.net/zhaoyue007101/article/details/7699259 $(pidof 进程名关键字)