#include <cstdio>
#include <cstring>
#include <vector> using namespace std; const int maxn = + ; // at most 1000 lowercase characters // union find set, each set is a tree
// only letters 'a' through 'z' will appear, so totally 26 nodes
const int maxc = ; int parent[maxc]; // parent of each node // find the root node of the set of node x
int findSet(int x) {
return parent[x] == x ? x : findSet(parent[x]);
} int used[maxc];
int degree[maxc]; // degree = out degree - in degree int main() {
int T;
int N; // 1 <= N <= 100000
char word[maxn]; scanf("%d", &T);
while(T--) {
scanf("%d", &N); memset(used, , sizeof(used));
memset(degree, , sizeof(degree)); // init the sets
for(int i = ; i < maxc; i++) parent[i] = i;
int numSet = maxc; for(int i = ; i < N; i++) { // read each word
scanf("%s", word);
int first = word[] - 'a';
int last = word[strlen(word) - ] - 'a';
// there is an edge first --> last
degree[first]++;// out degree
degree[last]--;// in degree used[first] = used[last] = ; int setFirst = findSet(first);
int setLast = findSet(last); if(setFirst != setLast) {
// union
parent[setFirst] = setLast;
numSet--;
}
} vector<int> d;
for(int i = ; i < maxc; i++) {
if(!used[i])
numSet--;
else if (degree[i] != )
d.push_back(degree[i]);
} bool ok = false;
if(numSet == && // all nodes are connected
// Euler circuit, all nodes have degree 0
(d.empty() ||
// Euler path, if d[0] == 1, then d[1] == -1, and vice versa
(d.size() == && (d[] == || d[] == -))))
ok = true; if(ok) printf("Ordering is possible.\n");
else printf("The door cannot be opened.\n");
} return ;
}

LRJ-Example-06-16-Uva10129的更多相关文章

  1. AtCoder Beginner Contest 100 2018/06/16

    A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...

  2. 2016/06/16 phpexcel

      程序部分   require_once './phpexcel/PHPExcel.php';   // 首先创建一个新的对象  PHPExcel object $objPHPExcel = new ...

  3. 【NLP新闻-2013.06.16】Representative Reviewing

    英语原文地址:http://nlp.hivefire.com/articles/share/40221/ 注:本人翻译NLP新闻只为学习专业英语和扩展视野,如果翻译的不好,请谅解! (实在是读不大懂, ...

  4. [每日一题2020.06.16] leetcode双周赛T3 5423 找两个和为目标值且不重叠的子数组 DP, 前缀和

    题目链接 给你一个整数数组 arr 和一个整数值 target . 请你在 arr 中找 两个互不重叠的子数组 且它们的和都等于 target .可能会有多种方案,请你返回满足要求的两个子数组长度和的 ...

  5. H3C汇聚层交换机认证在线人数展示系统之CheckList和燃尽图(16/04/06-16/04/13)

    一.CheckList(核查表) 序号 事件 计划完成时间 实际完成时间 未延迟 未完成 完成 1 登录口令加密以及解密 16/04/06   16/04/06 Y     2 表的创建和IP以及口令 ...

  6. JavaSE学习总结第06天_Java语言基础2 & 面向对象1

      06.01 二维数组概述和格式1的讲解 二维数组概述:二维数组其实就是一个元素为一维数组的数组 格式1:数据类型[][] 变量名 = new 数据类型[m][n]; m表示这个二维数组有多少个一维 ...

  7. hadoop 2.7.3本地环境运行官方wordcount

    hadoop 2.7.3本地环境运行官方wordcount 基本环境: 系统:win7 虚机环境:virtualBox 虚机:centos 7 hadoop版本:2.7.3 本次先以独立模式(本地模式 ...

  8. WebAPi之SelfHost自创建证书启动Https疑难解惑及无法正确返回结果

    前言 话说又来需求了,之前对于在SelfHost中需要嵌套页面并操作为非正常需求,这回来正常需求了,客户端现在加了https,老大过来说WebAPi访问不了了,这是什么情况,我去试了试,还真是这个情况 ...

  9. SQL Server-聚焦IN VS EXISTS VS JOIN性能分析(十九)

    前言 本节我们开始讲讲这一系列性能比较的终极篇IN VS EXISTS VS JOIN的性能分析,前面系列有人一直在说场景不够,这里我们结合查询索引列.非索引列.查询小表.查询大表来综合分析,简短的内 ...

  10. ASP.NET MVC5+EF6+EasyUI 后台管理系统(37)-文章发布系统④-百万级数据和千万级数据简单测试

    系列目录 我想测试EF在一百万条数据下的显示时间!这分数据应该有很多同学想要,看看EF的性能! 服务器 现在来向SQL2008R2插入1000000条数据吧 declare @i int; ; beg ...

随机推荐

  1. Anngular.js的大致了解

    只要出发事件 这个事件会刷新DOM节点,去重新渲染DOM节点(把dom擦掉重新画) 浪费内存 浪费效率所以使用Angular.js -懒查找 不涉及渲染 效率最高 展现效率 比原声使用两个事件互相监听 ...

  2. Leetcode501.Find Mode in Binary Search Tree二叉搜索树中的众数

    给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素). 假定 BST 有如下定义: 结点左子树中所含结点的值小于等于当前结点的值 结点右子树中所含结点的值大于等于当 ...

  3. 关于rss的内容(转载)

    转载自: https://blog.csdn.net/zhao1949/article/details/52806123 (本文对读者有帮助的话请移步支持原作者) 内容记录: 在C++技术网开通了RS ...

  4. lingo 出现63. MODEL IS ILL DEFINED 解决办法

    63. MODEL IS ILL DEFINED. CHECK FOR UNDEFINED INDICES AND/OR CONDITIONS IN EXPRESSION: EXPRESSION. 还 ...

  5. 2019-2-16-WPF-封装-dotnet-remoting-调用其他进程

    title author date CreateTime categories WPF 封装 dotnet remoting 调用其他进程 lindexi 2019-02-16 09:40:26 +0 ...

  6. docker相关教程【转】

    https://www.w3cschool.cn/docker/docker-run-command.html 运行容器 https://www.runoob.com/docker/docker-im ...

  7. 服务端关闭session的重要性

    原因如下: 如果你的站点是一个每日请求上百万级规模的接口服务, 每一次请求(这里应该是没有携带cookie的请求)都会在服务端产生一个session会话,也就是一个小文件. 一般而言,php的sess ...

  8. 【codeforces Manthan, Codefest 17 C】Helga Hufflepuff's Cup

    [链接]h在这里写链接 [题意]     k是最高级别的分数,最高界别的分数最多只能有x个.     1<=k<=m;     和k相邻的点的分数只能小于k;     n个点的树,问你每个 ...

  9. Sublime Text3 安装less

    1.安装Sublime 插件 (1)安装LESS插件:因为Sublime不支持Less语法高亮,所以,先安装这个插件,方法: ctrl+shift+p>install Package>输入 ...

  10. iOS:你App的设置做对了吗?

    http://www.cocoachina.com/ios/20151217/14707.html iOS 8及以上版本最不为人知的一个特点是与应用设置的深层链接,用户可以根据APP的需要授权启用位置 ...