#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. UVA1204 Fun Game

    Fun Game https://odzkskevi.qnssl.com/8d698323a1e07d605cdeea708ee8a01d?v=1508703139 [题解] 不难发现如果一个串的原串 ...

  2. excel一些常用的函数

    函数分类: 关联匹配类 清洗处理类 逻辑运算类 计算统计类 时间序列类 一.关联匹配类 经常性的,需要的数据不在同一个excel表或同一个excel表不同sheet中,数据太多,copy麻烦也不准确, ...

  3. 批处理启动应用程序(win)

    @echo off net session >nul 2>&1 " ( echo Oops: This tools must run with administrator ...

  4. ES6 中变量的解构赋值

    1. 数组的解构赋值 解构: ES6 中允许按照一定的模式从数组和对象中提取值,然后对变量进行赋值,这被称为解构(Destructuring). 1. 基本用法 本质上,这种写法属于"模式匹 ...

  5. JS---案例:旋转木马

    案例:旋转木马 页面加载时候出现的效果,script标签写在head里面,body上面 显示一个图片散开的动画,遍历之后,把每个图片用封装的动画函数移动到指定目标(同时改变多属性:宽,透明度,层级,t ...

  6. 机器学习之Adaboost算法原理

    转自:http://www.cnblogs.com/pinard/p/6133937.html 在集成学习原理小结中,我们讲到了集成学习按照个体学习器之间是否存在依赖关系可以分为两类,第一个是个体学习 ...

  7. JavaScript--预解析在IE存在的问题

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. ie8 下margin-top失效的小案例

    一个小案例,是关于IE8下的margin-top的失效问题,巨日代码如下: 正常的chrome浏览器下的显示如下: margin-top=10px,正常显示 但是在ie8下,最终样式如下: margi ...

  9. 【python小随笔】进程池 multiprocessing.Pool的简单实现与踩过的坑

    #导入进程模块 import multiprocessing #创建进程池 坑:一定要在循环外面创建进程池,不然会一直创建 pool = multiprocessing.Pool(30) for Si ...

  10. 2019-8-31-C#-循环的判断会进来几次

    title author date CreateTime categories C# 循环的判断会进来几次 lindexi 2019-08-31 16:55:58 +0800 2018-06-14 0 ...