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 ...
随机推荐
- WordPress使用固定链接
WordPress安装后我们会发现,文章默认的url是很丑的,http://example.com/?p=N,其中N是文章ID,一串数字.默认链接在所有的环境下都运转良好,但和其他的类型比起来却没那么 ...
- name after, name for, name as
name after, name for, name as name after是一个常见用法 : 1.Her parents named her Sophia after her grandmo ...
- 实现解耦-Spring.Net
spring.net属于IOC(中文名:控制反转)的思想实现. 概念解释: 控制反转概念: 控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来 ...
- iOS开发--录音简单实现
- 【UOJ #150】【NOIP 2015】运输计划
http://uoj.ac/problem/150 用树链剖分求lca,二分答案树上差分判断. 时间复杂度$O(nlogn)$,n,m同阶. #include<cstdio> #inclu ...
- 【POJ 1113】Wall
http://poj.org/problem?id=1113 夏令营讲课时的求凸包例题,据说是PKUSC2015的一道题 我WA两次错在四舍五入上了(=゚ω゚)ノ #include<cmath& ...
- nodeJS+bootstarp+mongodb整一个TODO小例子
又是一个简单的小玩意儿, 不过有个大玩意儿,就是nosql的mongodb(文件大小:130M), 你要下载一个mongdodb, 去官方网站下载 安装以后在mongodb的命令行目录下执行 mon ...
- slim
Slim 是一个非常优雅的 PHP 微框架,非常适合做API,支持多种http请求方式,比如get,post,delete,put等 安装使用Composer composer require sli ...
- iOS黑客技术相关
在黑客的世界里,没有坚不可破的防护系统,也没有无往不胜.所向披靡的入侵利器,有时候看似简单的问题,破解起来也许花上好几天.好几个月,有时候看似很 low 的工具往往能解决大问题:我们以实现微信自动抢红 ...
- bzoj 3223 splay模板题3
水题...貌似理解splay怎么维护数列了... 每个点维护一个size,它的位置就是它的size,区间翻转的话可以打标记,find的时候push_down,交换左右子树. #include<i ...