题意:

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

解析:

  把每个单词的首字母和尾字母分别看作两个点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. 如何通过C#开发调用Skyline软件中提供的小工具

    熟悉Skyline的朋友会知道,在TerraBuilder和TerraExplorer Pro软件的安装目录里,提供了很多个小工具(exe程序): 虽然我们看不到这些小工具的源代码,但我们还是可以在自 ...

  2. oracle 把查询结果插入到表中几种方式

    转载:Oracle中把一个查询结果插入到一张表中 以下是信息留存: 一.Oracle数据库中,把一张表的查询结果直接生成并导入一张新表中. 例如:现有只有A表,查询A表,并且把结果导入B表中.使用如下 ...

  3. JS-JS作用域问题

    一. js没有块级作用域(可以自己闭包或其他方法实现),只有函数级作用域,函数外面的变量函数里面可以找到,函数里面的变量外面找不到. var a=10; function fn(){ console. ...

  4. C#_委托与事件

    委托: 把方法当作参数进行传递 public delegate void AddDelegate(string name); public class Ad{ //addDelegate就是委托的一个 ...

  5. 利用Tarjan算法解决(LCA)二叉搜索树的最近公共祖先问题——数据结构

    相关知识:(来自百度百科)  LCA(Least Common Ancestors) 即最近公共祖先,是指在有根树中,找出某两个结点u和v最近的公共祖先. 例如: 1和7的最近公共祖先为5: 1和5的 ...

  6. 分布式监控系统Zabbix-完整安装记录 -添加端口监控

    对于进程和端口的监控,可以使用zabbix自带的key进行监控,只需要在server端维护就可以了,相比于nagios使用插件去监控的方式更为简单.下面简单介绍配置:监控端口zabbix监控端口使用如 ...

  7. jackson出现错误 Unrecognized field,几种处理方法

    1.请求的JSON里面字段多余映射的实体类,可以通过在类的顶部添加@JsonIgnoreProperties,2.0版本引入 import org.codehaus.jackson.annotate. ...

  8. Python_函数_复习_习题_24

    # 函数 # 可读性强 复用性强# def 函数名(): # 函数体 #return 返回值# 所有的函数 只定义不调用就一定不执行 #先定义后调用 #函数名() #不接收返回值#返回值 = 函数名( ...

  9. D. Too Easy Problems

    链接 [http://codeforces.com/group/1EzrFFyOc0/contest/913/problem/D] 题意 给你n个题目,考试时间T,对于每个问题都有一个ai,以及解决所 ...

  10. #科委外文文献发现系统——导出word模板1.0

    ps:该篇文档由实验室ljg提供. Crowdsourcing 一.             技术简介 Crowdsourcing, a modern business term coined in ...