题目链接

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

直接 将每一个单词第一个和最后一个字母建立一条有向边,保证除了首尾两个出入度不相等,其他的要保证相等。还有一个条件就是 首尾两个出入度差为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. 关于Node.js的httpClieint请求报错ECONNRESET的原因和解决措施

    背景说明 最近在工作项目中有下面一个场景: 使用Node.js的express框架实现了一个文件系统服务器端,其中有个API用于客户端上传文件.客户端使用Node.js的HttpClient来调用服务 ...

  2. Xamarin Android -创建Splash Screen (一)

    ......(空话少说) Xamarin 开发的技术资料很少,通过学习,把自己的学习过程及其中碰到的问题与大家分享. Splash Screen 就是在应用程序启动时,出现的一张图片,一般App的Sp ...

  3. Linux下sysstat工具学习

    Linux下,我们多用ssh链接服务器远程操控.对于系统的监控必不可少,sysstat很不错的监控工具包. sysstat官网:http://sebastien.godard.pagesperso-o ...

  4. jQuery基础--样式篇(4)

    jQuery选择器: 1.id选择器:通过id名来获取节点,id是唯一的,每个id在页面上只能使用一次. $( "#id" ) 2.类选择器:通过class样式类名来获取节点 $( ...

  5. 【BZOJ 2005】【NOI 2010】能量采集 数论+容斥原理

    这题设$f(i)$为$gcd(i,j)=x$的个数,根据容斥原理,我们只需减掉$f(i×2),f(i×3)\cdots$即可 那么这道题:$$ans=\sum_{i=1}^n(f(i)×((i-1)× ...

  6. Oracle数据库下sde用户系统表开放权限sql语句

    --sde用户登陆执行以下语句 grant insert, update, delete on sde.table_registry to PUBLIC; grant insert, update, ...

  7. Activiti 学习笔记记录(二)

    上一篇:Activiti 学习笔记记录 导读:对于工作流引擎的使用,我们都知道,需要一个业务事件,比如请假,它会去走一个流程(提交申请->领导审批---(批,不批)---->结束),Act ...

  8. 【SPOJ QTREE2】QTREE2 - Query on a tree II(LCA)

    You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, ...

  9. 58. Android一些开发习惯总结

    作者:漫步 链接:https://www.zhihu.com/question/27227425/answer/35973793 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...

  10. bzoj 1030 fail树dp

    dp[i][j][0]代表当前匹配到i号点走了j步且没到过单词节点,1代表到过,直接转移. #include<iostream> #include<cstdio> #inclu ...