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 ...
随机推荐
- 富文本KidnEditor在MVC中的应用
最近看到很多网站后台都用到了富文本,包括自己所在的公司也是.公司用的KindEditor,所以就讲讲KindEditor.之前我也没学过,所以网上搜了一篇博文,直接转载如下(PS:完全以学习为目的哦~ ...
- java泛型中的对象
import java.util.HashMap; class Key { String s; Key(String s) { this.s = new String(s); } @Override ...
- java web 100个知识点
http://wenku.baidu.com/link?url=ns5SvKesJSLzpcTckBKsFopqgbC6O0XBuVBS1BZwtJbK1P-aYbNV3fVOU9lYTbGQwKYK ...
- java多态实现原理
众所周知,多态是面向对象编程语言的重要特性,它允许基类的指针或引用指向派生类的对象,而在具体访问时实现方法的动态绑定.C++ 和 Java 作为当前最为流行的两种面向对象编程语言,其内部对于多态的支持 ...
- mailto: HTML e-mail 链接
转载: http://www.haorooms.com/post/mailto_link_html 什么是mailto链接? mailto链接是一种html链接,能够设置你电脑中邮件的默认发送信息.但 ...
- 怎样设置Word下次打开时跳转到上次阅读的位置
①我们启动Word2013,打开需要阅读的文档,当阅读完毕之后,在指定位置键入一个空格,然后按下Delete键删除,这样相当于是没有作任何更改. ②保存文档,单击文件--另存为,选择好路径,将文档保存 ...
- SpringMVC学习--参数绑定
spring参数绑定过程 从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到controller方法的形参上.springmvc中,接收页面提交的数据是通过方法形参来接收 ...
- linux 配置 crontab
linux中的crontab是定时用的,下面的cron服务就是定时器的意思 crontab -l 列举所有的cron服务 crontab -e 修改cron服务 crontab -r 删除cron服务 ...
- 关于QString中的arg()函数使用方法
例:正确做法:ui->label->setText(QString("Processingfile%1").arg(index));错误做法: ui->label ...
- Java GC收集器配置说明
根据Java GC收集器具体分类,我们可以看出JVM根据需求不同提供了三种选择:串行收集器.并行收集器.并发收集器. 串行收集器只适用于小数据量的情况,我们主要了解一下并行收集器和并发收集器.默认情况 ...