题意:

  给出几个单词,求能否用所有的单词成语接龙

解析:

  把每个单词的首字母和尾字母分别看作两个点u 和 v,输入每个单词后,u的出度++, v的入度++

  最后判断是否能组成欧拉路径 或 欧拉回路,当然首先要判断一下是否是一个连通块,用并查集维护就好了,当然有自环,所以用一个vis标记一下这个点是否出现过

看代码就懂了

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define pd(a) printf("%d\n", a);
#define plld(a) printf("%lld\n", a);
#define pc(a) printf("%c\n", a);
#define ps(a) printf("%s\n", a);
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff;
int head[maxn], in[maxn], out[maxn], f[maxn], vis[maxn];
int n, m, cnt;
set<int> s; int find(int x)
{
return f[x] == x ? x : (f[x] = find(f[x]));
} int main()
{
int T;
cin >> T;
while(T--)
{
s.clear();
mem(in, );
mem(out, );
mem(vis, );
for(int i = ; i <= ; i++) f[i] = i;
string str;
cin >> n;
for(int i = ; i <= n; i++)
{
cin >> str;
int u = str[] - 'a' + ;
int v = str[str.size() - ] - 'a' + ;
int l = find(u);
int r = find(v);
vis[u] = vis[v] = ;
if(l != r) f[l] = r;
out[u]++;
in[v]++;
}
int cnt1 = , cnt2 = , flag = , cnt = ;
for(int i = ; i <= ; i++)
{
int x = find(i);
if(vis[x]) s.insert(x);
if(in[i] != out[i])
flag = , cnt++;
if(in[i] == out[i] + )
cnt1++;
else if(in[i] + == out[i])
cnt2++;
}
if(s.size() != )
{
cout << "The door cannot be opened." << endl;
continue;
}
if(cnt != && cnt != )
{
cout << "The door cannot be opened." << endl;
continue;
}
if(cnt1 == && cnt2 == || flag == )
cout << "Ordering is possible." << endl;
else
cout << "The door cannot be opened." << endl; }
return ;
}

Play on Words HDU - 1116(欧拉路判断 + 并查集)的更多相关文章

  1. hdu 5833(欧拉路)

    The Best Path Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  2. HDU HDU1558 Segment set(并查集+判断线段相交)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1558 解题报告:首先如果两条线段有交点的话,这两条线段在一个集合内,如果a跟b在一个集合内,b跟c在一 ...

  3. HDU - 1272 小希的迷宫 并查集判断无向环及连通问题 树的性质

    小希的迷宫 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一 ...

  4. UVa 10129 Play On Words【欧拉道路 并查集 】

    题意:给出n个单词,问这n个单词能否首尾接龙,即能否构成欧拉道路 按照紫书上的思路:用并查集来做,取每一个单词的第一个字母,和最后一个字母进行并查集的操作 但这道题目是欧拉道路(下面摘自http:// ...

  5. POJ - 2513 Colored Sticks(欧拉通路+并查集+字典树)

    https://vjudge.net/problem/POJ-2513 题解转载自:優YoU  http://user.qzone.qq.com/289065406/blog/1304742541 题 ...

  6. poj2513字典树+欧拉图判断+并查集断连通

    题意:俩头带有颜色的木棒,要求按颜色同的首尾相连,可能否? 思路:棒子本身是一条边,以俩端为顶点(同颜色共点),即求是否有无向图欧拉路(每条棒子只有一根, 边只能用一次,用一次边即选一次棒子). 先判 ...

  7. hdu 1257 小希的迷宫 并查集

    小希的迷宫 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1272 D ...

  8. <hdu - 1272> 小希的迷宫 并查集问题 (注意特殊情况)

     本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 Problem Description: 上次Gardon的迷宫城堡小希玩了很久(见Probl ...

  9. BZOJ 1116 [POI2008]CLO(并查集)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1116 [题目大意] Byteotia城市有n个towns,m条双向roads.每条ro ...

随机推荐

  1. 开发板测试-GPRS

    注意事项: 经过测试,为了使STM32和Air202的串口稳定通信,需要更改 以前电路感觉应该是三极管控制极4.7K电阻太大,又因为开关速度快,然后开关的时候导致不足以让三极管处于全部导通状态,所以电 ...

  2. php和js字符串的acsii码函数

    简单普及下编码知识: javascript中有charCodeAt(),根据字符查找ascii码. String.fromCharCode(),根据ascii码查找对应的字符. console.log ...

  3. SQL2005中的事务与锁定(九)-(2)- 转载

    -------------------------------------------------------------------------- Author : HappyFlyStone -- ...

  4. HTML5 读取上传文件(转载)

    另参考 http://www.jianshu.com/p/46e6e03a0d53 1 filelist对象与file对象: <input type="file" id=&q ...

  5. Dubbo与Zookeeper在Window上的安装与简单使用

    一:Dubbo是什么?有什么用途?? 使用Dubbo可以将应用分布到多个服务器上,当有访问时,Dubbo有帮你管理自动将请求分配给合适得到服务器去执行,即建立多个生产者,建立多个消费者,自动匹配生产者 ...

  6. [HNOI2018]排列[堆]

    题意 给定一棵树,每个点有点权,第 \(i\) 个点被删除的代价为 \(w_{p[i]}\times i\) ,问最小代价是多少. 分析 与国王游戏一题类似. 容易发现权值最小的点在其父亲选择后就会立 ...

  7. 并行管理工具——pdsh

    1. pdsh安装2. pdsh常规使用2.1 pdsh2.2 pdcp 并行管理的方式有很多种: 命令行 一般是for循环 脚本 一般是expect+ssh等自编辑脚本 工具 pssh,pdsh,m ...

  8. BugkuCTF sql注入

    前言 写了这么久的web题,算是把它基础部分都刷完了一遍,以下的几天将持续更新BugkuCTF WEB部分的题解,为了不影响阅读,所以每道题的题解都以单独一篇文章的形式发表,感谢大家一直以来的支持和理 ...

  9. TRIO-basic指令--九九乘法表demo

    在路上闲的没事,想到之前自己用别的语言实现乘法口诀表,于是来了兴趣用TRIO-basic试一下,挺简单的一段代码,大家看看就好. ' TRIO-basic '实现乘法口诀表 定义两个整型的局部变量 D ...

  10. 解决Jira和Confluence访问打开越来越缓慢问题

    Jira和Confluence部署在同一台服务器上,跑一段时间后,发现访问jira和confluence时,打开越来越缓慢.这是因为根据主机物理内存不同,默认的java虚拟机内存也会不同(一个较低值) ...