题目链接

题意:给出一些单词,问全部单词能否首尾相连

直接 将每一个单词第一个和最后一个字母建立一条有向边,保证除了首尾两个出入度不相等,其他的要保证相等。还有一个条件就是 首尾两个出入度差为1

同时并查集判连通

 #include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int Max = + ;
struct Node
{
int x, y;
};
char str[Max];
Node node[Max];
int indegree[], outdegree[];
int father[], vis[];
int find_father(int x)
{
if (x == father[x])
return x;
return father[x] = find_father(father[x]);
}
int unionset(int x, int y)
{
x = find_father(x);
y = find_father(y);
if (x == y)
return false;
father[y] = x;
return true;
}
int main()
{
int T, n;
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
memset(indegree, , sizeof(indegree));
memset(outdegree, , sizeof(outdegree));
memset(vis, , sizeof(vis));
for (int i = ; i < ; i++)
father[i] = i;
int setcnt = ;
for (int i = ; i <= n; i++)
{
scanf("%s", str);
node[i].x = str[] - 'a';
node[i].y = str[ strlen(str) - ] - 'a';
indegree[node[i].y]++;
outdegree[node[i].x]++;
vis[node[i].x] = ;
vis[node[i].y] = ;
if (unionset(node[i].x, node[i].y))
{
setcnt++;
}
}
int numcnt = ;
for (int i = ; i < ; i++)
{
if (vis[i])
numcnt++;
}
if (setcnt != (numcnt - ))
{
printf("The door cannot be opened.\n");
continue;
}
if (n == )
{
printf("Ordering is possible.\n");
continue;
}
int x = , y = , z = ;
for (int i = ; i < ; i++)
{
if (vis[i] && indegree[i] != outdegree[i])
{
if (indegree[i] == outdegree[i] + ) //首
x++;
else if (indegree[i] + == outdegree[i]) // 尾
y++;
else
z++;
}
}
if (z)
printf("The door cannot be opened.\n");
else if ( (x == && y == ) || (x == && y == ) ) // 出入度不相等 且 差为1, 或者 是环
printf("Ordering is possible.\n");
else
printf("The door cannot be opened.\n");
/*
int cnt = 0, sum = 0;
for (int i = 0; i < 26; i++)
{
if (indegree[i] > 0 || outdegree[i] > 0)
{
sum++;
if (indegree[i] == outdegree[i])
cnt++;
}
}
if (cnt == sum || ( sum > 2 && sum - cnt == 2))
printf("Ordering is possible.\n");
else
printf("The door cannot be opened.\n");
*/
}
return ;
}

HDU 1116 Play on Words(有向欧拉判断)的更多相关文章

  1. HDU 1286:找新朋友(欧拉函数)

    http://acm.hdu.edu.cn/showproblem.php?pid=1286 题意:中文. 思路:求欧拉函数. #include <cstdio> #include < ...

  2. hdu 3307 Description has only two Sentences (欧拉函数+快速幂)

    Description has only two SentencesTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  3. HDU 6088 Rikka with Rock-paper-scissors(NTT+欧拉函数)

    题意 \(n\) 局石头剪刀布,设每局的贡献为赢的次数与输的次数之 \(\gcd\) ,求期望贡献乘以 \(3^{2n}\) ,定义若 \(xy=0\) 则,\(\gcd(x,y)=x+y\) 思路 ...

  4. HDU 4549 矩阵快速幂+快速幂+欧拉函数

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  5. (hdu step 7.2.2)GCD Again(欧拉函数的简单应用——求[1,n)中与n不互质的元素的个数)

    题目: GCD Again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. hdu 1286 找新朋友 (容斥原理 || 欧拉函数)

    Problem - 1286 用容斥原理做的代码: #include <cstdio> #include <iostream> #include <algorithm&g ...

  7. hdu 3792 Twin Prime Conjecture 前缀和+欧拉打表

    Twin Prime Conjecture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  8. 欧拉函数 & 【POJ】2478 Farey Sequence & 【HDU】2824 The Euler function

    http://poj.org/problem?id=2478 http://acm.hdu.edu.cn/showproblem.php?pid=2824 欧拉函数模板裸题,有两种方法求出所有的欧拉函 ...

  9. hdu2824 The Euler function(欧拉函数个数)

    版权声明:本文为博主原创文章,未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/36426357 题目链接:h ...

随机推荐

  1. Redis的五种数据结构

    Redis支持持久化只是它的一件武器,它提供了多达5种数据存储方式: 一  string(字符串) string是最简单的类型,你可以理解成与Memcached一模一样的类型,一个key对应一个val ...

  2. 在nginx中配置如何防止直接用ip访问服务器web server及server_name特性讲解

    看了很多nginx的配置,好像都忽略了ip直接访问web的问题,不利于SEO优化,所以我们希望可以避免直接用IP访问网站,而是域名访问,具体怎么做呢,看下面. 官方文档中提供的方法: If you d ...

  3. java中的集合和数组

    数组Array和集合的区别: (1)数组是大小固定的,并且同一个数组只能存放类型一样的数据(基本类型/引用类型) (2)JAVA集合可以存储和操作数目不固定的一组数据. (3)若程序时不知道究竟需要多 ...

  4. 十天冲刺---Day1

    站立式会议 由于第一天冲刺,所以有些没有昨天完成项和遇到的问题. 站立式会议内容总结: git上Issues内容: 燃尽图(做错了,将每天的燃尽图误以为是每天添加任务然后到一天结束后生成燃尽图(?)) ...

  5. linux下部署项目问题

    1. 今天linux下部署thinkphp项目,数据库用的mysql. 页面其他都是正常的,但是从数据库中取出的数据都是乱码.最后查了资料 解决方案: 在ThinkPHP里面 Library\Thin ...

  6. Java面试常考知识点

    1.  什么是Java虚拟机?为什么Java被称作是“平台无关的编程语言”? Java虚拟机是一个可以执行Java字节码的虚拟机进程.Java源文件被编译成能被Java虚拟机执行的字节码文件. Jav ...

  7. Mybatis 异常: The content of elements must consist of well-formed character data or markup

    原因很简单:在ibatis的配置文件中不能出现小于号(>)     <delete id="deleteByPrimaryKey" parameterType=&quo ...

  8. 切割haproxy的日志

    日志的切割有以下几种方法: 1.写个定时任务,每天某个时间点把旧的日志重命名,并对服务重启使其重新打开日志并写入. 2.通过管道的方式把新产生的日志写到另外一个日志文件里. 3.通过logrotate ...

  9. 怎么解决mysql不允许远程连接的错误

    最近使用Navicat for MySQl访问远程mysql数据库,出现报错,显示"1130 - Host'xxx.xxx.xxx.xxx' is not allowed to connec ...

  10. nutch-介绍

    本文主要源于http://www.ibm.com/developerworks/cn/opensource/os-cn-nutchintro/ 基本信息 Nutch是一个开放源代码(open-sour ...