HDU 1116 Play on Words(有向欧拉判断)
题意:给出一些单词,问全部单词能否首尾相连
直接 将每一个单词第一个和最后一个字母建立一条有向边,保证除了首尾两个出入度不相等,其他的要保证相等。还有一个条件就是 首尾两个出入度差为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(有向欧拉判断)的更多相关文章
- HDU 1286:找新朋友(欧拉函数)
http://acm.hdu.edu.cn/showproblem.php?pid=1286 题意:中文. 思路:求欧拉函数. #include <cstdio> #include < ...
- hdu 3307 Description has only two Sentences (欧拉函数+快速幂)
Description has only two SentencesTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- HDU 6088 Rikka with Rock-paper-scissors(NTT+欧拉函数)
题意 \(n\) 局石头剪刀布,设每局的贡献为赢的次数与输的次数之 \(\gcd\) ,求期望贡献乘以 \(3^{2n}\) ,定义若 \(xy=0\) 则,\(\gcd(x,y)=x+y\) 思路 ...
- HDU 4549 矩阵快速幂+快速幂+欧拉函数
M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- (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 ...
- hdu 1286 找新朋友 (容斥原理 || 欧拉函数)
Problem - 1286 用容斥原理做的代码: #include <cstdio> #include <iostream> #include <algorithm&g ...
- hdu 3792 Twin Prime Conjecture 前缀和+欧拉打表
Twin Prime Conjecture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 欧拉函数 & 【POJ】2478 Farey Sequence & 【HDU】2824 The Euler function
http://poj.org/problem?id=2478 http://acm.hdu.edu.cn/showproblem.php?pid=2824 欧拉函数模板裸题,有两种方法求出所有的欧拉函 ...
- hdu2824 The Euler function(欧拉函数个数)
版权声明:本文为博主原创文章,未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/36426357 题目链接:h ...
随机推荐
- sql server死锁神器
参考文章: http://blogs.msdn.com/b/sqlserverfaq/archive/2013/04/27/an-in-depth-look-at-sql-server-memory- ...
- B树和B+树
当数据量大时,我们如果用二叉树来存储的会导致树的高度太高,从而造成磁盘IO过于频繁,进而导致查询效率下降.因此采用B树来解决大数据存储的问题,很多数据库中都是采用B树或者B+树来进行存储的.其目的就是 ...
- EXCEL时间日期转换为常规字符显示
当我们做报表导入的时候,我们不得不思考这样一个问题,遇到的数据是时间格式的,而在EXCEL中,时间格式的单元格实际上是以1900年以后来计算的,例如,1900年是闰年(显然可以被4整除),那么1900 ...
- 34-nl 简明笔记
为文本文件添加行号 nl [options] files 参数 files是nl需要为其添加行号的文本文件路径名,如果有多个文件,则nl会把多个文件合在一起编号,并输出到标准输出上 选项 -b ...
- Android判断当前系统时间是否在指定时间的范围内(免消息打扰)
/** * 判断当前系统时间是否在指定时间的范围内 * * @param beginHour * 开始小时,例如22 * @param beginMin * 开始小时的分钟数,例如30 * @para ...
- iOS开发--音乐文件播放工具类的封装(包含了音效的封装)
一.头文件 #import <Foundation/Foundation.h> #import <AVFoundation/AVFoundation.h> @interface ...
- Fresco内存机制(Ashmem匿名共享内存)
Fresco的内存机制 Fresco是Facebook出品的高性能图片加载库,采用了Ashmem匿名共享内存机制, 来解决图片加载中的OOM问题.这里不对Fresco做深入分析,只关注Fresco在A ...
- 【HDU 5839】Special Tetrahedron(计算几何)
空间的200个点,求出至少四边相等,且其余两边必须不相邻的四面体的个数. 用map记录距离点i为d的点有几个,这样来优化暴力的四重循环. 别人的做法是枚举两点的中垂面上的点,再把到中点距离相等的点找出 ...
- 优先队列priority_queue的比较函数
STL头文件:#include<queue> 优先队列: 默认从大到小排列:priority_queuee<node>q; 自定义优先级的三种方法: 1.重载操作符: bool ...
- asp.net mvc 外网获取不到port问题解决
var IPandPort = Request.ServerVariables["HTTP_HOST"];