#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. 按照in条件排序

    --按照in中条件排序 ,,,) order by instr('395,396,399,313',ghdm);

  2. I Love Palindrome String

    I Love Palindrome String 时间限制: 2 Sec  内存限制: 128 MB 题目描述 You are given a string S=s1s2..s|S| containi ...

  3. 洛谷P1311 [NOIP2011提高组Day1T2]选择客栈

    P1311 选择客栈 题目描述 丽江河边有n 家很有特色的客栈,客栈按照其位置顺序从 1 到n 编号.每家客栈都按照某一种色调进行装饰(总共 k 种,用整数 0 ~ k-1 表示),且每家客栈都设有一 ...

  4. js中index()的四种经典用法111

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. hdu 2444 The Accomodation of Students(二分匹配 匈牙利算法 邻接表实现)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  6. LintCode_175 翻转二叉树

    题目 翻转一棵二叉树 您在真实的面试中是否遇到过这个题? Yes 样例 1 1 / \ / \ 2 3 => 3 2 / \ 4 4 和前序遍历代码很相似从叶子节点依次翻转递归到根节点C++代码 ...

  7. python3.7 安装gensim使用word2Vec库

    应用的文章(个人试验过,完全正确):https://radimrehurek.com/gensim/index.html#install

  8. Lunix文件的读写权限问题

    今天把在windows平台build好的lunix执行文件复制到Lunix的虚拟机上,发现可执行文件不能执行了,后来才发现文件时有读写和可执行权限的,但是是可以更改的,右键属性,把可执行属性勾选上就可 ...

  9. thinkphp5.0 模板输出常用内容

    1.在模板获取session和cookie等全局变量 {$Think.session.user_id}//输出$_SESSION['user_id']变量 2.获取请求信息 {$Request.con ...

  10. 连接池c3p0

    连接池c3p0 C3P0:hibernate和spring使用,有自动回收空闲连接的功能. 使用步骤: 1.导入jar包(c3p0-0.9.1.2.jar) 2.使用api a.硬编码(不推荐) ne ...