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 ...
随机推荐
- 洛谷 - P1443 - 马的遍历 - bfs
略有收获的bfs,使用了try_enqueue函数使得加入队列非常方便.性能理论上是一样的因为是inline? 还有就是左对齐是使用%-4d,相对于右对齐的%4d,还有右对齐前导零的%04d,自己试一 ...
- 模板 - 数学 - 快速傅里叶变换/快速数论变换(FFT/NTT)
先看看. 通常模数常见的有998244353,1004535809,469762049,这几个的原根都是3.所求的项数还不能超过2的23次方(因为998244353的分解). 感觉没啥用. #incl ...
- (水题)Codeforces - 650A - Watchmen
http://codeforces.com/contest/650/problem/A 一开始想了很久都没有考虑到重复点的影响,解欧拉距离和曼哈顿距离相等可以得到 $x_i=x_j$ 或 $y_i=y ...
- Codeforces - 773A - Success Rate - 二分 - 简单数论
https://codeforces.com/problemset/problem/773/A 一开始二分枚举d,使得(x+d)/(y+d)>=p/q&&x/(y+d)<= ...
- Codeforces510B【dfs】
判断一个图里是否有一个自环: 50*50 标记起点,然后暴搜? #include <bits/stdc++.h> #include<algorithm> using names ...
- mysql关于时间函数的应用
1.获取今天的数据 date_format(publishtime, '%Y-%m-%d')= date_format(now(), '%Y-%m-%d') 2.获取今天和明天的数据 server.b ...
- python __builtins__ filter类 (24)
24.'filter', 用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表.该接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为参数传递给函数进行判,然后返回 True ...
- bzoj 2023: [Usaco2005 Nov]Ant Counting 数蚂蚁【生成函数||dp】
用生成函数套路推一推,推完老想NTT--实际上把这个多项式乘法看成dp然后前缀和优化一下即可 #include<iostream> #include<cstdio> using ...
- 笔记:重新认识CSS3
1.CSS3边框 border-radius box-shadow border-image 2.CSS3背景 background-image background-size background- ...
- (DP)51NOD 1007正整数分组
将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 2 4分为1组,3 5分为1组,两组和相差1,是所有方案中相差最少的. 输入 第1行:一个数N,N为正整数的数量. 第 ...