HDU1116(欧拉路径+并查集)
题意:
给出一些字符串,有这两个字符串,如果第一个字符串的最后一个字母和第二个字符串的第一个字母是一样的,则这两个字符串是可以连接在一起的。
问给出的这些字符串能否串成一个环或者一整个链。
思路:
将头部看做是入度,将尾部看做是出度,如果是一个链的话那么
链的头部那个字母:indegree = outdegree+1;
链的尾部那个字母:indegree+1 = outdegree;
中间出现的字母:indegree = outdegree;
首先用并查集看看给出的这些链能不能连成一个连通分量,如果不是一个连通分量,那就一定不能连成一条链或者一个环。
然后根据欧拉路径(欧拉回路)的入度和出度的性质来判断。
欧拉路径:(百度百科)
代码:
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <iomanip>
#define MAX 1000000000
#define inf 0x3f3f3f3f
#define FRE() freopen("in.txt","r",stdin) using namespace std;
typedef long long ll;
const int maxn = ;
int n;
int fa[],vis[],in[],out[];
char str[maxn]; int _find(int x)
{
return fa[x]==x ? x:fa[x] = _find(fa[x]);
} void init()
{
for(int i=; i<; i++)
{
fa[i] = i;
in[i] = out[i] = ;
vis[i] = ;
}
} int main()
{
//FRE();
int kase;
scanf("%d",&kase);
while(kase--)
{
init();
scanf("%d",&n);
for(int i=; i<n; i++)
{
scanf("%s",str);
int u = str[]-'a',v = str[strlen(str)-]-'a';
in[u]++;
out[v]++;
vis[u] = vis[v] = ;
u = _find(u);
v = _find(v);
if(u != v)
{ fa[u] = v; }
}
int a=,b=,c=,d=;
for(int i=; i<; i++)
{
if(vis[i] && fa[i]==i) { c++; }//首先得是一个连通分量才能连成一个环或者是一条链
if(vis[i])
{
if(in[i]==out[i]) continue;
else if(in[i]+==out[i]) a++;//尾部的字母
else if(in[i]==out[i]+) b++;//头部的字母
else d++;//中间不符合条件的情况
}
}
//cout<<a<<" "<<b<<" "<<c<<endl;
if(c> || d>) { puts("The door cannot be opened."); }
else if((a==&&b==) || (a==&&b==)) { puts("Ordering is possible."); }
else { puts("The door cannot be opened."); }
}
return ;
}
HDU1116(欧拉路径+并查集)的更多相关文章
- hdu 1116 Play on Words 欧拉路径+并查集
Play on Words Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- POJ2513:Colored Sticks(字典树+欧拉路径+并查集)
http://poj.org/problem?id=2513 Description You are given a bunch of wooden sticks. Each endpoint of ...
- 【hihocoder】欧拉路径 并查集判连通
#include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...
- UVa 10129 (并查集 + 欧拉路径) Play on Words
题意: 有n个由小写字母的单词,要求判断是否存在某种排列使得相邻的两个单词,前一个单词末字母与后一个单词首字母相同. 分析: 将单词的两个字母看做节点,则一个单词可以看做一条有向边.那么题中所求的排列 ...
- POJ 2513 字典树+并查集+欧拉路径
Description: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 解题思路: 可以用图论中欧拉路的知识来解这道题,首先可以把木 ...
- PKU 2513 Colored Sticks(并查集+Trie树+欧拉路径(回路))
题目大意: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相连接的一端必须是同颜色的. 解题思路: 可以用图论中欧拉路的知识来解这道题,首先可以把木棒两端看成节点 ...
- hdu1116 Play on Words--并查集
原题链接: pid=1116">http://acm.hdu.edu.cn/showproblem.php? pid=1116 一:原题内容 Problem Description S ...
- POJ2513 【并查集+欧拉路径+trie树】
题目链接:http://poj.org/problem?id=2513 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total ...
- UVa 10129 Play on Words(并查集+欧拉路径)
题目链接: https://cn.vjudge.net/problem/UVA-10129 Some of the secret doors contain a very interesting wo ...
随机推荐
- 51nod 1062【水题】
直接打表构造啊 #include <stdio.h> #include <string.h> #include <iostream> using namespace ...
- python __builtins__ 函数
dir(__builtins__) 1.'abs', 对传入参数取绝对值 abs(x, /) Return the absolute value of the argument. >>&g ...
- bzoj 4316: 小C的独立集【仙人掌dp】
参考:https://www.cnblogs.com/clrs97/p/7518696.html 其实和圆方树没什么关系 设f[i][j][k]为i点选/不选,这个环的底选不选 这个底的定义是设u为这 ...
- bzoj 5019: [Snoi2017]遗失的答案【dp+FWT】
满足GL的组合一定包含GL每个质因数最大次幂个最小次幂,并且能做限制这些数不会超过600个 然后质因数最多8个,所以可以状压f[s1][s2]为选s1集合满足最大限制选s2集合满足最小限制 dfs一下 ...
- 11.6NOIP模拟赛
[数据规模和限制] 对于全部测试数据,满足 N,M,K≤,W≤ 各个测试点的数据规模及特殊性质如下表. 测试点 N M K ≤ ≤ ≤ ≤ ≤ ≤ ≤ ≤ ≤ ≤ 师 更多咨询:北京信息学窦老师 QQ ...
- 51NOD 1134 最长上升子序列
给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素是递增的) 例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10. 输入 第1行:1个数N, ...
- 线段树(单点更新) HDU 1754 I Hate It
题目传送门 /* 线段树基本功能:区间最大值,修改某个值 */ #include <cstdio> #include <cstring> #include <algori ...
- c语言读取一个文件夹下的全部文件(jpg / png 文件)
#include <cstdio> #include <cstring> #include <unistd.h> #include<dirent.h> ...
- azkaban-web-start.sh启动时出现Table 'execution_flows' is marked as crashed and should be repaired Query错误的解决办法(图文详解)
问题详情 [hadoop@master bin]$ ./azkaban-web-start.sh Using Hadoop Using Hive from /home/hadoop/app/hive ...
- Suricata的规则解读(默认和自定义)
不多说,直接上干货! 见suricata官网 https://suricata.readthedocs.io/en/latest/rules/index.html 一.Suricata的规则所放位置 ...